Conway's Game of Life
A concurrent implementation in Elixir
The game of life is the best-known two-dimensional cellular automaton, invented by John H. Conway and popularised in Martin Gardner's Scientific American column starting in October 1970.
The life cellular automaton is run by placing a number of filled cells on a two-dimensional grid. Each generation then switches cells on or off depending on the state of the cells that surround them. The rules are defined as follows. All eight of the cells surrounding the current one are checked to see if they are on or not. The cells that are on are counted and this count is then used to determine what will happen to the current cell.
Death: if the count is less than 2 or greater than 3, the current cell is switched off.
Survival: if the count is exactly 2, or the count is exactly 3 and the current cell is on, the current cell is left unchanged.
Birth: if the current cell is off and the count is exactly 3, the current cell is switched on.
2. The OTP architecture
The following drawing describe the architecture of the Elixir app that contains the game:
Each cell is represented by a
GenServer process and they are all controlled by the
Cell.Supervisor with a strategy simple_one_for_one which informs the system that cells will be added and removed dynamically.
Universe.Supervisor controls the
Universe together with the
Cell.Supervisor and the
Registry. The Universe describes the overall state of the game and can be ticked in order to advance to the next state. The Registry is a key-value storage structure and provides a comfortable way to link each Cell PID with a key which is its postion in the form
3. The bigger picture
In order to provide a front-end for the game, a Phoenix app
interface has been created and added together with the
game Elixir app under the
game_of_life Elixir umbrella app. This allows the two apps to live independently but at the same time to easily communicate. The communication between the two apps is provided by the Phoenix Channels based on websockets.