cellular_automata.game_of_life

Conway’s Game Of Life, Author Anurag Kumar(mailto:anuragkumarak95@gmail.com)

Requirements:
  • numpy

  • random

  • time

  • matplotlib

Python:
  • 3.5

Usage:
  • $python3 game_of_life <canvas_size:int>

Game-Of-Life Rules:

1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. 2. Any live cell with two or three live neighbours lives on to the next generation. 3. Any live cell with more than three live neighbours dies, as if by over-population. 4. Any dead cell with exactly three live neighbours be- comes a live cell, as if by reproduction.

Attributes

canvas_size

choice

usage_doc

Functions

__judge_point(→ bool)

Apply Conway's Game of Life rules to determine the next state of a cell.

create_canvas(→ list[list[bool]])

Create a square canvas of given size filled with False (dead cells).

run(→ list[list[bool]])

Run one generation of Conway's Game of Life on the canvas.

seed(→ None)

Module Contents

cellular_automata.game_of_life.__judge_point(pt: bool, neighbours: list[list[bool]]) bool

Apply Conway’s Game of Life rules to determine the next state of a cell.

Args:

pt: Current state of the cell (True=alive, False=dead) neighbours: 3x3 grid including the cell and its 8 neighbors

Returns:

The next state of the cell

Rules:
  1. Live cell with <2 live neighbours dies (under-population)

  2. Live cell with 2-3 live neighbours survives

  3. Live cell with >3 live neighbours dies (over-population)

  4. Dead cell with exactly 3 live neighbours becomes alive

>>> __judge_point(
...     True, [[True, True, False], [False, True, False], [False, False, False]]
... )
True
>>> __judge_point(
...     True, [[True, False, False], [False, True, False], [False, False, False]]
... )
False
>>> __judge_point(
...     True, [[True, True, True], [True, True, False], [False, False, False]]
... )
False
>>> __judge_point(
...     False, [[True, True, False], [True, False, False], [False, False, False]]
... )
True
>>> __judge_point(
...     False, [[True, False, False], [False, False, False], [False, False, False]]
... )
False
cellular_automata.game_of_life.create_canvas(size: int) list[list[bool]]

Create a square canvas of given size filled with False (dead cells).

Args:

size: The dimension of the square canvas

Returns:

A size x size 2D list of boolean values, all initialized to False

>>> canvas = create_canvas(3)
>>> len(canvas)
3
>>> len(canvas[0])
3
>>> all(all(not cell for cell in row) for row in canvas)
True
>>> create_canvas(1)
[[False]]
>>> create_canvas(0)
[]
cellular_automata.game_of_life.run(canvas: list[list[bool]]) list[list[bool]]

Run one generation of Conway’s Game of Life on the canvas.

Applies the Game of Life rules to all cells simultaneously to produce the next generation.

Args:

canvas: 2D list representing current state of cells

Returns:

2D list representing the next generation state

>>> blinker = [[False, False, False, False, False],
...            [False, False, True, False, False],
...            [False, False, True, False, False],
...            [False, False, True, False, False],
...            [False, False, False, False, False]]
>>> result = run(blinker)
>>> result[2]
[False, True, True, True, False]
>>> run([[False, False, False], [False, False, False], [False, False, False]])
[[False, False, False], [False, False, False], [False, False, False]]
>>> block = [[False, False, False, False],
...          [False, True, True, False],
...          [False, True, True, False],
...          [False, False, False, False]]
>>> run(block)[1]
[False, True, True, False]
cellular_automata.game_of_life.seed(canvas: list[list[bool]]) None
cellular_automata.game_of_life.canvas_size
cellular_automata.game_of_life.choice = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
cellular_automata.game_of_life.usage_doc = 'Usage of script: script_name <size_of_canvas:int>'