From 3a41670fea576a2657e525ff6693d45fbefe444c Mon Sep 17 00:00:00 2001 From: Rene Luria Date: Tue, 3 May 2022 23:17:45 +0200 Subject: [PATCH] check only normal people --- src/population.rs | 107 +++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/src/population.rs b/src/population.rs index 39d9964..f16e590 100644 --- a/src/population.rs +++ b/src/population.rs @@ -108,6 +108,8 @@ impl Population { pub fn propagate(&mut self) -> [i32; 4] { let mut people_to_check: Vec = Vec::with_capacity((self.width * self.height) as usize); + let mut possible_infected: Vec = + Vec::with_capacity((self.width * self.height) as usize); let mut people_to_infect: Vec = Vec::with_capacity((self.width * self.height) as usize); let mut people_to_cure: Vec = @@ -120,6 +122,7 @@ impl Population { for h in self.humans.iter() { match h.present_state { State::Normal => { + possible_infected.push(Point{ x: h.x, y: h.y}); stats[0] += 1; } State::Infected => { @@ -150,59 +153,57 @@ impl Population { } } } - for pos in self.humans.iter() { - if pos.present_state == State::Normal { - let infected: bool = self.is_inside_and_infected( - Point { - x: pos.x - 1, - y: pos.y - 1, - }, - ) || //Top Left - self.is_inside_and_infected( - Point { - x: pos.x, - y: pos.y - 1, - }, - ) || //Top - self.is_inside_and_infected( - Point { - x: pos.x + 1, - y: pos.y - 1, - }, - ) || //Top Right - self.is_inside_and_infected( - Point { - x: pos.x - 1, - y: pos.y, - }, - ) || //Left - self.is_inside_and_infected( - Point { - x: pos.x + 1, - y: pos.y, - }, - ) || //Right - self.is_inside_and_infected( - Point { - x: pos.x - 1, - y: pos.y + 1, - }, - ) || //Bottom Left - self.is_inside_and_infected( - Point { - x: pos.x, - y: pos.y + 1, - }, - ) || //Bottom - self.is_inside_and_infected( - Point { - x: pos.x + 1, - y: pos.y + 1, - }, - ); //Bottom Right - if infected { - people_to_infect.push(Point { x: pos.x, y: pos.y }); - } + for pos in possible_infected.iter() { + let infected: bool = self.is_inside_and_infected( + Point { + x: pos.x - 1, + y: pos.y - 1, + }, + ) || //Top Left + self.is_inside_and_infected( + Point { + x: pos.x, + y: pos.y - 1, + }, + ) || //Top + self.is_inside_and_infected( + Point { + x: pos.x + 1, + y: pos.y - 1, + }, + ) || //Top Right + self.is_inside_and_infected( + Point { + x: pos.x - 1, + y: pos.y, + }, + ) || //Left + self.is_inside_and_infected( + Point { + x: pos.x + 1, + y: pos.y, + }, + ) || //Right + self.is_inside_and_infected( + Point { + x: pos.x - 1, + y: pos.y + 1, + }, + ) || //Bottom Left + self.is_inside_and_infected( + Point { + x: pos.x, + y: pos.y + 1, + }, + ) || //Bottom + self.is_inside_and_infected( + Point { + x: pos.x + 1, + y: pos.y + 1, + }, + ); //Bottom Right + if infected { + people_to_infect.push(Point { x: pos.x, y: pos.y }); } }