

Two particularly interesting questions that we can ask are “What types of stable patterns arise most frequently from soup?” and “How long does soup generally last before stabilizing?” I will focus on the latter question for today’s post, as the former has become the bud of a more ambitious project that I will announce in the near future. In Conway’s Game of Life, a simple way to learn about the general dynamics of the game are to study what happens to soup – patterns made up of a random assortment of ON and OFF cells. Survive = status * survive_underpopulation * survive_overpopulation Survive_overpopulation = live_neighbors < = 3 Return FuncAnimation(fig, update, * *animation_kwargs)ĭef get_init_status(size, initial_prob_life): Status, live_neigbors = apply_conways_game_of_life_rules(status)Ĭolors = get_updated_colors(status, live_neigbors)Īnimation_kwargs = if animation_kwargs is None else animation_kwargs transAxes, ha = " left ", va = " bottom ", color = " dodgerblue ",įontsize = 25, family = " sans-serif ", fontweight = " bold ") text( 0.5, 0.965, " GAME OF LIFE ", transform =axes.

transAxes, ha = " right ", va = " bottom ", color = " w ", fontsize = 25,įamily = " sans-serif ", fontweight = " light ")Īxes. scatter( *board, animated =True, s =marker_size, edgecolor =None)Īxes.

Status = get_init_status(size, initial_prob_life)įig, axes = plt. """ Implementation of Conway ' s Game of Life Parameters - size: tuple Defines the size of the board (n by m) initial_prob_life: float Float in the range (0, 1] defines the initial probability of a square containing life at initialisation animation_kwargs: dict Dictionary of keyword arguments to be passed to FuncAnimation marker_size: Size of scatter plot markers Returns - Instance of FuncAnimation """ Coloured squares/cells depending on the the number of neighboursĭef play_game(size =( 50, 50), initial_prob_life = 0.5, animation_kwargs =None, marker_size = 300):.
Conway game of life python update#
Update the color and transparency of the scatter markers:.Apply Conway’s rules to generate the new state.Calculate the number of neighbours using convolution - very fast.apply conways rules based on the current system state:.Update the board using matplotlibs FuncAnimation tool, by passing an update function.Plot the board using matplotlibs scatter plot.Instantiate the board at random using numpy’s random() function and specifying an initial probability of life (1 - all cells alive, 0 - no live cells).Represent the status “live” or “dead” of each “square” on the board as True and False.Represent the “board” in our game using a numpy array, which can be constructed using numpy’s meshgrid function.We can pass the “wrap” option to scipy’s 2D convolution tool to facilitate this. Represented in 3D space this shape would be toroid specifically a torus. A most sophisticated solution is to wrap the universe edges in a circular fashion. When calculating the number of neighbours we have a few options when we reach the edge of the universe, the most simplistic implementation is that the game “universe” has distinct edges. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.Ī single cell has a total of 8 neighbouring cells i.e.Any live cell with more than three live neighbors dies, as if by overpopulation.Any live cell with two or three live neighbors lives on to the next generation.Any live cell with fewer than two live neighbors dies, as if by underpopulation.From matplotlib.animation import FuncAnimation
