1 unstable release
0.1.0 | May 22, 2022 |
---|
#959 in Games
15KB
258 lines
Conway's Game of Life but customized.
This is a small and simple crate. The crate is meant to mimic Conway's Game of Life but with customization. You can edit the rules using the play
or play_for
by providing your own closure. Also, the [Cells][Cell] (squares/pixels) are 8-bit RGB values, which means you can make a colored game of life.
Note that this crate only provides functionality for 2D matrix manipulation designed for Game of Life. It does not provide functionality for drawing the game.
Examples
This is how you would use the normal rules of Conway's Game of Life. First, we create a life defining how big it is and which cells are alive and/or which color they are. (You can define them as RGB instead by replacing ALIVE
or DEAD
with Cell {r: 255, g: 255, b: 255 }
.) Then we use the play_for
method to apply our closure 2 times.
const ALIVE: Cell = Cell::alive();
const DEAD: Cell = Cell::dead();
fn main() {
let mut life = Life::from([
[ALIVE, DEAD, ALIVE],
[DEAD, ALIVE, DEAD],
[ALIVE, DEAD, ALIVE],
]);
life.play_for(2, |same, others, _, _| {
let alive = others.alive();
if alive < 2 {
DEAD
}
else if alive == 2 {
same
}
else if alive == 3 {
ALIVE
}
else {
DEAD
}
});
}