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:
- For a cell that is 'alive' or 'populated':
- if it has only one or no live neighbors, it dies from loneliness
- if it has four or more live neighbors, it dies due to overpopulation
- if it has two or three live neighbors, it lives
- For a cell that is 'dead' or 'unpopulated'
- if exactly three of it's neighbors are alive, it becomes populated
Requirements
- You must implement a GameOfLife class that has a Grid data member. You may add additional data members as you like.
- 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
- 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:
- call the startX method to initialize the world for
the appropriate game of life
- prompt the user to enter the number of time steps to simulate this
game
- 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:
- Add other starting point methods that initialize the world
to different configurations. There are many that will lead to
interesting patterns that move across the space in some way that
converge to a static pattern in some way. Look on-line for some
examples.
- Modify the Grid and Cell class so that a user can use mouse clicks
to turn cells ON or OFF to set a starting pattern for each game.
If you do this extension, submit two solutions: gameoflife.py with the
basic assignment; and gameoflife_ex2.py with this version.
I have not done this extension, so I don't know how difficult
it is, but I suspect that it will require a significant amount of
effort. I think the best way to try this is to add another row of
buttons to the graphics window that have functionality such as start,
stop, clear and quit. The user would then choose clear to clear the grid,
click on individual grid cells to turn them ON or OFF, and then choose
start to start the game of life simulation, and stop to stop it. The
quit button would close the game.
Submit
Once you are satisfied with your program, hand it in by typing
handin21 in a terminal window.