https://github.com/dpm76/gameoflife
Game of Life simulator in Python featuring clean architecture, unit testing, and graphical visualization.
https://github.com/dpm76/gameoflife
cellularautomata clean-architecture console-app conway design-patterns gameoflife gameoflifesimulation infinitegrid portfolio python python-3 simulation software-architecture tkinter tkinter-python ui unittest
Last synced: 7 days ago
JSON representation
Game of Life simulator in Python featuring clean architecture, unit testing, and graphical visualization.
- Host: GitHub
- URL: https://github.com/dpm76/gameoflife
- Owner: dpm76
- License: mit
- Created: 2025-11-16T16:21:16.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-11-18T10:24:20.000Z (7 months ago)
- Last Synced: 2025-11-18T13:10:58.394Z (7 months ago)
- Topics: cellularautomata, clean-architecture, console-app, conway, design-patterns, gameoflife, gameoflifesimulation, infinitegrid, portfolio, python, python-3, simulation, software-architecture, tkinter, tkinter-python, ui, unittest
- Language: Python
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 𧬠Game of Life β Python Implementation
A **Python implementation** of Conwayβs **Game of Life** simulating the evolution of cellular automata through standard rules.
The initial state is defined in a file and the simulation can be visualized:
* in the **console**, or
* in a **GUI window** (requires `tkinter`)
---
## π Project Structure
```
GameOfLife/
β
βββ game/ # Core Game of Life logic
βββ ui/ # User interfaces (console / GUI)
βββ initial_states/ # Sample initial state input files
βββ test/ # Unit tests
βββ main.py # Application entry point
βββ LICENSE
βββ README.md
```
Game logic and user interface are cleanly separated across modules.
---
## π§© Software Design, Architecture & Best Practices
### Overview
This project is structured with a clear separation of concerns:
| Layer | Directory | Responsibility |
| --------------------------- | --------- | ---------------------------------------------- |
| **Game Logic** | `/game` | Cell state management and Conway's rules |
| **User Interface** | `/ui` | Console and Tkinter GUI visualization |
| **Application Entry Point** | `main.py` | Command-line parsing and runtime configuration |
This modular architecture allows the UI to evolve independently from the game engineβfor example, adding a web or mobile UI without modifying the core simulation logic.
---
### π― Domain-Driven Entities
The simulation is based on two core domain objects:
| Class | Type | Responsibility |
| --------------- | ------------------------ | ----------------------------------------------------------- |
| `Cell` | Value Object | Represents a living cell in the infinite grid |
| `CandidateCell` | Specialized Value Object | Used temporarily during evolution to compute survival/birth |
The board (`Board`) receives and produces lists of these entities each turn, applying Conway's Game of Life rules.
---
### π§ Design Patterns & Engineering Principles Applied
| Pattern / Principle | Location | Benefit |
| ---------------------------------- | ---------------------------------------- | ---------------------------------------------------- |
| **Value Object Pattern** | `Cell` | Coordinates define identity; correct equality checks |
| **Inheritance for Specialization** | `CandidateCell(Cell)` | Extends state only when required for evolution |
| **Transient Entity Pattern** | `CandidateCell` | Exists only during a single turn β memory efficiency |
| **Spatial Hashing** | `Cell.create_hash()` | Efficient O(1) lookup in sparse infinite grid |
| **Separation of Concerns** | `/game` vs `/ui` | Core logic isolated from rendering concerns |
| **Dependency Inversion Principle** | UI depends on interfaces (Board results) | Easy to test and replace UI |
| **Encapsulation** | Private attributes | Protects internal state from external mutation |
| **Command-Line Interface Pattern** | `main.py` | Encourages flexible and scriptable usage |
---
### π§ͺ Testing & Maintainability
Unit tests included in `/test` help ensure reliable behaviors:
* correct cell identity and hashing
* accurate neighbor generation
* validated rule execution in `Board`
Designed for:
* fast execution
* deterministic output
* TDD-friendly iteration
---
### βοΈ Performance Considerations
| Optimization | Result |
| --------------------------------------------- | ------------------------------------------- |
| Only living cells are stored between turns | Takes advantage of sparse grids |
| Only neighbors of living cells are considered | Avoids scanning infinite grid |
| Spatial hashing for identity | Simplifies comparisons and dictionary usage |
These techniques allow **infinite board simulation** without large memory structures.
---
## βΆοΈ Running the Application
```bash
python main.py filename [--mode MODE]
```
### π Command line arguments
| Argument | Description |
| --------------------------- | ----------------------------------- |
| `filename` | Path to the initial state file |
| `--mode {console,gui,grid}` | Visualization mode (default: `gui`) |
| `-h`, `--help` | Display help |
Examples:
```bash
python main.py initial_states/glider --mode console
python main.py initial_states/gosper_glider_gun --mode gui
```
> GUI mode requires the `tkinter` module.
---
## π§ͺ Running Unit Tests
```bash
python -m unittest
```
Verbose output:
```bash
python -m unittest -v
```
---
## π οΈ Requirements & Virtual Environment (optional)
Only **Python standard libraries** are used.
To create and activate a virtual environment:
```bash
python -m venv venv
source venv/bin/activate # Linux/macOS
venv\Scripts\activate # Windows
```
---
## πͺ Installing tkinter
If GUI mode fails due to missing tkinter, install it using:
### Linux (Debian/Ubuntu/Raspberry Pi OS)
```bash
sudo apt-get update
sudo apt-get install python3-tk
```
### Fedora
```bash
sudo dnf install python3-tkinter
```
### Arch Linux
```bash
sudo pacman -S tk
```
### macOS & Windows
Usually included with Python from python.org.
If missing, reinstall Python using the official installer.
---
## π Initial State File Format
Initial states are stored in **plain text files**:
1. **First line** β starting board coordinates
Format:
```
x, y
```
Example:
```
0, 0
```
2. Starting from the **second line**:
* `*` β alive cell
* space `" "` β dead cell
Cells are placed **relative to the starting coordinates**.
All rows must have the same width.
### Example: Blinker (oscillator)
```
5, 3
*
*
*
```
This places the first cell of the second line at coordinate `(5,3)` and builds the pattern downward.
---
## π Famous Patterns
Some classic patterns compatible with this format:
| Pattern | Type | Description |
| --------------------- | ---------- | ----------------------------- |
| **Glider** | Spaceship | Moves diagonally forever |
| **Blinker** | Oscillator | Alternates between two states |
| **Toad** | Oscillator | Period-2 oscillator |
| **Gosper Glider Gun** | Gun | Produces infinite gliders |
| **Block** | Still Life | Stays unchanged |
Multiple ready-to-use samples are located in the `initial_states/` directory.
---
## π Possible Future Enhancements
* Support alternative rule sets (e.g., HighLife, Seedsβ¦)
* Parallelization or chunk-based updates
* Web or GPU rendering (Canvas / WebGL)
* Support for RLE pattern format (community standard)
---
## π Additional Resources
Learn more about Conwayβs Game of Life:
* Wikipedia β Conwayβs Game of Life
[https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life)
* ConwayLife.com β The Life Wiki
[https://www.conwaylife.com/wiki/Main_Page](https://www.conwaylife.com/wiki/Main_Page)
* Play Game of Life online
[https://playgameoflife.com/](https://playgameoflife.com/)
* Coding Train β Game of Life video tutorial
[https://www.youtube.com/watch?v=FWSR_7kZuYg](https://www.youtube.com/watch?v=FWSR_7kZuYg)
* Original 1970 Scientific American article (Martin Gardner)
[https://web.archive.org/web/20231101083058/http://www.ibiblio.org/lifepatterns/october1970scientificamerican.pdf](https://web.archive.org/web/20231101083058/http://www.ibiblio.org/lifepatterns/october1970scientificamerican.pdf)
---
## π License
This project is open-source (under MIT license).
Contributions and improvements are welcome!