Game of Life Rules and Demo — B3/S23, Steps, and Periodic Boundaries

12 min read
SimulationAlgorithmCanvas APITypeScript

Open the Game of Life demo to toggle cells and follow their changes one generation at a time. This cellular automaton uses B3/S23: the current state of the target cell and the number of living cells among its eight neighbors determine the next state. Start with a small pattern and use Step to compare the rule with the board.

Start with Clear, three cells, and Step

  1. Press Clear. Automatic playback stops and the counter returns to Gen: 0.
  2. Click or tap three horizontally adjacent cells near the center. Each press toggles one cell between alive and dead. Leave the surrounding cells empty.
  3. Press Step once. The horizontal line becomes a vertical line. Press it again to return to the horizontal line. This two-generation oscillator is called a blinker.
  4. Use Play to continue and Pause to stop. Pause before pressing Step when you want to inspect a single generation.

You can toggle a cell while playback is running. A pointer press changes the cell at that position; dragging does not continuously paint cells. Toggling a cell alone does not advance the generation counter.

Button Behavior in this demo
Play / Pause Starts or stops automatic generation updates, preserving the board and counter
Step Advances one generation and adds one to the counter. It also works during playback, so Pause first to isolate one update
Random Gives each cell a 0.3 probability of being alive and resets the counter to zero. Keeps the current playback state
Clear Makes every cell dead, resets the counter to zero, and stops playback

The 0.3 in Random is a probability for each cell, not a requirement that exactly 30% of the board be alive. If you press it during playback, automatic updates continue from the new board. Pause first to inspect the initial pattern.

What does the B3/S23 rule count?

Count living cells in the eight positions around the target, including diagonals. Exclude the target itself. B3 means a dead cell is born with three living neighbors; S23 means a living cell survives with two or three. Stanford's Game of Life assignment explains these neighbors and rules.

Living neighbors Currently alive Currently dead
0 or 1 Dies Stays dead
2 Survives Stays dead
3 Survives Is born
4–8 Dies Stays dead

Every cell reads the same old board when computing a generation. Otherwise, cells updated earlier could affect neighbor counts later in that generation. This demo reads from the current array, cur, writes into a new array, next, and replaces the board after calculating every cell.

const next = new Uint8Array(cols * rows)
// Count each cell's neighbors from the old cur array
const alive = cur[y * cols + x]
next[y * cols + x] = neighbors === 3 || (alive === 1 && neighbors === 2) ? 1 : 0
// After calculating every cell: gridRef.current = next

This shortened excerpt expresses the update with an equivalent condition. It omits the neighbor-counting loop. The implementation allocates a new Uint8Array for each generation; it does not reuse and swap two existing arrays.

Cells at an edge are neighbors of the opposite edge

The demo uses periodic boundaries: the right edge connects to the left, and the top connects to the bottom. Neighbor coordinates use the remainder calculations below. Both dx and dy range over −1, 0, and 1; the combination for the target itself is skipped.

const nx = (x + dx + cols) % cols
const ny = (y + dy + rows) % rows
neighbors += cur[ny * cols + nx]

After Clear, choose a row near the middle and place cells at the leftmost column, the column beside it, and the rightmost column of the same row. Across the boundary, these form another horizontal blinker. Step turns it into three vertical cells centered on the leftmost column. Cells separated on screen can be neighbors in the calculation. The Wrapping section of Stanford's assignment explains this connection between opposite edges.

For comparison, press Clear and place a 2×2 block near the center with empty cells around it. Each living cell has three living neighbors, so the block keeps its shape after Step. Comparing it with the blinker shows how the same rule produces a still pattern or a periodic one.

How large is the board, and how is it drawn?

The number of rows and columns is dynamic. Cells are 8 CSS pixels wide; the demo divides the container width and height by eight and rounds down. Resizing the container rebuilds the board with the same initialization as Random. Keep the window size unchanged while comparing patterns.

The state is stored as zeros and ones in a one-dimensional Uint8Array, with coordinate (x, y) mapped to y * cols + x. MDN's Uint8Array reference documents unsigned 8-bit elements, one byte per element, and zero initialization when an array is created from a length.

Game of Life draws living cells with individual Canvas fillRect calls. SandFall uses ImageData, but that difference alone does not establish which is faster. FPS, garbage-collection overhead, and rendering speed differences have not been measured here.

How do WFC and SandFall differ?

All three demos use a grid and Canvas, but an update performs a different operation in each.

Demo What an update does What to try
Game of Life Computes the next generation from the whole old board Clear, place a pattern, then use Step
Wave Function Collapse Chooses one unresolved cell with fewest candidates, selects a tile, and narrows neighboring candidates Use Step while paused, then Play
SandFall Scans from lower rows upward, moving sand down or diagonally down within the same board Select a color, then press or drag. Use Clear to erase

Open the WFC Generator demo to switch between Pipes, Roads, and Terrain. If a cell loses all its candidates, the demo stops and displays Contradiction. Reset starts a new attempt. This is a different problem from updating living and dead cells in Life.

Open the SandFall demo to drop sand in five colors. It modifies the board in place while scanning from bottom to top, rather than using Life's synchronous update. Its cell values represent empty space and sand colors; there is no water selector.

Go deeper on this topic

Creative Coding

Mathematical simulations, nature visualization, and generative art in the browser

Article 1 of 13 in this series.

View topic page

Use the topic hub as a map, then continue to the next article in the series. New here? Start at the home hub →