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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
use crate::{ConwayState, EvolutionStrategy, IndexedDataOwned}; pub struct ConwayStrategy {} impl<CI, I> EvolutionStrategy<CI, ConwayState, I> for ConwayStrategy where I: Iterator<Item = IndexedDataOwned<CI, ConwayState>>, { fn next_state(&self, _: CI, cur_state: ConwayState, neighbors: I) -> ConwayState { let mut alive_count = 0; for (_, state) in neighbors { alive_count += match state { ConwayState::Alive => 1, ConwayState::Dead => 0, }; } if alive_count == 3 || alive_count == 2 && cur_state == ConwayState::Alive { ConwayState::Alive } else { ConwayState::Dead } } } impl ConwayStrategy { pub fn new() -> Self { Self {} } } #[cfg(test)] mod conway_strategy_test { use crate::{ConwayState, ConwayStrategy, EvolutionStrategy}; fn create_neighbors(alive_count: usize) -> Vec<ConwayState> { let mut res = vec![ConwayState::Alive; alive_count]; res.append(&mut vec![ConwayState::Dead; 8 - alive_count]); res } #[test] fn conway_strat_test_0() { let strat = ConwayStrategy::new(); let neighbors = create_neighbors(0); let neighbors_iter = neighbors.into_iter().enumerate(); let alive_next = strat.next_state(0, ConwayState::Alive, neighbors_iter); assert_eq!(alive_next, ConwayState::Dead); } }