CS21 Lab 12: Game of Life

Due 11:59pm Saturday night, Dec 6

You may work with a partner on this assignment

Run update21, if you haven't already, to create the cs21/labs/12 directory. Then cd into your cs21/labs/12 directory and create the python programs for lab 12 in this directory:

$ update21
$ cd cs21/labs/12
$ vim gameoflife.py
Then copy your grid.py solution from your labs/11 directory into your labs/12 directory:
$ cp ../11/grid.py .
$ ls
gameoflife.py   grid.py
Introduction

For this assignment, you will implement a class, GameOfLife, that can be used to play John Conway's Game of Life. The GameOfLife class will use the Grid and Cell classes you wrote for lab 11.

The Game of Life simulates evolution of a cellular automata in a 2-D world. At each time step in the simulation, a cell either lives or dies based on it's state and the state of it's neighbors.

In this game, after an initial configuration of the world's cells are set, then at each time step a cell's state changes according to the following rules:

Requirements
  1. You must implement a GameOfLife class that has a Grid data member. You may add additional data members as you like.
  2. Your GameOfLife class should have at least these methods (you may add more if you'd like):
    • __init__(row, cols): will create a new GameOfLife object and create it's Grid object data member with the passed number of rows and cols.
    • close(): will invoke the Grid's close method that will not close the graphics window until the user clicks on it.
    • startRandomGame(): creates a starting point world where Cells are randomly selected to be live or dead
    • startWalkerGame(): creates a starting point world of the walker pattern in the lower left corner (see the Sample output section for what this pattern should look like)
    • startExpanderGame(): creates a starting point world of the expander pattern such that it is drawn CENTERED in the world grid (see the Sample output section for what this pattern should look like)
    • playGame(num_steps): play the game of life on the current world state for num_steps time steps (see the rules of the game of life above). Use the sleep function to sleep some number of tenths of a second between each time step
  3. You should write a main program that prompts the user to enter the number of rows and columns in the world, then creates a GameOfLife object of the given dimensions. For each of the game starting points (random, walker and expander) in turn your program should:
    1. call the startX method to initialize the world for the appropriate game of life
    2. prompt the user to enter the number of time steps to simulate this game
    3. play the game of life
Sample output
$ python gameoflife.py

This program plays Conway's Game of Life

Enter number of rows in the grid: 21
Enter number of cols in the grid: 21

Enter the number of steps to run the random game: 30

Enter the number of steps to run the walker game: 50

Enter the number of steps to run the expander game: 20
Click to exit
Test Patterns

If you want to test your game, to make sure it is working, try a simple test pattern such as the plus sign on the left below. If you start with the plus sign, it will evolve into the box on the right, and then back and forth between the two states.

Optional Extensions
As always, do not even think of trying any of these until you have the basic assignment implemented and tested.

Here are some ideas for extensions:

Submit

Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.