summaryrefslogtreecommitdiffstats
path: root/CLI/rust/src/game_of_life/simulation/universe.rs
diff options
context:
space:
mode:
authora14m <[email protected]>2021-04-07 18:55:59 +0200
committera14m <[email protected]>2021-04-07 18:55:59 +0200
commite46e336295b35850ba037247d4964b241aee92d9 (patch)
treebdaadc6a8b8d3c239e2d709f244654b0540e28d8 /CLI/rust/src/game_of_life/simulation/universe.rs
parent6cdcb8a1ce50883cc3e0af5bf89766b910931040 (diff)
Use the len from plane and add testing for evolve method
Diffstat (limited to 'CLI/rust/src/game_of_life/simulation/universe.rs')
-rw-r--r--CLI/rust/src/game_of_life/simulation/universe.rs54
1 files changed, 51 insertions, 3 deletions
diff --git a/CLI/rust/src/game_of_life/simulation/universe.rs b/CLI/rust/src/game_of_life/simulation/universe.rs
index e91e1bd..4dfde92 100644
--- a/CLI/rust/src/game_of_life/simulation/universe.rs
+++ b/CLI/rust/src/game_of_life/simulation/universe.rs
@@ -98,9 +98,11 @@ impl Universe {
]
}
- pub(crate) fn evolve(&mut self) -> &Self {
- for row in 0..self.opts.height {
- for col in 0..self.opts.width {
+ /// Update the current state of the universe (according to the game of life laws) and
+ /// set the current state to the next future generation
+ pub(crate) fn evolve(mut self) -> Self {
+ for row in 0..self.current.len() {
+ for col in 0..self.current.0.len() as isize {
let living_neighbors = self
.neighbors(row, col)
.iter()
@@ -248,3 +250,49 @@ mod neighbors {
assert_eq!(u.neighbors(0, 0), vec![true; 8]);
}
}
+
+#[cfg(test)]
+mod evolve {
+ use super::*;
+
+ fn opts() -> Opts {
+ Opts {
+ seed: 1337,
+ input: None,
+ width: 13,
+ height: 13,
+ live_cell: 'L',
+ dead_cell: 'D',
+ delay: 13,
+ }
+ }
+
+ #[test]
+ fn blinker() {
+ let universe = Universe {
+ opts: opts(),
+ future: Plane(vec![Plane(vec![false; 5]); 3]),
+ current: Plane(vec![
+ Plane(vec![false, false, false, false, false]),
+ Plane(vec![false, true, true, true, false]),
+ Plane(vec![false, false, false, false, false]),
+ ]),
+ };
+ assert_eq!(
+ universe.evolve(),
+ Universe {
+ opts: opts(),
+ future: Plane(vec![
+ Plane(vec![false, false, true, false, false]),
+ Plane(vec![false, false, true, false, false]),
+ Plane(vec![false, false, true, false, false]),
+ ]),
+ current: Plane(vec![
+ Plane(vec![false, false, true, false, false]),
+ Plane(vec![false, false, true, false, false]),
+ Plane(vec![false, false, true, false, false]),
+ ]),
+ }
+ )
+ }
+}