diff options
Diffstat (limited to 'CLI/rust/src')
| -rw-r--r-- | CLI/rust/src/game_of_life/simulation/universe.rs | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/CLI/rust/src/game_of_life/simulation/universe.rs b/CLI/rust/src/game_of_life/simulation/universe.rs index 6e6c1d0..8c1965d 100644 --- a/CLI/rust/src/game_of_life/simulation/universe.rs +++ b/CLI/rust/src/game_of_life/simulation/universe.rs @@ -85,7 +85,7 @@ impl Universe { /// .xxx. | ..... | ..... | ..... | ..... | ..... | /// ..... | ..... | .xxx. | xxx.. | xx..x | ..... | /// - pub(crate) fn neighbors(self, y: isize, x: isize) -> Vec<bool> { + fn neighbors(&self, y: isize, x: isize) -> Vec<bool> { vec![ self[y - 1][x - 1], self[y - 1][x], @@ -98,6 +98,26 @@ impl Universe { ] } + pub(crate) fn evolve(&mut self) -> &Self { + for row in 0..self.opts.height { + for col in 0..self.opts.width { + let living_neighbors = self + .neighbors(row, col) + .iter() + .filter(|&c| *c == true) + .count(); + match self[row][col] { + true if living_neighbors == 2 || living_neighbors == 3 => { + self.future[row][col] = true + } + false if living_neighbors == 3 => self.future[row][col] = true, + _ => self.future[row][col] = false, + } + } + } + self.current = self.future.clone(); + self + } } #[cfg(test)] |
