Library Reference¶
Game of Life¶
The game of life algorithm.
-
pconway.core.gameoflife.compute_cell_next_state(current, neighbours)¶ Return the next state of the cell on position (i, j) in alive_matrix.
- Parameters
current (int) – The state of the cell, 1 or 0 (live or dead)
neighbours (array) – The number of alive cells around the cell.
- Returns
new_state – The new state of the cell, 1 or 0 (live or dead)
- Return type
int
-
pconway.core.gameoflife.compute_next_state(matrix)¶ Returns the next state of a matrix according to the game of life algs.
- Parameters
matrix (array) – A matrix with entities 1 or 0 (live or dead)
- Returns
next_matrix – The evoluted state.
- Return type
array
Note
Any live cell with fewer than two live neighbours dies. 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. Any dead cell with exactly three live neighbours becomes a live cell.
-
pconway.core.gameoflife.get_alive_matrix(matrix)¶ Returns a matrix with number of lives around each cell.
- Parameters
matrix (array) – A matrix with entities 1 or 0 (live or dead)
- Returns
alive_matrix – The number of alive cells around each cell.
- Return type
array
-
pconway.core.gameoflife.get_alive_neighbours(matrix, i, j)¶ Returns the number of alive neighbours around the i,j cell.
- Parameters
matrix (array) – A matrix with entities 1 or 0 (live or dead)
i (int) – Number of rows of the main cell of interest.
j (int) – Number of column of the main cell of interest.
- Returns
nalive – The number of alive cells around cell of interest.
- Return type
int
-
pconway.core.gameoflife.mutation(matrix, mutation_rate)¶ Mutate the population
- Parameters
matrix (array) – A matrix with entities 1 or 0 (live or dead)
mutation_rate (float) – A float from 0 to 1. The chance that a cell would flip. This applies after each evolution.
- Returns
matrix – The mutated matrix.
- Return type
array
Game of Life Classes¶
Base¶
A game of Conway’s game of life
-
class
pconway.core.game.GameOfLife(matrix, mutation_rate=0)¶ Bases:
objectA game of Conway’s game of life.
- Parameters
matrix (array) – A matrix with entities 1 or 0 (live or dead)
mutation_rate (float, optional) – A float from 0 to 1. The chance that a cell would mutate. This applies after each evolution. Defaults to 0.
-
matrix¶ A matrix with entities 1 or 0 (live or dead)
- Type
array
-
mutation_rate¶ A float from 0 to 1. The chance that a cell would mutate. This applies after each evolution. Defaults to 0.
- Type
float, optional
-
population¶ The number of live cells in the game.
- Type
int
-
iteration¶ The number of iteration evolved.
- Type
int
-
evolve()¶ Evolve a step.
Custom Games¶
Some defined games to start with
-
class
pconway.core.custom_game.RandomGame(nrow, ncol, seed=123, spawn_rate=0.5, mutation_rate=0)¶ Bases:
pconway.core.game.GameOfLifeConway’s game of life initialized with random population.
- Parameters
nrow (int) – Number of rows in the universe.
ncol (int) – Number of columns in the universe.
seed (int, optional) – The seed for the random number generator. Defaults to 123.
spawn_rate (float, optional) – The chance of a live cell being spawned. Defaults to 0.5.
mutation_rate (float, optional) – A float from 0 to 1. The chance that a cell would mutate. This applies after each evolution. Defaults to 0.
Render¶
Plays the Conway’s game of life.
-
pconway.render.play.play(game_type='random', fps=10, alive_char='.', dead_char=' ', fg_color='white', bg_color='black', borderless=True, duration=- 1, kwargs={})¶ Plays the game of life.
- Parameters
game_type (str) – “random”, … or …
fps (int) – Frame per second.
alive_char (str, optional) – The character representing a live cell. Defaults to “.”.
dead_char (str, optional) – The character representing a dead cell. Defaults to ” “.
fg_color (str, optional) – Foreground color. One of “black”, “blue”, “cyan”, “green”, “magenta”, “red”, “white”, “yellow”. Defaults to “white”.
bg_color (str, optional) – Background color. One of “black”, “blue”, “cyan”, “green”, “magenta”, “red”, “white”, “yellow”. Defaults to “black”.
borderless (bool, optional) – Borderless. Defaults to True.
duration (float, optional) – Duration of the game in seconds. Negative means play indefinitely. Defaults to -1.
**kwargs (dict) – Keyword arguments passed to the pconway.core.game.Game class.
- Returns
Returns True when finished.
- Return type
Boolean