Game of life
Created: 2021-04-05. Modified: 2021-05-23.
#
IntroductionIn this article, we will implement the game of life:
From an implementation point of view, the game of life is simply a grid of square cells where the colours of the cells are controlled by a simulation system. This gives us a 2-step plan for the implementation.
- Draw a 2-dimensional grid of squares
- Implement the model that simulates the cell dynamics
#
Step 1 - Draw a 2-dimensional grid of squaresWe will use the following configurations for the application
rules = basic_rules(), deparsers = default_2_deparsers())
.
#
i. Load librariesFirst, we load the p5
library and import some R functions from
sketch
#
ii. Set up relevant variablesNext, we set up some variables for the canvas and the grid.
- We need the width and the height for the canvas,
cw
andch
. - For the grid, we need the
grid_size
, from which we can work out the number of columns and rows of the grid,ncol
andnrow
. - The grid is represented as a matrix of 0s and 1s representing the black and white colours respectively.
#
iii. Draw the gridNow, we will draw a grid using nested for-loops. The xy-coordinates of
the cell is given by the (i,j)-position of the cell multiplied by the
grid size. Note that the grid entry is accessed using grid[i][j]
rather than grid[i, j]
. This is one thing to watch out for when the
basic_rules()
configuration is used.
#
Step 2 - Implement the model that simulates the cell dynamicsThe cell evolution follows the following rules (from Wikipedia):
At each step in time, the following transitions occur:
- Any live cell with fewer than two live neighbours dies, as if by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
These rules, which compare the behavior of the automaton to real life, can be condensed into the following:
- Any live cell with two or three live neighbours survives.
- Any dead cell with three live neighbours becomes a live cell.
- All other live cells die in the next generation. Similarly, all other dead cells stay dead.
To implement these rules, we need a function countNeighbours
to count
the neighbours of a cell and use the count to determine if the cell
should be dead or alive (i.e. equal to 0 or 1).
countNeighbours
function#
The Notes
- The count (
total_sum
) starts with the negative of the centre entry to avoid handling an exception during the loop. - We allow wrapping around the edges, so the top edge is the neighbour of the bottom edge (and similarly the left / right edges are neighbours). This is implemented by taking modulus with the number of columns / rows.
#
Change grid entry state following the evolution rules#
Extra - add a button to reset the gridGiven the simulation ends in a fairly short time, it would be convenient
to have a button to restart the simulation. We use the createButton
function from p5 to create a button, then position it and make it
regenerate the grid upon click using the position
and mousePressed
methods of the button.
#
Credits / ReferenceI learned the above visualisations from Daniel
Shiffman. Check out the
coding train for many more fun examples
with p5.js
.