This is an example implementation of Conway's Game of Life in Python. The primary focus of the implementation is cleanliness, not performance. It serves as a lose guideline for Nelkinda Coderetreat facilitators.
This Python implementation is a new translation from Kotlin and has not yet undergone thorough review.
The universe of the Game of Life is an infinite, two-dimensional orthogonal grid of square cells. Each cell is in one of two possible states:
- Alive aka populated
- Dead aka unpopulated
Every cell interacts with its eight neighbors. The neighbors are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
- Underpopulation: Any live cell with fewer than 2 live neighbors dies.
- Survival: Any live cell with 2 or 3 live neighbors survives on to the next generation.
- Overpopulation Any live cell with more than 3 live neighbors dies.
- Reproduction (birth): Any dead cell with exactly 3 live neighbors becomes a live cell.
— Conway's Game of Life - Wikipedia
This implementation of Game of Life follows the following activities and constraints:
- Behavior Driven Development.
- Test-Driven Development.
- Immutable objects only (not enforced). Only pure functions.
- No variable reassignments (except for the Parser).
- Short functions only. Most functions are expression functions. Exception: The Parser is big.
- Domain-Specific Language:
The symbol names are taken from the problem domain.
A point, for example, is constructed with
P(x, y)
instead ofPoint(x, y)
. That makes the code shorter and easier to read. - Functional Core, Imperative Shell: The code is purely functional. The imperative shell is so far out that it's in the test only. The Parser implementation is imperative, but its interface and observable behavior are functional.