JavaScript Game of Life

I thought it'd be interesting to code a JavaScript implementation of John Conway's Game of Life cellular automaton. The game of life is comprised of a set of cells (the world), where each cell can be either in the "on" or "off" state (usually represented by black or white pixels, respectively).

The game is split into discrete time steps. At each step, all of the cells in the world are evaluated to calculate their liveness in the next step. Those that are live will change to the "on" state, whilst those who have died are changed to the "off" state.

The liveness of a cell is determined by its state, and the state of its 9 neighbours, using a simple set of rules. The number of live neighbours is counted, and the cell's state is calculated thus:

  1. A live cell with < 2 dies.
  2. A live cell with 2 or 3 lives.
  3. A live cell with > 3 dies.
  4. A dead cell with 3 lives.

I've uploaded my implementation to Github, here. A demo is shown below. The world is initialised to contain 2 Gosper glider guns, which are designed such that they emit a certain pattern of cells with a fixed period.

The start/stop button pauses the timesteps, whose length is determined by the slider control. The clear button clears the world, whilst the randomise button adds a random set of "on" cells to the world. Individual cells can be switched on/off by clicking the left and right mouse buttons, respectively (pause the game first, else the new cells will immediately die). I chose to implement my world using a toroidal array, so both sides of the world "wrap around". This means gliders can pass from one side to the other, rather than simply dying at the edges.