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 notebook GameOfLifeLists.ipynb and save it to your Google drive. Then open the notebook in Google Colab.

  2. Look over the functions in that notebook 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 list of lists of 0s and 1s printed followed by a square with black and white blocks followed by some black squares. (For now, keep the size small, like 5x5 or 6x6, so you can see the entire array to test it is working properly.)

  4. Complete the 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)
    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 printWorld((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. Add a Text cell at the end of your notebook and write a summary of what you discovered while experimenting with different input parameters.

  11. (Optional Challenge): Read through the article Conway's Game of Life in Python. In particular, read about some interesting starting configurations. Modify your program to run with one or more of these configurations.

Submit