Understand Conway's Game of Life
00:09 One, Conway’s game of life isn’t so much a game as a simulation. It is one type of cellular automata, which is a kind of visual math puzzle that has a regular grid of cells where each cell has a finite number of states and the system is based on rules using the current state to produce the next state in the system.
00:52 Some more complex cellular automata use hexes or even go into the third dimension in Conway’s version. Each square in his grid has only two states, usually referred to as alive or dead, and there are just a few simple rules for determining the next state, all of which are based on the immediate neighbors in the grid.
01:14 Let’s walk through the set of rules. Each of these gets applied to the current state or frame to determine the next state for any given cell. If it is alive and less than two of its neighbors are alive, it dies.
02:29 You can think of this in two different ways, either as the grid being infinite, but only those items being displayed get processed. Or you can think of it as bound and treat out of bound cells as dead.
02:41 Every time I determine the future state of a cell, I consider eight neighbors. In this case, five of which are outside the boundary. None of these eight cells is currently alive, so the first rule applies: less than two are alive, so the future state of the cell is dead, the same as its current state.
03:20 The entire future state is based on the entire current state. You don’t change values in the same grid as you go. There is a variation on the game that does this, but it doesn’t cause the same results.
03:58 I’ve processed a few more cells, once again looking at the question marks. This time there are three neighbors inside the green cells. The current cell is dead and it has exactly three neighbors.
04:10 So rule four gets invoked, this cell gets it on, triggering the reproduction rule, and the future state is alive. Future moving one cell to the right, the center cell is alive and has exactly two neighbors.
04:29 and this is the final state of the second frame. Calculating frame three would be done in the same way, but by using frame two as the current state. In the next lesson, I’ll be talking about how to store this data for your program.
04:49 One of the things mathematicians find so fascinating about cellular automata is how a few simple rules can create quite complex behavior. Conway’s game of life is popular partially because the rules are so simple. Using the same four rules, but different starting patterns produces drastically different outcomes.
05:08 The example I used to show you how the rules work is known as the blinker pattern. It’s part of a family of patterns known as oscillators, named that way because they loop endlessly through a series of frames.
05:40 These are known as still lifes. I already showed you the blinker, but there are other types of oscillators as well. One goes through 15 different frames before repeating and is known as Penta decathlon.
06:40 The whole point of visualizing multiple frames is to animate it, so you’ll need a way of displaying the next frame subsequently over the current one. Having initial patterns as part of the code is limiting, so let’s use a data file so the user can insert their own patterns.
06:57 The program is going to have some patterns that the user might want to muck with. At bare minimum, you’re going to need the name of the initial pattern to use, but you might also want to change the size of the grid, how many frames to show, how fast the frames go and other things.
For most programs in your terminal, you run them directly. For Python code, you usually have to type
python then the name of the script. There is a way to make your script more like a direct program though, so let’s make it runnable.
There’s a nested
rp_life/ directory inside my
rp_life/ directory. As annoying as I find this, it’s common practice. The first level is the name of the package that gets installed, while the second level is the name of the Python module, and in our case also the name of the runnable script hook.
This module also contains at data file,
patterns.toml. This has some predefined patterns the user can get started with. You can create all these files in your IDE right now, leaving them empty if you like, or you can code the pieces as you go along.
09:03 You can see the desired state at the end of each lesson. As the course progresses, you’re probably itching to write some code, but before getting there, I’m going to spend some time thinking about how to represent the data first.
Become a Member to join the conversation.