(note the simulation was limited to 100 iteration for the sake of the .gif filesize)
Six years ago (September 2011), I needed a science fair topic. Having recently been introduced to and fascinated by John Conway's Game of Life, I formulated a hypothesis along the lines of "There exists an optimal initial population density to maximize generations until stabilization in John Conway's Game of Life." I decided to revisit this project now. You can read more about The Game of Life here, or use a simple online simulator here. Different branches off this idea and implementation decisions are given their own file, detailed below.
This was my first implementation in the revival of this project. It runs a simulation of The Game of Life and displays each iteration on screen. The grid size is a constant which may be adjusted, as is the character which is printed for "living" cells. The time each iteration is shown before the next is specified in seconds by DELAY
. You can also set a timeout in the form of MAX_ITERATIONS
. This program uses the curses
library to draw over the screen instead of printing many, many lines. Be sure your terminal is at least SIZE
+2 lines high. When the simulation completes you may press Enter to run another, or any other key to quit.
This is a trimmed down version of the above. It cuts out all of the UI code (besides a single print
statement) to focus on the clarity and readability of the core logic. make2DList
is a helper method which accept arguments for the height and width of the 2D list (or array, if you prefer) to create, and a function to generate the value for each element. This last parameter is optional, by default it will simple result in an array full of False
. iterate
accepts a game state in the form of a 2D list and returns the following generation. simulate
will, given a grid size and percent of cells to be initially alive, randomly generate and simulate a game of life. Note this fraction should be between 0 and 1, not 0 to 100. The function will return False
if the population was eradicated, or True
if it stabilized in a cycle of repeated iterations. If executed as __main__
, it will run a hundred-thousand trials on each grid size 5x5 through 9x9, and each starting population density 0.1 through 0.9, and print the percentage of simulations that stabilized (did not die out).
I was having some trouble with matplotlib on Windows; so, knowing Tk is cross-platform, I wanted to use the tkinter
module included in Python to visualize the simulations.