Game of Life

Conway’s Game of Life is a nice demonstration of how simple rules can generate complex structures. It’s also a great introduction to the fascinating world of Cellular Automata.

see also: Game of life (revisited)

The Game of Life, or simply ‘Life’, takes place on an infinite 2-dimensional grid, populated by cells. In every next generation, the new state of each cell depends on the current state of the eight surrounding cells (its ‘Moore neighborhood’), following this simple set of rules:

  1. Living cells die if they have less than 2, or more than 3 living neighbors.
  2. Dead cells are brought to life if they have exactly 3 living neighbors.

The initial state of the system completely determines the further process, in which order and chaos can easily get involved in a continuous arm wrestling game.

Mathematicians and Life enthusiasts have found some incredible patterns like the Gosper Glider Gun, the Puffer Train and the Breeder. This video starts on a grid with ~25% of the cells ‘alive’ (randomly chosen).

The main challenge of running a simulation of ‘Life’ on an Arduino Uno lies in its limited RAM memory size (2K only!). Since the rules are applied to all cells simultaneously, we need to store both the old and the new state of the grid.

First I used Booleans to store cell states, but since every boolean claims a full byte (8 bits), the largest grid that Uno could handle was about 17×17 in size. The trick was to use bytes instead, where each byte now holds the state of eight consecutive LEDs in one of the rows, thus replacing eight boolean bytes!

With this new approach, Arduino Uno can simulate a 64×32 cell grid on my 128×64 pixels monochrome I2C Oled display (using 89% of Uno’s precious RAM). In that configuration, cells are represented by 2×2 LED squares, lit when alive and unlit when dead.

Note that you can define the initial state of the grid within the seed() function of the sketch. I used a simple border rule for the video: neighbor cells outside the grid are considered dead.