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.
isAlive
function
always returns 0, so all the cells are dead after the first step.
So the animation will turn all black after the first step. (Ask an
instructor or TA if you need help setting this up.)-->
countNeighbors
function. (There
will be three lines for you to add the eight neighbors of the given
cell.) Test it in iPython
with the following
statements:
In[1]: w = generateWorld(10, .3) In[2]: countNeighbors(w, 5, 5)The
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
print(world)
statement before or after the
display(world)
statement inside the loop in the
simulation
function.
python -> Preferences
menu,
then choose iPython console
. Go to the
Graphics
tab, and change Graphics Backend
to Automatic or TKinter. (It is probably set at Inline.) Then
start a new iPython console. Run your program in this new console.
Tools ->
Preferences
menu, then choose iPython
console
. Go to the Graphics
tab, and change
Graphics Backend
to Automatic or TKinter. (It is probably
set at Inline.) Then start a new iPython console. Run your
program in this new console. You may need to run it twice in order
for the interactive window to open up.