Game of Life

 


Introduction

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:

By enforcing these rules in sequential steps, beautiful and unexpeted patterns can appear.

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.



Exercises

  1. Download the file gameOfLife.py and open it in Spyder.

  2. Look over the functions in that file for their purpose and how they will work together. Don't worry if some of the code doesn't make sense. Focus on reading the comments.

  3. Run the program. At this point, you should see a series of arrays of 0s and 1s printed. (For now, keep the size small, like 5x5 or 6x6, so you can see the entire array to test it is working properly.) go to Python -> Preferences -> iPython console. Then select the Graphics tab. Experiment with Automatic or Tkinter for the Graphics Backend. Just remember that at this point, the 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.)-->

  4. Complete the 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).

  5. Test countNeighbors with other cell locations, including cells on the borders and cells in the corners.

  6. Complete the code for the 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.

  7. Complete the code in the 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.

  8. Test the 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.

  9. Experiment with different size worlds, different percentages of cells that start as alive, and different numbers of generations. Do you see any interesting patterns?

  10. To see the images that represent the worlds from the different generations change more like in an animation, we will make a change to the iPython console settings and open a new console.
    To do this on a mac, go to the 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.
    To do this on a PC, go to the 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.

Submit