Algorithms · Visualized
Matrix: Game of Life
Matrix & 2D
Rotations, traversals, and grid searches on 2D arrays.
A zero-player cellular automaton: every cell is alive (1) or dead (0), and one generation is replaced by the next using only each cell’s eight neighbours. Time: O(rows × cols) average, O(rows × cols) — eight neighbour reads per cell worst; space: O(rows × cols) for the next-generation copy; O(1) extra if you encode both states in each cell.
Big-O: Classic O(m·n) sweeps and in-place tricks.
Matrix: Game of Life
Matrix / 2D ArrayA zero-player cellular automaton: every cell is alive (1) or dead (0), and one generation is replaced by the next using only each cell’s eight neighbours. Live cells with two or three live neighbours survive, live cells with fewer than two die of loneliness, live cells with more than three die of overcrowding, and a dead cell with exactly three live neighbours is born. The subtlety is that all cells must be judged against the same previous generation, so the algorithm reads the old grid and writes a separate next grid — updating in place would let already-modified cells pollute later neighbour counts.
How Game of Life Works
- Copy the grid so the generation you read from and the one you write to never mix.
- For every cell (i, j), count the live cells among its up-to-eight neighbours, skipping positions outside the 4×3 grid.
- Alive with 2 or 3 live neighbours → stays alive; alive with 0, 1 or 4+ → becomes dead.
- Dead with exactly 3 live neighbours → becomes alive; any other dead cell stays dead.
- Write the verdict into the copy, never into the grid being read.
- Advance the generation counter and show the copy — that is one complete step of the animation.
Game of Life Worked Example
Input: {matrix: [[0, 1, 0], [0, 0, 1], [1, 1, 1], [0, 0, 0]]}
The visualizer’s default grid is four rows of three: row 0 = 0 1 0, row 1 = 0 0 1, row 2 = 1 1 1, row 3 = 0 0 0 — five live cells. Cell (0,1) has one live neighbour and dies; (0,2) has two and survives; (1,0) has three and is born; (1,1) sees five and stays dead; (1,2) has three and survives; (2,0) has one and dies; (2,1) has three and survives; (2,2) has two and survives; (3,1) has three and is born. The next generation reads 0 0 1 / 1 0 1 / 0 1 1 / 0 1 0 — six live cells, the shape having crept one row downward and one column right, exactly like the glider it is.
def game_of_life(board):
rows, cols = len(board), len(board[0])
for r in range(rows):
for c in range(cols):
live = 0
for dr in (-1, 0, 1):
for dc in (-1, 0, 1):
if dr == 0 and dc == 0:
continue
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols and abs(board[nr][nc]) == 1:
live += 1
if board[r][c] == 1 and (live < 2 or live > 3):
board[r][c] = -1 # was alive, dies
if board[r][c] == 0 and live == 3:
board[r][c] = 2 # was dead, lives
for r in range(rows):
for c in range(cols):
board[r][c] = 1 if board[r][c] > 0 else 0
return boardBehind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Game of Life Time & Space Complexity
When to Use Game of Life
- Teaching simultaneous-update semantics — the bug of updating in place is a classic interview trap.
- Neighbour-counting grids in general: minesweeper, image smoothing, and convolution kernels use the same skeleton.
- Simulation exercises where a cheap local rule produces surprisingly complex global behaviour.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.