Skip to main content

dcconway/
rules.rs

1use crate::grid::Grid;
2
3pub trait Ticker {
4    fn tick(grid: &Grid) -> Grid;
5}
6
7pub struct BasicRuleSet {}
8
9impl Ticker for BasicRuleSet {
10    fn tick(grid: &Grid) -> Grid {
11        // identify live cells with two or three neighbours
12        let live_cells = grid
13            .alive_positions_iter()
14            .filter(|pos| {
15                let neighbour_count = grid.neighbour_count(**pos);
16
17                neighbour_count == 2 || neighbour_count == 3
18            })
19            .chain(grid.neighbour_count_iter().filter_map(|(pos, count)| {
20                if *count == 3 {
21                    Some(pos)
22                } else {
23                    None
24                }
25            }))
26            .cloned()
27            .collect();
28
29        Grid::new(live_cells)
30    }
31}