Project 7: Game of Life
You will be implementing Conway's Game of Life.
Read about it.
See a demonstration.
How the program should work
Write a program that asks the user to type in five things:
- The number of rows the grid should have.
- The number of columns the grid should have.
- The filename containing the starting pattern of alive/dead cells.
- The number of generations the program should run.
- The amount of time (in seconds) to pause between generations (to give time to see the picture
before it is overwritten by the next generation).
The program should calculate each generation and display it graphically, pausing between
each generation so the output can be seen. Good values for the pause are 0.1 seconds for high-speed, or 1
second for slow.
The starting cell file format
The file format is one you are already familiar with: each line of the file will have
two integers, corresponding to the row and column of a cell that starts off alive.
Every other cell starts off dead.
Try these files. All are taken from
here.
- blinker (try with rows=3 and cols=3)
- beacon (try with rows=4 and cols=4)
- glider (try with rows=40 and cols=40; it should move from upper left to lower right)
- lightweight spaceship (try with rows=7 and cols=40; it should move from left
to right)
- Gosper's glider gun (try with rows=30 and cols=40; this one is slow because it's big)
Hints
Try these steps:
- USE THIS STARTING CODE! It will help you, because all you have to do is
fill in some functions. Read the entire file to get a sense of how the program is supposed to work!
- Fill in read_starting_board. This function should read in the file specified, and set
board[row][col] to 1 if the coordinate "row col" appears in the file you're reading. Run the program to make
sure that the board gets printed the way you think it should be. For instance, for life-blinker.txt, the
board should be printed as:
000
111
000
- Fill in get_number_of_alive_neighbors. This function returns the number of alive neighbors
the cell at position [row_num][col_num] has. We discussed this in class on 11/26.
- Fill in make_next_generation. Use nested loops to check for each cell whether it should be alive
or dead in the next generation.
- At this point you are done with everything except changing from text-based output to graphics.
Make sure your output is correct before continuing! For example, try the glider example above and make sure it
moves from upper left to lower right.
- Fill in draw_board. Use a nested loop like print_board.
Each alive cell should be drawn as a 10 by 10 square on the canvas. The hard part
here is figuring out the coordinates of where to draw each square. Use the draw_filled_rect
function, which takes four arguments: the x and y coordinates for the upper-left corner of
the rectangle (square in this case), and the width and height of the rectangle. Clearly, width
and height are 10. Use your math skills to figure out the x and y for the upper left corner.
Hint: if board[0][0] is alive, then you should draw a square with upper left at (0, 0). But
if board[1][1] is alive, you can't draw that square at (1, 1), because it would overlap
with the square at (0, 0)....what would the appropriate coordinates be?
What to turn in
Through Moodle, turn in your code as a file called life_yourLastName_yourFirstName.py.