1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use crate::grid::Grid;

pub trait Ticker {
    fn tick(grid: &Grid) -> Grid;
}

pub struct BasicRuleSet {}

impl Ticker for BasicRuleSet {
    fn tick(grid: &Grid) -> Grid {
        // identify live cells with two or three neighbours
        let live_cells = grid
            .alive_positions_iter()
            .filter(|pos| {
                let neighbour_count = grid.neighbour_count(**pos);

                neighbour_count == 2 || neighbour_count == 3
            })
            .chain(grid.neighbour_count_iter().filter_map(|(pos, count)| {
                if *count == 3 {
                    Some(pos)
                } else {
                    None
                }
            }))
            .cloned()
            .collect();

        Grid::new(live_cells)
    }
}