Sub topic 4
A Reference Case: Conway's Game of Life
This chapter assumes you know loops, functions, sets, and dictionaries. It also assumes a basic game loop: a program stores a state, updates it during a tick, and then displays the new state.
Conway's Game of Life is a cellular automaton, a system made from many small cells that update according to the same local rules. Each cell is either alive or dead. The cells are arranged on a grid, and each cell checks its eight neighboring positions.
The rules are simple:
- A live cell with two or three live neighbors stays alive.
- A live cell with fewer than two live neighbors dies from isolation.
- A live cell with more than three live neighbors dies from overcrowding.
- A dead cell with exactly three live neighbors becomes alive.
The rules do not mention movement, creatures, or machines. However, particular starting patterns can move, repeat, grow, or disappear. These larger patterns emerge from many local updates.
Updating all cells at once
A generation is one complete update of the grid. A common mistake is to update cells directly while inspecting them. That allows an early change to affect a later cell in the same generation. The result depends on iteration order rather than on the defined rules.
Instead, calculate the next state from the old state:
- Read the current set of live cells.
- Count the neighbors around every live cell.
- Decide which cells will be alive in the next generation.
- Replace the old state with the new state.
The following implementation stores only live cells. A set is useful because each coordinate appears at most once.
from collections import Counter
def life_step(live):
"""Return the next generation of Conway's Game of Life."""
counts = Counter(
(x + dx, y + dy)
for x, y in live
for dx in (-1, 0, 1)
for dy in (-1, 0, 1)
if (dx, dy) != (0, 0)
)
return {
cell
for cell, neighbors in counts.items()
if neighbors == 3 or (neighbors == 2 and cell in live)
}
glider = {
(1, 0),
(2, 1),
(0, 2),
(1, 2),
(2, 2),
}
state = glider
for generation in range(4):
state = life_step(state)
print(sorted(state))counts contains the number of live neighbors for every cell that might change. Notice that a live cell with zero neighbors is absent from counts, so it correctly dies. A live cell with two neighbors remains alive because it is already in live. A dead cell with three neighbors appears in the result.
The glider is a famous pattern. After several generations, it has the same five-cell shape but has shifted across the grid. No rule says “move diagonally.” Movement is a consequence of the timing and position of the local births and deaths.
Why local rules create global patterns
Each cell knows only nearby information. It does not know the shape of the whole grid or the location of the glider. Yet cells share one state space, so one cell's change affects its neighbors during the next generation. Those neighbors then affect their neighbors.
This creates a chain of interactions:
The same process can produce different results from small changes in the starting state. A pattern may become stable, oscillate between shapes, expand into a moving structure, or vanish. The program remains predictable in the narrow sense that the same input always gives the same output. The long-term pattern can still be difficult to predict by inspection.
Applying the model to game design
Game of Life is not a complete game design, but it provides a useful reference model:
| Game of Life feature | Design lesson |
|---|---|
| One rule applies to every cell | General rules can replace many scripted cases |
| Cells inspect nearby neighbors | Local interaction can create wider effects |
| The next state is shared by all cells | Timing and state boundaries affect outcomes |
| Patterns emerge from starting layouts | Setup can matter as much as individual actions |
When designing a systemic game, ask whether an interesting outcome comes from a special instruction or from ordinary rules interacting. A scripted glider would need a rule such as “move this object diagonally.” Game of Life needs only neighbor counts and simultaneous updates.
Exercise
Why does the set-comprehension implementation include a live cell when its neighbor count is exactly two?
Show answer
The cell survives with two neighbors, so the condition checks both its neighbor count and whether it is already in the current live set.