Conway's Game of Life¶
An implementation of Conway's cellular automata.
Examples Source Code
The code for all the examples can be obtained by either:
Inputs¶
The only inputs are the grid size (2d) and the proportion of cells that are alive initially.
Implementation¶
This example uses the StateGrid
class for efficient neighbour counting, requiring only a few lines of code to evolve a generation (see below). The initial alive cells are sampled randomly according to the proportion set. In each new generation, alive cells with less than 2 or more than 3 neighbours die; empty cells with 3 live neighbours come alive. Dead cells are coloured black, live cells are coloured according to how long they have been alive, from white (youngest) to brown (oldest).
def step(self) -> None:
n = self.domain.count_neighbours(lambda x: x > 0)
deaths = np.logical_or(n < 2, n > 3)
births = n == 3
self.domain.state = self.domain.state * ~deaths + births
self.__update_visualisation()
Outputs¶
The sole output is an animation as shown above.