The Game of Life

The Game of Life

November 20, 2025

In this article, we implement Conway’s Game of Life, see [1] for more information. This is more of an exercise for myself to determine how to insert and play around with JavaScript directly within my Hugo setup.

Background #

For context, and for those of you unaware, Conway’s famous Game of Life, is a simple game that aims to emulate in an extremely basic sense, life. More specifically, with a few very simple rules underpinning the game, you can create an abundance of complexity, including a general purpose computer, and even the Game of Life itself, see here, or here for the Game of Life inside the Game of Life, inside the Game of Life! In more specific terms, it is a simple game that produces emergent behaviour.

So, what are these simple rules?

  1. Underpopulation: Any live cell with fewer than two live neighbours dies.
  2. Survival: Any live cell with two or three live neighbours lives on to the next generation.
  3. Overpopulation: Any live cell with more than three live neighbours dies.
  4. Reproduction: Any dead cell with exactly three live neighbours becomes a live cell.
1. Underpopulation (< 2 neighbours → dies)
2. Survival (2 or 3 neighbours → stays alive)
3. Overpopulation (> 3 neighbours → dies)
4. Reproduction (dead with 3 neighbours → becomes alive)

Pseudocode #

We write a simple script for the Game of Life logic. In particular, we define a function step, which at each step iterates through all the cells in the domain, and based on the previous state, defines the status of the cells at the current step.

# Assumes:
#  - cols, rows: grid dimensions
#  - grid: current Uint8Array of size cols*rows (1 = alive, 0 = dead)
#  - next: scratch Uint8Array same size
#  - idx(x,y): returns linear index, handling wrap-around (toroidal) indexing

function step():
  for y from 0 to rows - 1:
    for x from 0 to cols - 1:
      # count alive neighbours (8 surrounding cells)
      neighbours = 0
      neighbours += grid[idx(x-1, y-1)]
      neighbours += grid[idx(x,   y-1)]
      neighbours += grid[idx(x+1, y-1)]
      neighbours += grid[idx(x-1, y  )]
      neighbours += grid[idx(x+1, y  )]
      neighbours += grid[idx(x-1, y+1)]
      neighbours += grid[idx(x,   y+1)]
      neighbours += grid[idx(x+1, y+1)]

      i = idx(x, y)                      # current cell index

      # apply Conway's rules:
      #  - live cell with 2 or 3 neighbours → lives
      #  - live cell with <2 or >3 neighbours → dies
      #  - dead cell with exactly 3 neighbours → becomes alive
      if grid[i] == 1:
        if neighbours == 2 or neighbours == 3:
          next[i] = 1
        else:
          next[i] = 0
      else:
        if neighbours == 3:
          next[i] = 1
        else:
          next[i] = 0

  # swap buffers so "next" becomes the current state
  swap grid and next

Implementation #

Tada!

Generation: 0
Population: 0
Zoom: 10

References

  1. Wikipedia contributors (2025). Conway's Game of Life - Wikipedia, The Free Encyclopedia.