A Swift implementation of Conway's Game of Life with:
- An iOS app (SwiftUI) under
ConwayGamewith Core Data pagination for large datasets - A reusable engine as a Swift Package under
ConwayGameEngine - A CLI executable target
conway-clifor terminal simulations - A REST API under
ConwayAPI(Vapor)
ConwayGameEngine: Core simulation logic and patterns; includesconway-cli.- Tests:
cd ConwayGameEngine && swift test - CLI:
cd ConwayGameEngine && swift run conway-cli --help
- Tests:
ConwayAPI: REST API exposing the engine via HTTP.- Docs: see
ConwayAPI/README.md - Local run:
cd ConwayAPI && swift run conway-api - Docker:
cd ConwayAPI && docker compose up
- Docs: see
- Requirements: Xcode 15+, iOS 16+ simulator/device, macOS 13+
- Open the project:
open ConwayGame.xcodeproj - Select the
ConwayGamescheme and run on a simulator or device.
The CLI lives in the Swift package at ConwayGameEngine.
-
Build and run from the package directory:
cd ConwayGameEngineswift run conway-cli --help
-
Commands:
conway-cli run <width> <height> <generations> [pattern] [--density=<value>] [--rules=<name>]- Runs a simulation with the given grid size and generation count
- Optional
pattern:random,empty, or a named pattern --density: Random density (0.0-1.0), default: 0.25--rules: Rule preset (conway, highlife, daynight), default: conway
conway-cli pattern <name> [--rules=<name>]- Runs a predefined pattern for a short showcase
--rules: Rule preset (conway, highlife, daynight), default: conway
-
Examples:
swift run conway-cli run 20 20 50 randomswift run conway-cli run 10 10 25 empty --density=0.4swift run conway-cli run 15 15 30 random --rules=highlifeswift run conway-cli pattern glider --rules=daynight
Available patterns: block, beehive, blinker, toad, beacon, glider, pulsar, gospergun.
Output uses * for alive and . for dead.
The engine supports multiple configurable rule sets:
- Neighborhood: Moore (8 surrounding cells)
- Survival: a live cell with 2 or 3 neighbors stays alive
- Birth: a dead cell with exactly 3 neighbors becomes alive
- Otherwise: the cell is dead in the next generation
- HighLife: Conway rules + birth on 6 neighbors
- Day and Night: Survival on 3,4,6,7,8 neighbors; birth on 3,6,7,8 neighbors
Use --rules=<name> in CLI or configure programmatically in the iOS app.
- Finished states (engine/app/CLI):
- Extinction: all cells are dead.
- Cycle detected: a previously seen board state recurs (includes still lifes with period 1 and oscillators with period > 1).
- iOS app limits:
- Auto-play cap: pauses after 500 steps per run.
- Final state search cap: attempts up to 500 generations; if living cells remain with no cycle/extinction, it reports that the generation limit was reached.
- CLI behavior:
run: runs exactly the number of generations requested, but stops early on extinction or when a repeated state is detected.pattern: showcases up to configurable generations (default: 50) with short delays; also stops early if the pattern stabilizes.
- Bounded grid: no wrap-around at edges (cells outside the grid are always dead).
- Synchronous updates: the next generation is computed from the entire current state.
- CLI
randomstarts with configurable density (default: 25% alive cells). - Sim termination hints in CLI: stops on extinction or when a previously seen state recurs (cycle detected).
- Engine tests:
cd ConwayGameEngine && swift test - iOS app tests:
xcodebuild -scheme ConwayGame -destination 'platform=iOS Simulator,name=iPhone 16 Pro' test - Build CLI only:
cd ConwayGameEngine && swift build - Run CLI:
cd ConwayGameEngine && swift run conway-cli ...
Engine entry points:
ConwayGameEngine.computeNextState(_:)— next generation.ConwayGameEngine.computeStateAtGeneration(_:generation:)— advance multiple generations.
Performance & Scalability:
- Core Data pagination handles large board collections efficiently
- Comprehensive performance benchmarks validate scaling characteristics
GitHub Actions runs tests on Linux and macOS for Swift 5.10 and 6.0:
- ConwayAPI unit/integration tests
- ConwayGameEngine tests
conway-clibuild check
See the workflow at .github/workflows/conwayapi-ci.yml.