authorimg

Game of life

Ileana

5.0

Here is Your Sample Download Sample 📩

Game Of Life

This project is developed to develop a program in python that to make the game of Life. This game was invented by British mathematician John Horton Conway in 1970. (Izhikevich et al., 2015)

Game Description

In the classical Game of Life Universe there is a two-dimensional grid that has square cells and each state of cell is is either dead or alive. The dead cell is represented with 0 and the live cell is represented with 1. There are eight states that interact with each other and in each step taken the following changes occur:

  • If there are less than two neighbours in a live cell, the cell dies due to underpopulation.
  • If there are more than three neighbours in a live cell, the cell dies due to overpopulation.
  • If there are two or three neighbours in a live cell, the cell continues to live.
  • If a dead cell has exactly three neighbours, then the cell becomes alive again. (Izhikevich et al., 2015)

This game is assumed to be played on a Torus which means that the left border of the board is attached to the right border, and the top border of the board is attached to the bottom border. So the eight neighbours of the cell should be calculated using

  • board[(i-1+n)%n][(j-1+n)%n],
  • board[(i-1+n)%n][j],
  • board[(i-1+n)%n][(j+1)%n],
  • board[i%n][(j+1)%n],
  • board[(i+1)%n][(j+1)%n],
  • board[(i+1)%n][j],
  • board[(i+1)%n][(j-1+n)%n],
  • board[i][(j-1+n)%n].

As per the question the program is developed in python using the object- oriented approach. (Bassi, 2016)

Game of life class is defined which contains the board description and contains

member functions to create the first generation and update the generation through

user's choice

class GameOfLife

    constructor for initialisation of board

    def __init__(self,i):

        # declaring board with 0 values first

        self.board = []

        # setting the size to 20x20

        rows, cols = 20, 20

        for k in range(rows):

            col = []

            for j in range(cols):

                col.append(0)

            self.board.append(col)

        # depending on user input the function for the particular pattern will be called.

        if(i ==1):

            self.initialise_board1()

        elif(i==2):

            self.initialise_board2()

        elif(i==3):

            self.initialise_board3()

    Function that will create the

    first initial pattern of board

    def initialise_board1(self):

        print('PATTERN 1')

        # the binary code for pattern 1

        temp = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0]

                , [0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0]

                , [0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0]

                , [0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0]

                , [0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0]

                , [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

        self.board = temp.copy()

""""

    Function that will create the

    second initial pattern of board

    """

    def initialise_board2(self):

        print('PATTERN 2')

        # the binary code for pattern 2

        temp1 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0]

                , [0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0]

                , [0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0]

                , [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                , [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

        self.board = temp1.copy()

""""

    Function that will create the

    third initial pattern of board

    """

    def initialise_board3(self):

        print('PATTERN 3')

        # the binary code for pattern 3

        temp2 = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0]

                ,[0,0,0,0,0,0,0,0,0,1,1,1,0,1,1,0,0,1,1,0]

                ,[0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,1,0]

                ,[0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0]

                ,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,1,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                ,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

              ]

        self.board = temp2.copy()

    """

    function defined to print the board

    """

    def printBoard(self):

         for k in range(20):

            for l in range(20):

                # if the value of pattern code is 1 then * will be printed

                if(self.board[k][l] == 1):

                    print('* ', end="")

                # if the value of pattern code is 0 then blank will be printed

                elif(self.board[k][l] == 0):

                    print('  ', end="")

            print('')

    function defined to calculate the sum

    of live neighbours in the torus board

    def neighbourSum(self,i, j):

        # the calculation for  sum of live neigbours. the board is torus

neighbourTotal = self.board[(i-1+20)%20][(j-1+20)%20]+ self.board[(i-1+20)%20][j]+     self.board[(i-1+20)%20][(j+1)%20]+ self.board[i%20][(j+1)%20]+ self.board[(i+1)%20][(j+1)%20]+ self.board[(i+1)%20][j]+ self.board[(i+1)%20][(j-1+20)%20]+ self.board[i][(j-1+20)%20]

        return neighbourTotal

    function that will calculate the next pattern of generation

    """

    def nextPattern(self):

        # creating a temporary board first of size 20x20

        tboard = []

        rows, cols = 20, 20

        for i in range(rows):

            col = []

            for j in range(cols):

                col.append(0)

            tboard.append(col)

        # loop to check if each cell of board is alive or dead

        for i in range(20):

            for j in range(20):

                # calling the neighbourSum function to get the total live neighbour count

                nsum = self.neighbourSum(i,j)

                # if the cell is alive

                if(self.board[i][j] == 1):

                     # if the total neighbour is less than 2 or greater than 3 then the cell dies due to overcrowding

                    if(nsum<2 or nsum>3):

                        tboard[i][j]=0

                    # if the total neighbour is less than 2 or greater than 3 then the cell dies due to overcrowding

                    elif(nsum>=2 and nsum<=3):

                        tboard[i][j]=1

                # if the cell is dead

                else:

                    # if there are exactly 3 neigbours the cell becomes live again

                    if(nsum == 3):

                        tboard[i][j]=1

        self.board=tboard.copy

The main function from where

the program execution begins

    def main()

    #creating 3 objects of class for 3 different patterns

    game1 = GameOfLife(1)

    game1.printBoard()

    game2 = GameOfLife(2)

    game2.printBoard()

    game3 = GameOfLife(3)

    game3.printBoard()

    # creating another variable to store the objects based on user input

    obj=None

    # asking for input in while loop so that when user inputs any value other than 1 2 3 it keeps asking for valid input

    while(1):

        # using try except to check the user input

        try:i = input("Please enter the pattern number between 1,2,3  you wish to play with: ")

            # if condition to  send the respective pattern object to class and the loop breaks.

            if(i=="1" )obj=game1

                break

            elif(i=="2"):

                obj=game2

                break

            elif(i=="3"):

                obj=game3

                break

            else:

                print("Wrong Input!")

        # for wrong input exception is raised and the loop continues

        except ValueError:

            continue

# loop to keep printing next pattern if the user wants to continue

    while(1):

        # first the board is printed

        obj.printBoard()

        # user is asked if they wish to continue

        ch=input("To print the next pattern enter Y/y. To end the game enter any other symbol: ")

        if(ch=='Y' or ch =='y'):

            # next pattern function is called.

            obj.nextPattern()

            obj.printBoard()

        else:

            exit()

# main function is called

if __name__ == "__main__":

    main()

Output

First the three initial patters are printed on the output screen

Then the user is asked to enter the pattern number to play the game. The chosen pattern is printed again.

The user is then asked to enter Y/y if they wish to print the next pattern and any other symbol to exit the game. If they enter y the next pattern is printed.

After some number of iteration of printing the next pattern, it goes back to the initial pattern again.

If they enter any other symbol than Y/y the game exits.

Game Testing

The testing of the game has been done using  where the user is allowed to draw the initial pattern and create the next generation pattern. So, pattern 1 was drawn and the subsequent patterns where matched with the program’s output which gave successful result.

References

Bassi, S. (2016). Introduction to object orienting programming (OOP). Python for Bioinformatics, 165–184. 

Izhikevich, E., Conway, J., & Seth, A. (2015). Game of Life. Scholarpedia10(6), 1816. 

Play John Conway's Game of Life. Play John Conway's. (n.d.). Retrieved September 15, 2022, from