Sub topic 4
A reference case: Conway's Game of Life
Conway's Game of Life is the smallest well-known system where simple local rules produce large-scale behavior that no rule mentions. This chapter assumes you can read Python, including sets, comprehensions and Counter. It also assumes you know the idea of a game loop that updates state in discrete ticks. We use Life as a reference case: later chapters on tag systems, feedback loops and tuning rely on the vocabulary introduced here.
The setup
Life is a cellular automaton. A cellular automaton is a grid of cells, where each cell holds one of a few states and all cells update together according to a rule that looks only at the cell and its neighbors.
In Life:
- Each cell is either alive or dead.
- A cell's neighborhood is the eight cells touching it, including diagonals. This is called the Moore neighborhood.
- Time moves in discrete steps called generations.
- All cells update at the same moment, based on the previous generation.
There are two rules:
- A dead cell with exactly 3 live neighbors becomes alive (birth).
- A live cell with 2 or 3 live neighbors stays alive (survival). Any other live cell dies, from loneliness with 0 or 1 neighbors or from overcrowding with 4 or more.
Compact notation writes this as B3/S23: born with 3, survive with 2 or 3.
Notice what the diagram contains: no direction, no speed, no object, no shape. Every cell uses the same rule and looks only at its eight neighbors.
An implementation that stores only live cells
Here is the implementation from the chapter plan. Instead of a fixed 2D array, it stores a set of (x, y) coordinates for live cells.
from collections import Counter
def life_step(live):
"""Advance Conway's Game of Life one generation.
live is a set of (x, y) cells that are alive."""
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, n in counts.items()
if n == 3 or (n == 2 and cell in live)
}
glider = {(1, 0), (2, 1), (0, 2), (1, 2), (2, 2)}
state = glider
for _ in range(4):
state = life_step(state)
print(sorted(state)) # the same glider shape, shifted by (1, 1)The code works in two phases.
- Count. Each live cell adds 1 to the counter of each of its eight neighbors. When this phase ends,
counts[cell]is the number of live neighbors ofcell, for every cell with at least one. - Decide. A cell is alive in the next generation if its count is 3, or if its count is 2 and it is already alive. That one condition covers both birth and survival.
Three properties of this design matter beyond Life.
- Synchronous update.
life_stepreadsliveand builds a new set. It never modifieslivewhile counting. If you updated cells in place, a cell processed late would see neighbors that had already changed, and results would depend on iteration order. The fire example in the chapter plan makes the same choice by copying the grid (new = [row[:] for row in grid]) before writing. - Unbounded world. A set has no edges, so a glider can travel forever. A fixed array forces you to choose what happens at the border: wrap around (a torus), treat outside cells as dead, or clamp. Each choice changes which patterns survive, so a border rule is a design decision rather than a detail.
- Cost scales with population. Work is proportional to the number of live cells times 8, not to the world area. An empty world costs nothing.
A live cell with no live neighbors never appears in counts, so it is absent from the result and dies. That matches the rule, and it is a good example of an edge case the code handles without special code.
Looking at a generation
Printing coordinates is hard to read. This helper draws a window of the world.
def render(live, width, height):
return "\n".join(
"".join("#" if (x, y) in live else "." for x in range(width))
for y in range(height)
)
blinker = {(1, 0), (1, 1), (1, 2)}
print(render(blinker, 3, 3))
print()
print(render(life_step(blinker), 3, 3))This prints a vertical bar of three cells and then a horizontal bar. Apply life_step again and you get the vertical bar back. The blinker is an oscillator with period 2.
A small catalog of what the rules produce
Run Life long enough from a handful of starting shapes and the same kinds of behavior keep showing up. People have given them names.
| Pattern | Cells | Behavior | Why the rules produce it |
|---|---|---|---|
| Block | 4 (2x2 square) | Never changes (still life) | Each cell has exactly 3 live neighbors, so all survive. No outside cell has 3 live neighbors, so none are born. |
| Blinker | 3 in a row | Flips between horizontal and vertical (oscillator, period 2) | The two end cells die with 1 neighbor. Two cells beside the middle are born with 3. The middle survives with 2. |
| Glider | 5 | Repeats its shape every 4 generations, shifted one cell diagonally (spaceship) | Births on its leading edge and deaths on its trailing edge move the shape. |
| R-pentomino | 5 | Churns chaotically, then settles after 1103 generations | Reported to leave behind 116 cells, including several gliders. |
The glider is the instructive case. Look at the program again. No variable holds a velocity, and no line says "move." The program only counts neighbors and applies B3/S23. Yet the pattern is shifted by (1, 1) after 4 generations. The movement exists only at the level of the pattern. You can say "the glider moves" and be correct, but nothing in the rules is responsible for it except the sum of local births and deaths.
This is the working definition of emergence from the first chapter, in concrete form: behavior that no single rule describes, appearing when simple rules act on shared state. The shared state here is the grid. Each cell's next state depends on cells that other cells' rules are also reading.
Why local rules give global structure
Three features of Life explain why it works as a reference case.
- Strictly local rules. A cell never sees beyond its neighbors. Information travels at most one cell per generation, so the grid has a built-in "speed of light." A glider's speed of one cell per 4 generations (c/4) sits well below it.
- Uniform rules. Every cell obeys the same rule. This is why patterns are portable. A glider behaves identically anywhere, so players can name and reuse it.
- Nonlinear thresholds. The numbers 2 and 3 are narrow windows. A cell with 2 neighbors is stable, a cell with 3 is productive, and everything else is destructive. The balance between growth and decay is what keeps Life from either filling the grid or emptying it. Change B3/S23 to B3/S1234 and most starting patterns grow without bound. Change it to B3/S3 and most patterns die quickly. Interesting behavior lives in a narrow band of rule parameters, which is the central concern of the tuning chapter.
Behavior at the pattern level can also be combined. Gliders can collide with blocks, and certain collisions delete them, rebuild them or turn them into other patterns. Researchers have used such collisions to build logic gates, memory and in the end full computers inside Life. Life is Turing complete, which means that in principle it can compute anything a general-purpose computer can.
Predictability and its limit
You might expect that because the rules are fully known and deterministic, you can always predict what a starting pattern will do. For many patterns you can. For general starting patterns you cannot do much better than running the simulation.
The R-pentomino shows this. It has 5 cells, and nobody can read the fate of its 1103-generation evolution off the rule. You run it and see. More formally, because Life can simulate a computer, questions such as "will this pattern ever die out?" are undecidable in general. No algorithm answers them for every starting pattern.
Two distinct things are true at once:
- The system is deterministic. The same start always gives the same result.
- The system is not shortcut-predictable. There is no formula that gives generation 1,000,000 faster than computing it.
For designers, this means you cannot fully reason your way to knowing what players will do with a rule set. You can reason about local effects, such as "this rule makes fire spread to grass," but not always about global outcomes. The practical answer is to simulate, observe and adjust, which later chapters on playtesting and tuning cover.
Common mistakes when implementing and studying Life
- Updating in place. Writing into the same grid you are reading from makes results depend on scan order. Always build the next generation separately.
- Counting the cell itself. Forgetting
(dx, dy) != (0, 0)adds the cell to its own count and shifts every threshold by one for live cells. - Wrong border choice. On a small fixed grid with dead borders, a glider crashes into the edge and becomes a block. On a wrapping grid, it circles forever. Both are valid, but they are different games.
- Reading the pattern's name into the rules. Saying "the glider decides to turn" is a convenient description, but only the cell rules cause events. When a pattern behaves unexpectedly, debug at the cell level.
- Expecting smooth tuning. In a system with sharp thresholds, one rule change can move behavior from dead to explosive. Change one parameter at a time.
What to carry into game design
Life is not a game that players play in the usual sense, but its structure appears in many designs. A fire spreading across flammable tiles uses a local neighbor rule, shared grid state, synchronous update and a threshold (flammable or not). A strategy game's economy uses the same pattern of simple per-entity rules acting on shared state. The next chapters take these elements apart: how to combine rules so they interact cleanly, how to build tag-based systems, and how feedback loops decide whether a match stays close or snowballs.
Exercise
In the life_step implementation, suppose you change the final condition to n == 3 or (n == 2 and cell in live) or (n == 4 and cell in live). Name the rule in B/S notation and say which of the original Life behaviors you would expect to change. Then explain why the block pattern is unaffected.
Show answer
The new rule is B3/S234: birth with 3 neighbors, survival with 2, 3 or 4. Patterns that relied on overcrowding to stay bounded would change, because live cells with 4 neighbors no longer die, so many patterns grow more and become more stable or dense. The block is unaffected because each of its four cells has exactly 3 live neighbors, so it survives under both rules. No cell outside it has more than 2 live neighbors, so there are no births.
Exercise
A teammate writes a Life step that loops over the grid and writes each cell's new state directly into the same grid. The glider test fails, and the result changes if the loop scans rows in reverse order. What is the bug, and what is the fix?
Show answer
The update is in place, so later cells see neighbors that have already moved to the next generation, which mixes two generations and makes the outcome depend on scan order. The fix is synchronous update: read only from the old state and write into a separate new state (a copied grid or a new set), then replace the old state with the new one.
Exercise
Explain in two or three sentences why it is accurate to say that a glider "moves" even though the code has no movement rule. What does this show about where emergent behavior lives?
Show answer
The glider's shape reappears one cell diagonally away after 4 generations because births on its leading edge and deaths on its trailing edge shift the live cells. Movement is a property of the pattern, which an observer identifies across generations, not of any single cell or rule. Emergent behavior lives in the interaction of rules acting on shared state, and it can be described only at a higher level than the rules themselves.