Skip to content

The Game of Life

A grid space has one key for each cell: "row,column". The axis neighbors(8) has the eight cells around the focus as targets. The option where keeps the live neighbors, and count() counts them.

The rule of the Game of Life is one Vex expression. A cell is alive in the next generation if it has 3 live neighbors. A live cell with 2 live neighbors also stays alive. Each generation evaluates the rule at each cell with all().

The Game of Lifegeneration 0, 8 live cells
const g = vex(NumDomain, BoolDomain).over(space.grid(cells));
const live = g.start(0)
  .neighbors(8, (n) => n.from("alive"), { where: (n) => n.from("alive") })
  .count();
const next = live._.eq(3)._.or(g.from("alive")._.and(live._.eq(2)));
next.all();   // the next state of each cell

A Vex program is a function of one position in a space: it reads its own cell and the cells around it. Each cell runs the same program. In mathematics, this is the “extend” operation of a comonad, and cellular automata are the standard example. Refer to the comonad.

Select a cell to change it. Then select “Start” or “One generation”.