summaryrefslogtreecommitdiffstats
path: root/CLI/rust/src/game_of_life/simulation/universe.rs
diff options
context:
space:
mode:
authora14m <[email protected]>2021-04-07 17:26:07 +0200
committera14m <[email protected]>2021-04-07 17:26:07 +0200
commitffb998e6b6d45b50cc1eb46e8d0b76ca24b60b15 (patch)
treed46dbaf8ec140696f2443b09f1df16086abc3e60 /CLI/rust/src/game_of_life/simulation/universe.rs
parente21427dabffaee0ca4d3cf0a97c739a92470ff0f (diff)
Add draft evolve implementation
Diffstat (limited to 'CLI/rust/src/game_of_life/simulation/universe.rs')
-rw-r--r--CLI/rust/src/game_of_life/simulation/universe.rs22
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)]