From e46e336295b35850ba037247d4964b241aee92d9 Mon Sep 17 00:00:00 2001 From: a14m Date: Wed, 7 Apr 2021 18:55:59 +0200 Subject: Use the len from plane and add testing for evolve method --- CLI/rust/src/game_of_life/simulation/universe.rs | 54 ++++++++++++++++++++++-- 1 file 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]), + ]), + } + ) + } +} -- cgit v1.2.3