Conways Game

Starting off, let me declare, the logic of the game appeared rather simple at first but testing all conditions are complicated. Hopefully through this post I can clarify to myself how the game works. The output should be a continuous stream of moves and printing the board after every move. The program will continue to run in an infinite loop.

width	:	60
height	:	20





### #  ## # ##   #  # ######    ### ## #    ##   ### # # ###
## #      ## # ##  ### ##  # # #  # ## ###   #   # #  ###   
########### ## #   #    ###########  ### ##  ###### ##  ## #
#### ### ## ##    # # #     # ## # #    #### # # ## ##  # ##
  #  ## ## #  # ##  #   ####    ## ###   #  # ### ######### 
# ### ## #### ##     ##  #     #  ## ## ## #   # ###### #  #
     ## # ######     ## ## #  ## #   ## # ##  ###         # 
#          #     #   #  ##       ####  #### ##  ####       #
 ####    #  ##  ###          #    # ##  ##  ##  ##   ## # ##
#  ## #   # ###### #         # # ###   ##    ##  # ## #### #
#### #  # ##     #    ##    ## # ####   ## ###### # ## ## ##
### # ### ###  ## ##   #  ## # # ####       #  #  ##  # # ##
   #    ## ###  # ##   #   ### ### #  ### # # ##      ######
#### ####   # #   #  #  # #  #####      ### # # #   ## ##   
# # ### ### ## ##    ##  # #      # # ## # ### ##    #### # 
   # ## ## # ## ##  ##    ##  ######         ####    #     #
 ### #########  #   ### #  ##  ######    ##  #  # #    # ## 
#  #    # # # ## ## ## #  #  #  #     #  ####  ### #  ######
 #   #   ###     ##### ##### ## #### #  # #  #  ## #    ### 
## # ## ##  ## ### ######   #### ## # # ##   ####      # # #





    ##### #      #        ##                #  # # ##    #  
               ## ## #       #           ##    #            
               ## #   # ###          #     # # #            
               #### ## #           #   #   #                
              # ## ## ######  #  #   ###    ##              
 ####               # #    #  ##   #    #  ###          #  #
 # #### #      ##   #         ## #           ##      #    # 
######   #     # ##  ######   # ##                #      #  
 ## ##    #                   # #    ##   ##   #     ## #   
          #   ##             #       # #  ##       #        
        #          #  ##   # # #       ### #    #           
    ###      # ##  #   ## #    #     #    #      ####       
              # #   # ####         ##  ## # # # #  ##       
#             # # #### ####  #     # #          #   #       
#         #     #     #  # ###         # # #     #      ##  
#                #     # #    #      #  ## #         #     #
 # # #            #    # #  ##      #    #   #    #    #    
#  # #        ##             #       #  #   ####   #  #     
 #   ###                  ##       #### #    #        #     
   # ##      ##                       # ##     #   ## ##    





    #  #      #####                     # #   ##  ####      
     ###       #  ##       #              ###  #            
              #       ####                 ## #             
              #     #      #        ## #   # ##             
  ##                    ####  ##  # # ###    #              
##               #  # # ## # #    #   ##   #                
      #        ### ##   ###  #   ##          ##          ###
#        #     # #   #####   ##  #            #      ##  #  
#    #   ##   ###     ####   ## ##   ##   ##                
         #                  ## #     # #    #               
     #          #     ###   #          ### #     #  #       
     #        ###  ###    #   #     # #   #    #### #       
     #       ## # #                # ##  # #   ##    #      
                #  ##      # #     #  ## ###   ### ##       
##             ## #####    ####     # #  #                  
##               #    ## # #  #          #  #           #  #
 ##                         ###     ##   ##  #        #    #
##   #                    ## #     #  ####  #         ##    
  #    #     # #                    # # #   ## #   ## #     
        #                           # # ##    # # ### ##    

The Logic

The logic of the game is rather simple to read, albeit a bit complicated to program. To be honest, the code I have written is almost a direct excerpt from the book.

To clarify, it is as follows:

  1. A filled square can be “Alive” or “Dead”.
  2. If a Living square, it continues to Live in the next step if it has two or three living neighbors.
  3. If a Dead square, it comes alive in the next step if it has exactly three living neighbors.
  4. Every other square dies or remains dead in the next step.

The Imports

To start off, I will begin by explaining the imports and their uses. As you will notice I import the sys, random, os, platform, time and copy. Not to forget the nameof() import, which you will notice I have used a lot for the control variables and debugging purposes.

import sys,random,os,platform,time, copy
from varname import nameof

Initialization

Now I will proceed to initialize the board, by the book, we are creating a grid pattern of 60 columns and 20 rows.

width = 60
height = 20

As a control, I have even proceeded to print the values of height and width. This for me was a debugging step hence I have printed out these values. Then I proceed to create a list, as I understand it, this list will contain the entire playing board including, the living and the dead cells.

Notice on the first run, we are assigning random values to the playing board. This initializes the board.

for x in range(width):
    column = []
    for y in range(height):
        if random.randint(0,1) == 0:
            column.append("#")
        else:
            column.append(" ")
    nextcells.append(column)

The Main Loop

Now, lets look at the main loop. To start I first enter the print command to leave a few newline characters (“\n”), this will create a few blank lines between every iteration of the game (each move).

To start off we create another working deep copy of the board in the currentcells variable.

    print("\n\n\n\n")
    currentcells = copy.deepcopy(nextcells)

Now as is slightly obvious the currentcells variable contains the board as it stands now. Further the nextcells variable contains the boards’ next move. The next move is decided from the list of gameplay rules mentioned above in the logic section. Prior to calculating the nextcells we find out the number of neighbors with the current standing of the board.

numneighbours = 0
            if currentcells[leftcoord][abovecoord] == "#":
                numneighbours += 1
            if currentcells[x][abovecoord] == "#":
                numneighbours += 1
            if currentcells[leftcoord][y] == "#":
                numneighbours += 1
            if currentcells[rightcoord][abovecoord] == "#":
                numneighbours += 1
            if currentcells[rightcoord][y] == "#":
                numneighbours += 1
            if currentcells[leftcoord][belowcoord] == "#":
                numneighbours += 1
            if currentcells[x][belowcoord] == "#":
                numneighbours += 1
            if currentcells[rightcoord][belowcoord] == "#":
                numneighbours += 1

After calculating the number of neighbors I proceed to construct an if-else check condition for the three main moves in the game.

            if currentcells[x][y] == "#" and (numneighbours == 2 or numneighbours == 3):
                nextcells[x][y] = "#"
            elif currentcells[x][y] == " " and numneighbours == 3:
                nextcells[x][y] = "#"
            else:
                nextcells[x][y] = " "

As you can probably notice the if-else conditions have been set verbatim to the conditions in the logic section above. Finally we induce a small pause between the moves of the game, here is where we will make full use of the time package using the sleep() method. Keep in mind the time is in seconds.

    time.sleep(1)

Final Thoughts

It was interesting trying out the logic of the game, albeit slightly complicated. Finally though it is interesting to see the random moves that can be made in the game. Another side-note the random moves in the game and the setup of the board can be used to emulate random decision making. Since the initialization is random, so are the next moves depending on the basic three conditions in the logic of the program. An improvement that can be made is to put everything in various functions and have only the function calls in the main program loop.

About: sahilkapila