diff options
| author | a14m <[email protected]> | 2021-04-07 17:26:07 +0200 |
|---|---|---|
| committer | a14m <[email protected]> | 2021-04-07 17:26:07 +0200 |
| commit | ffb998e6b6d45b50cc1eb46e8d0b76ca24b60b15 (patch) | |
| tree | d46dbaf8ec140696f2443b09f1df16086abc3e60 /CLI/rust/src/game_of_life | |
| parent | e21427dabffaee0ca4d3cf0a97c739a92470ff0f (diff) | |
Add draft evolve implementation
Diffstat (limited to 'CLI/rust/src/game_of_life')
| -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)] |
