Implementing Conway’s Game of Life in Python
This is a simulation of the late John Conway’s Game of Life that I made using Python 3 and Pygame. I’d heard of this game many times before, but I’d never gotten round to learning more about it until recently.
Conway’s Game of Life
Conway’s Game of Life isn’t really a “game” in the conventional sense of the word because there are no players involved, making it a zero-player game. This might not seem very interesting on the surface, but the Game of Life has managed to grab the same level of attention (or more) as some of Conway’s other discoveries. So what makes it so intriguing?
One of the reasons for this is that the rules of Life are very simple, but somehow lead to really complex and unpredictable (not a technical term)1 behaviour. There are only three simple rules of Life (see next section), but some of the patterns that people have come up with are truly remarkable.
Another reason why people find the Game of Life interesting is because it is able to simulate any computer algorithm, which can be carried out by a Turing Machine (assuming the Church-Turing thesis). Paul Rendell has even managed to do this!
The basic rules of the game are (from Wikipedia):
- Any live cell with two or three live neighbours survives.
- Any dead cell with three live neighbours becomes a live cell.
- All other live cells die in the next generation. Similarly, all other dead cells stay dead.
The game starts off in a dead state by default, where a blank grid is displayed. The option is provided to set the state to a random state, or a particular initial state can be set up by clicking on desired squares (i.e. mouse editing). To run or move to the next iteration, mouse editing must be disabled. “Runs” or automatic iterations can be paused, and after enabling mouse editing the new state can then be modified.
You can try out my implementation of the Game of Life here. Below are some snapshots of the simulation:
Gosper’s glider gun periodically shoots gliders down to the bottom right of the screen
A pseudo-randomly generated start state
Most cells have died after 108 iterations
SPACE: Enable/disable mouse editing
RIGHT_ARROW: Next iteration of Life. Requires disabled mouse editing.
P: Play/pause automatic iterations. Requires disabled mouse editing.
D: Dead state, or an empty board. Requires disabled mouse editing.
R: Random state. Requires disabled mouse editing.
When mouse editing is enabled, squares can be clicked on to change their state. Live cells are blue and dead cells are white. Other features like the board size, rate of iteration, colours, etc. can be changed in the “constants.py” file – see the GitHub repo for this.
Note that this isn’t a technical definition – in principle, the entire future of Life could be predicted on the basis of a known initial state, given enough computation. What I mean by “unpredictable” is more along the lines of “doesn’t match what we intuitively expect to see”. ↩