https://github.com/naseridev/rust-life
Conway's Game of Life in Rust
https://github.com/naseridev/rust-life
gameoflife rust
Last synced: about 1 month ago
JSON representation
Conway's Game of Life in Rust
- Host: GitHub
- URL: https://github.com/naseridev/rust-life
- Owner: naseridev
- License: mit
- Created: 2025-08-27T09:45:40.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2025-08-27T09:58:08.000Z (about 1 month ago)
- Last Synced: 2025-08-27T19:09:38.910Z (about 1 month ago)
- Topics: gameoflife, rust
- Language: Rust
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Conway's Game of Life in Rust
## Abstract
This project implements Conway's Game of Life, a zero-player cellular automaton devised by British mathematician John Horton Conway in 1970. The implementation utilizes the Rust programming language to demonstrate fundamental concepts in computational biology, emergent behavior, and discrete dynamical systems.
## Introduction
Conway's Game of Life is a cellular automaton that simulates the evolution of a population of cells on a two-dimensional grid. Despite its simple rules, the Game of Life exhibits complex emergent behaviors and has been extensively studied in the fields of mathematics, computer science, and theoretical biology.
### Mathematical Foundation
The Game of Life operates on an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states: alive (populated) or dead (unpopulated). The system evolves in discrete time steps according to the following rules:
1. **Underpopulation**: Any live cell with fewer than two live neighbors dies
2. **Survival**: Any live cell with two or three live neighbors survives to the next generation
3. **Overpopulation**: Any live cell with more than three live neighbors dies
4. **Reproduction**: Any dead cell with exactly three live neighbors becomes a live cellThese rules can be expressed mathematically as:
```
C(t+1) = {
1, if N(C(t)) = 3
C(t), if N(C(t)) = 2
0, otherwise
}
```Where `C(t)` represents the state of a cell at time `t`, and `N(C(t))` represents the number of live neighbors.
## Implementation Details
### Architecture
The implementation consists of several key components:
- **World Representation**: A 30×30 boolean matrix representing the cellular grid
- **Visualization Engine**: Console-based rendering using ASCII characters
- **Evolution Algorithm**: Implements the cellular automaton rules
- **Boundary Conditions**: Toroidal topology (wraparound edges)### Key Features
#### 1. Data Structure
```rust
type World = [[bool; WIDTH]; HEIGHT];
```
The world is represented as a two-dimensional array of boolean values, where `true` indicates a living cell and `false` represents a dead cell.#### 2. Neighbor Calculation
The implementation uses a Moore neighborhood (8-connectivity) with toroidal boundary conditions:```rust
let ny = ((y as i32 + dy + HEIGHT as i32) % HEIGHT as i32) as usize;
let nx = ((x as i32 + dx + WIDTH as i32) % WIDTH as i32) as usize;
```This approach ensures that cells on the edges of the grid have neighbors on the opposite side, effectively creating a torus topology.
#### 3. Evolution Algorithm
The evolution function implements a parallel update scheme where all cells are updated simultaneously based on the current state, preventing temporal artifacts that could occur with sequential updates.#### 4. Random Initialization
Initial conditions are generated using a pseudo-random number generator with a 1/3 probability for each cell to be alive, creating a balanced starting configuration.## System Requirements
- **Rust**: Version 1.70.0 or higher
- **Dependencies**:
- `clearscreen`: For terminal clearing functionality
- `rand`: For pseudo-random number generation## Installation and Execution
### Prerequisites
Ensure that Rust and Cargo are installed on your system. Visit [rustup.rs](https://rustup.rs/) for installation instructions.### Dependencies
Add the following dependencies to your `Cargo.toml`:```toml
[dependencies]
clearscreen = "2.0"
rand = "0.8"
```### Compilation and Execution
```bash
cargo run
```## Performance Characteristics
- **Time Complexity**: O(n²) per generation, where n is the grid dimension
- **Space Complexity**: O(n²) for storing the current and next generation states
- **Frame Rate**: Approximately 6.67 FPS (150ms per frame)## Educational Applications
This implementation serves as an excellent educational tool for demonstrating:
1. **Emergent Complexity**: How simple rules can generate complex patterns
2. **Cellular Automata Theory**: Fundamental concepts in discrete dynamical systems
3. **Computational Biology**: Basic principles of population dynamics
4. **Algorithm Design**: Efficient implementation of grid-based simulations
5. **Rust Programming**: Memory safety, ownership, and systems programming concepts## Theoretical Significance
The Game of Life has proven to be Turing complete, meaning it can simulate any computation that can be performed by a Turing machine. This property makes it a valuable tool for studying:
- Computational universality
- Pattern formation and self-organization
- Emergent behavior in complex systems
- Information propagation in discrete systems## Known Patterns
The simulation may produce various well-documented patterns, including:
- **Still Lifes**: Static patterns (blocks, beehives, loaves)
- **Oscillators**: Periodic patterns (blinkers, toads, pulsars)
- **Spaceships**: Moving patterns (gliders, lightweight spaceships)
- **Guns**: Patterns that emit spaceships periodically## Future Enhancements
Potential improvements to this implementation could include:
1. **Interactive Interface**: Allow users to modify the grid during execution
2. **Pattern Recognition**: Automatic detection and classification of emergent patterns
3. **Performance Optimization**: GPU acceleration for larger grids
4. **Statistical Analysis**: Population dynamics tracking and visualization
5. **Alternative Rule Sets**: Implementation of other cellular automata (e.g., Brian's Brain, Wireworld)## References
1. Conway, J. H. (1970). "The Game of Life". Scientific American, 223(4), 4-10.
2. Berlekamp, E. R., Conway, J. H., & Guy, R. K. (2001). "Winning Ways for Your Mathematical Plays, Volume 2". Academic Press.
3. Wolfram, S. (2002). "A New Kind of Science". Wolfram Media.
4. Gardner, M. (1970). "Mathematical Games: The fantastic combinations of John Conway's new solitaire game 'life'". Scientific American, 223(4), 120-123.---
**Note**: This implementation demonstrates fundamental concepts in cellular automata and serves as a foundation for more advanced studies in computational biology, complex systems, and theoretical computer science.