Playground
Conway's Game of Life

Explanation

The game proceeds step by step. First, an initial generation of living cells is defined on the playing field. From the current generation (the overall state of the playing field), the next generation is determined. The state of each individual cell in the next generation is derived from simple rules based on its current state and the current states of its eight neighboring cells (Moore neighborhood). Wikipedia

Lenia Lenia is a continuous version of this game: cell values are smooth between 0 and 1, the neighborhood is a soft ring, and the rule is a smooth curve. Gliders and pulsing creatures appear. Try it on the Lenia page: Lenia

Original work

How it works

The board is two grids of cells with an alive flag: a read grid and a write grid (double buffering). Each generation reads only from one and writes only to the other, then both are swapped. So a cell that already changed does not affect its neighbors in the same generation.

  • For each cell the eight neighbors are counted (Moore neighborhood). The border is cut off, not wrapped: border cells have fewer neighbors, the board is not a torus.
  • Time per generation is O(rows · columns · 8). At 200 × 200 that is 40,000 cells and 320,000 neighbor checks.
  • The loop is an async loop: compute, draw, then wait until the configured time per generation has passed. If no cell changes any more, the game stops by itself.
  • The whole grid is drawn onto a canvas: one fill operation for the background plus one per living cell.

Source code:conway-gol.service.ts

Note According to Conway’s original rule, a cell is alive in the next round if exactly three cells are alive in its 3x3 neighborhood beforehand, counting itself only if necessary, that is:

  • A living cell remains alive in the next generation if it has either two or three living neighbors.
  • A dead cell is “born” (is alive in the next generation) if it has exactly three living neighbors.
  • A living cell “dies” (is dead in the next generation) if it has fewer than two (underpopulation) or more than three (overpopulation) living neighbors.
  • A dead cell remains dead if it does not have exactly three living neighbors.
Generation: 0Generations/s: 0Compute time per generation: 0 ms
Alive Empty