In 1970, the British mathematician John Conway created his "Game of Life" -- a set of rules that mimics the chaotic yet patterned growth of a colony of biological organisms. The "game" takes place on a two-dimensional grid consisting of "living" and "dead" cells, and the rules to step from generation to generation are simple:
The purpose of this mini-lab is to practice creating and working with two-dimensional arrays in Python, and to gain a little experience working with more complicated programs that require multiple programmer defined functions.
countNeighbors
function. (There
will be three lines for you to add the eight neighbors of the given
cell.) To test that this function works correctly, add a Code cell
at the bottom of the program and add the following two statements to
it:
w = generateWorld(10, 0.3)The
countNeighbors(w, 5, 5)
generateWorld
function will display the world, so
check that countNeighbors gives the right number of neighbors for
the cell at (5,5).
countNeighbors
with other cell locations,
including cells on the borders and cells in the corners.
isAlive
function. You
should be writing nested if-else
statements by
following the logic described in the comments within the function.
Test it in iPython
with the world you created to test
the countNeighbors
function and different cell
locations. Be sure to test cells that you know are alive that will
stay alive, cells that are alive, but will die (by either underpopulation
or overpopulation), cells that are not alive, but will come
alive, and cells that are not alive and do not come alive.
simulation
function by
filling in the code to set the cells in the nextWorld
to be alive or dead. There is pseudcode for the necessary
statements in the comments within the
function that should help.
simulation
function by calling
main
. To begin the testing, use a 10 x 10 size world,
with 2 generations and 0.3 percent of the cells being alive.
Check that the program does what you expect. If you would like to
see the actual 0s and 1s of nextWorld
, you could add a
printWorld((world)
statement before or after the
display(world)
statement inside the loop in the
simulation
function.