Conway's Game of Life
Conway’s Game of Life is a so called zero-player game designed by the British mathematician John Horton Conway in 1970. It only has a initial configuration which can be set by the player and then evolves without any further interactions with the game.
The rules are rather simple. Here is an excerpt from wikipedia.org:
The universe of the Game of Life is an infinite, two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead, (or populated and unpopulated, respectively). Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
- Any live cell with fewer than two live neighbors dies, as if by underpopulation.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by overpopulation.
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed; births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick. Each generation is a pure function of the preceding one. The rules continue to be applied repeatedly to create further generations.
In this post we will program the game in and look at a few results.
In principal, the grid should be infinite. Due to the limitations of our computers, this is not realizable, so we limit ourselves to finite dimensions. For our experiments, we choose rather small values for the number of cells on the orthogonal grid.
First, we need a function which initializes the grid. Each cell has a chance of 50% to be alive:
In the next step, it can be useful, to have a function which counts the live neighbors for each cell of the grid. What we have to do is, put a 3x3 box around each cell (on the edges the boxes are smaller) and sum up all cells in this 3x3 box which are alive (excluding the cell in the center, which is our target cell for which we count the neighbors):
Now, we are ready to write a function which performs the transition from generation to the next. If we look carefully at the above rules for a transition, we can identify a simple instruction which specifies which cells will be alive in the next generation:
- All cells with exactly 3 neighbors or
- All live cells with exactly 2 live neighbors.
All other cells will be dead in the next generation. These two simple rules make it very easy to write a function which evolves the grid:
And that’s basically it. We just need some additional functions which evolve the grid-world over many time steps and which plot the world after each transition. The whole code can be found on Github and is also listed in the appendix.
Let us take a look at how a randomly initialized 100x100 grid evolves over time:
Depending on the initialization of the grid-world, many interesting patterns can be created. Many different patterns are discussed on wikipedia.org.
Usually, the population of live cells never dies out completely and converges to a certain number. The image below shows exemplarily for our 100x100 grid how the population develops over time for 20 different initial worlds. In the very beginning, around 50% of all cells are alive, but quickly die away in all runs. After 1000 iterations around 350 alive cells are left and then appears to stagnate from then on.
A2: Further Examples