An implementation of Conway's Game of Life, a classical cellular automaton,
using the morph
function. The gosperGliderGun
function provides an interesting starting configuration.
gameOfLife(init, size, density = 0.3, steps = 200, viz = FALSE,
tick = 0.5)gosperGliderGun()
A binary matrix representing the final state of the system after
steps
generations.
The initial state of the automaton, a binary matrix. If missing,
the initial state will be randomly generated, with a population density
given by density
.
The dimensions of the board. Defaults to the size of
init
, but must be given if that parameter is missing. If both are
specified and size
is larger than the dimensions of init
,
then the latter will be padded with zeroes.
The approximate population density of the starting state.
Ignored if init
is provided.
The number of generations of the automaton to simulate.
If TRUE
, the state of the system at each generation is
plotted.
The amount of time, in seconds, to pause before plotting each
successive generation. Ignored if viz
is FALSE
.
Jon Clayden <code@clayden.org>
Conway's Game of Life is a simple cellular automaton, based on a 2D matrix of ``cells''. It shows complex behaviour based on four simple rules. These are:
Any live cell with fewer than two live neighbours dies, as if caused by under-population.
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 overcrowding.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
Live and dead cells are represented by 1s and 0s in the matrix, respectively.
The initial state and the rules above completely determine the behaviour of the system. The Gosper glider gun is an interesting starting configuration that generates so-called ``gliders'', which propagate across the board.
In principle the size of the board in a cellular automaton is infinite. Of course this is not easy to simulate, but this implementation adds a border of two extra cells around the board on all sides to approximate an infinite board slightly better. These are not visualised, nor returned in the final state.
The morph
function, which powers this simulation.
if (FALSE) gameOfLife(init=gosperGliderGun(), size=c(40,40), steps=50, viz=TRUE)
Run the code above in your browser using DataLab