< Back

Lab 31: Game of Life

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

$ cd ~/cs120/labs
$ mkdir lab31
$ cd lab31


Conways's Game of Life

In Conway's game of life, a two dimensional grid represents a petridish. Each cell in the grid is either occupied by a bacteria (1), or not (0). In each turn of the game every cell in the grid changes state based on the following rules:

Details

Create a file called game_of_life.py to play Conway's Game of Life. The program should load a board, a two-dimensional list of 0s and 1s, from a file and repeatedly print the result of updating the board according to the above rules. The program should end when the user kills the program by pressing ctrl-c.

The first line of the input file is going to be a specification of the number of rows (\(m\)) in the board and the number of columns (\(n\)) in the board, separated by a single space. These tell you the number of lines to follow, and the length of each line. The rest of the input file should be formatted as an \(m \times n\) grid of 1's and 0's.

Example

00000000000
00000000000
00000000000
00000000000
00000100000
00001110000
00000100000
00000000000
00000000000
00000000000
00000000000

00000000000
00000000000
00000000000
00000000000
00001110000
00001010000
00001110000
00000000000
00000000000
00000000000
00000000000

00000000000
00000000000
00000000000
00000100000
00001010000
00010001000
00001010000
00000100000
00000000000
00000000000
00000000000

Hint

  • Create a function called read_game_state(filename). This function takes as parameter the name of a file holding a board. It needs to read the first line of the file to determine how many rows and columns the overall board will contain. It should then read the rest of the game board using a nested for loop. This function should return the actual game board.
  • Create a function called sum_adjecent_cells(board, row, col). The function should have three parameters, board, a two-dimensional list of 0s and 1s, and row and col, integers that are the row and column of a cell in the board. The function should return the sum of the cells that are adjacent to the specified cell. Note, if the cell is on the edge of the board, some of the adjacent cells may not exist. In this case, the function should sum the adjacent cells that do exist.
  • Add a function called update_board(board). The should have one parameter, board, a two-dimensional list of 0s and 1s. The function should return a new two-dimensional list of 0s and 1s that is equivalent to the input board after being updated according to the rules of the game of life. This function should call the sum_adjacent_cells function in order to determine the new state of a cell. Note, the input board should not be modified. If it is, subsequent cell updates may be incorrect. You must create a new, empty board that is the same size of the input board. This new board will be where updates to the board are placed.
  • Finally, after you have tested the above functions, create a program that plays the game. The program should call the read_game_state function to initialize the board. It should have a loop that repeatedly prints the board, using the print_game function, and updates the board, using the update_board function.

Challenge

As you have no doubt seen, watching the game of life through the terminal window is incredibly difficult. The 1's and 0's flying past the screen do very little to aid in your visual of what is going on. Let's layer a Graphical Interface on top of your 2-d list representation, which hopefully will make it easier to visualize.

Details

In the same file as above, create a new function called draw_game. This function should take the game board as a parameter, plus any additional information necessary to perform the drawing. You should be able to simply call draw_game in the same place you have been calling print_game, with minimal changes else where.

Your visual should consist of a 2-d grid, where each cell in the grid represents one of the values from the 2-d list of values representing the game board. Cells that are valued 0 will not be filled in, while cells that are valued 1 will be filled in with a color of your choosing (traditionally black).

Your grid should fill as much of the window as possible. As such, you should decide the size of the cells based off the dimensions of the window, as opposed to specifying a default grid size.

Example