Golang Project Structure

Tips and tricks for writing and structuring Go code

Conway’s Game of Life

The Game of Life — which can be played below — was first created in 1970 by British mathematician John Conway, who was working at the University of Cambridge (before he was later appointed to the John von Neumann Chair of Mathematics at Princeton University). It is a zero-player game, which means that it naturally evolves from an initial state and requires no more input.

It is interesting, however, to see how different initial states evolve and which sort of patterns regularly tend to emerge.

The gameboard consists of a grid of square cells: each one can be either alive or dead. The evolutionary lifecycle of each cell over the course of the game depends on the state of its neighbouring cells.

If a cell is currently alive, it can be killed by overpopulation if more than three of its neighbouring cells are also alive; likewise, it can be killed by underpopulation if fewer than two of its neighbouring cells are alive. If a cell is currently dead, it can  come back to life by reproduction if exactly three of its neighbouring cells are alive.

The three simple rules listed above (i.e. overpopulation, underpopulation and reproduction) are enough to create all of the complex interactions that you will see in the game.

In order to make patterns easier to spot, I have introduced a colour-coding system. Cells that are currently alive and were also alive in the previous frame are painted black. Cells that have recently come to life (i.e. they are currently alive and were dead in the previous frame) are painted dark red. Cells that have recently died (i.e. they are currently alive and were dead in the previous frame) are painted light violet. Cells that are currently dead are were also dead in the previous frame are simply white.

Now let’s play the Game of Life: