A cellular automaton library and command-line simulator written in MoonBit.
cellular provides two families of cellular automata:
- Life-like automata (2D) — configurable birth/survival rules using the
standard
B/Snotation (e.g. Conway's Game of Life isB3/S23). - Elementary cellular automata (1D) — the 256 Wolfram rules (0–255), including Rule 30, Rule 90, and Rule 110.
The core is pure functions over plain Bool arrays, so it is trivially
testable and runs on the native, wasm, and wasm-gc targets.
moon add sssssurf/cellularlet rule = @cellular.conway()
// A glider: a spaceship that travels diagonally.
let mut grid = @cellular.glider()
for _ in 0..<4 {
grid = @cellular.life_step(rule, grid, true) // toroidal edges
}
println(@cellular.render_grid(grid))Custom life-like rules via B/S notation:
let highlife = @cellular.parse_life_rule("B36/S23").unwrap()
let grid = @cellular.life_step(highlife, @cellular.r_pentomino(), false)1D elementary cellular automata:
let state : Array[Bool] = Array::make(41, false)
state[20] = true
for row in @cellular.eca_run(30, state, 16, false) {
println(@cellular.render_eca(row)) // Rule 30 -> Sierpinski-like triangle
}// Grid helpers
let g = @cellular.Grid::new(20, 20)
let g = @cellular.Grid::from_string(".#.\n###\n.#.").unwrap()
let g = @cellular.Grid::random(20, 20, 0.3)
let copy = g.copy()
let d : Double = g.density() // fraction of live cells
// Stepping and analysis
let next = @cellular.life_step(rule, g, true)
let history : Array[Grid] = @cellular.life_run(rule, g, 10, true)
let pop : Array[Int] = @cellular.population_curve(rule, g, 10, true)
// Period detection (None if no repeat within the limit)
let period = @cellular.find_period(rule, g, 100, true)@cellular.rule_descriptions() lists every built-in rule with its B/S notation.
Run the simulator without writing code:
# Conway's Game of Life, glider pattern, 10 generations
moon run cmd/main -- life B3/S23 10 glider
# Rule 30 (1D), width 41, 16 generations
moon run cmd/main -- eca 30 41 16
# List available patterns
moon run cmd/main -- patterns
# Random initial state, Conway's rule, 20x10 grid, 5 generations
moon run cmd/main -- random B3/S23 20 10 5
# Show every built-in rule and pattern
moon run cmd/main -- info| Command | Rule | Meaning |
|---|---|---|
life B3/S23 |
Conway's Game of Life | the classic rule |
life B36/S23 |
HighLife | adds a replicator |
life B2/S |
Seeds | every cell dies, explosive growth |
life B3678/S34678 |
Day & Night | symmetric under on/off inversion |
eca 30 |
Rule 30 | chaotic, used as a PRNG |
eca 90 |
Rule 90 | Sierpinski triangle |
eca 110 |
Rule 110 | Turing-complete |
Built-in patterns: block, blinker, toad, beacon, glider,
r-pentomino, beehive, tub.
Additional life-like rules are also available as functions: replicator,
life_without_death, maze, anneal, diamoeba, and coral.
moon test # run the test suite
moon build # compile
moon run cmd/main -- helpApache-2.0