โ† Back to Index

๐Ÿงฌ Game of Life

Simulation ยท Conway's CA
Click or drag to toggle cells

How It Works

Conway's Game of Life is a cellular automaton โ€” a grid of cells that evolve according to simple rules. Each cell is alive or dead; each generation, all cells update simultaneously based on their 8 neighbors.

The Four Rules

Key Implementation

function step(grid, rows, cols) {
  const next = Array.from({length: rows}, () => new Uint8Array(cols));
  for (let r = 0; r < rows; r++) {
    for (let c = 0; c < cols; c++) {
      let n = 0;
      for (let dr = -1; dr <= 1; dr++)
        for (let dc = -1; dc <= 1; dc++) {
          if (dr === 0 && dc === 0) continue;
          const nr = (r + dr + rows) % rows;  // wrap-around
          const nc = (c + dc + cols) % cols;
          n += grid[nr][nc];
        }
      // apply rules
      next[r][c] = (grid[r][c] && (n === 2 || n === 3)) || (!grid[r][c] && n === 3) ? 1 : 0;
    }
  }
  return next;
}

Toroidal topology: The grid wraps around โ€” left edge connects to right, top connects to bottom. This avoids edge-case handling and creates an "infinite" universe feel.

Famous Patterns