4 Commits

Author SHA1 Message Date
herel a2bbd1d431 fix test propagate_infect_cure_all 2022-05-03 23:11:23 +02:00
herel b456a9fb63 human: remove new() trait 2022-05-03 23:10:54 +02:00
herel 89020eeb21 check surroundings instead 2022-05-03 23:01:48 +02:00
herel 5affe83fdc remove line 2022-05-03 22:29:51 +02:00
3 changed files with 122 additions and 106 deletions
+17 -17
View File
@@ -14,20 +14,20 @@ pub struct Human {
pub x: i32,
pub y: i32,
}
impl Human {
// humans with state "Normal"
pub fn new(pos_x: i32, pos_y: i32) -> Self {
Self {
present_state: State::Normal,
x: pos_x,
y: pos_y,
}
}
// pub fn new_empty() -> Self{
// Self{
// present_state:State::Normal,
// x : 0,
// y : 0,
// }
// }
}
// impl Human {
// // humans with state "Normal"
// pub fn new(pos_x: i32, pos_y: i32) -> Self {
// Self {
// present_state: State::Normal,
// x: pos_x,
// y: pos_y,
// }
// }
// // pub fn new_empty() -> Self{
// // Self{
// // present_state:State::Normal,
// // x : 0,
// // y : 0,
// // }
// // }
// }
-2
View File
@@ -17,8 +17,6 @@ fn main() {
let term = Term::stdout();
term.write_line("********** Rusty Propagation (Console) 2022 **********")
.expect("Oops Looks like we have a problem here...");
term.write_line("Press any key to start the propagation")
.expect("Oops Looks like we have a problem here...");
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
let mut population = Population::new(20, 10, 5, 1000, 1000, disease);
+105 -87
View File
@@ -85,20 +85,27 @@ impl Population {
}
}
fn push_if_inside(&self, point_list: &mut Vec<Point>, point: Point) {
fn is_inside_and_infected(&self, point: Point) -> bool {
if self.is_inside(&point) {
point_list.push(point);
let idx = human_idx(point.x, point.y, self.width);
if self.humans[idx].present_state == State::Infected {
roll(self.plague.infection_rate)
} else {
false
}
} else {
false
}
}
pub fn propagate(&mut self) -> [i32; 4] {
let mut people_to_check: Vec<Point> =
let mut people_to_check: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_infect: Vec<Point> =
let mut people_to_infect: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_cure: Vec<Point> =
let mut people_to_cure: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_kill: Vec<Point> =
let mut people_to_kill: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut stats: [i32; 4] = [0, 0, 0, 0];
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
@@ -133,83 +140,78 @@ impl Population {
if roll(self.plague.death_rate) {
//cheks if the man dies
people_to_kill.push(Point { x: pos.x, y: pos.y });
} else {
let mut possible_infections: Vec<Point> = Vec::with_capacity(8);
// Vec::new();
//possible_infections.push(Point{x:pos.x,y:pos.y});
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x - 1,
y: pos.y - 1,
},
); //Top Left
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x,
y: pos.y - 1,
},
); //Top
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x + 1,
y: pos.y - 1,
},
); //Top Right
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x - 1,
y: pos.y,
},
); //Left
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x + 1,
y: pos.y,
},
); //Right
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x - 1,
y: pos.y + 1,
},
); //Bottom Left
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x,
y: pos.y + 1,
},
); //Bottom
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x + 1,
y: pos.y + 1,
},
); //Bottom Right
for poss_infected_pos in possible_infections.iter() {
//possible_infections.iter().map(|poss_infected_pos|{
let inf_idx =
human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
if self.humans[inf_idx].present_state == State::Normal {
if roll(self.plague.infection_rate) {
people_to_infect.push(Point {
x: poss_infected_pos.x,
y: poss_infected_pos.y,
});
}
}
}
}
}
}
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,
},
);
if infected {
people_to_infect.push(Point { x: pos.x, y: pos.y });
}
}
}
// ); //Bottom Right
// for poss_infected_pos in possible_infections.iter() {
// //possible_infections.iter().map(|poss_infected_pos|{
// let inf_idx =
// human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
// if self.humans[inf_idx].present_state == State::Normal {
// if roll(self.plague.infection_rate) {
// people_to_infect.push(Point {
// x: poss_infected_pos.x,
// y: poss_infected_pos.y,
// });
// }
// }
// }
for infected_position in people_to_infect.iter() {
// println!("To infect: {:?}", infected_position);
@@ -343,7 +345,11 @@ mod tests {
let mut stats: Stats;
for _ in 0..10 {
humans.push(Human::new(0, 0));
humans.push(Human{
present_state: State::Normal,
x: 0,
y: 0,
});
}
stats = humans_stats(&humans);
assert_eq!(stats.normal, 10);
@@ -558,26 +564,38 @@ mod tests {
println!("stats after init: {:?}", stats);
assert_eq!(stats.normal, 8);
// kill every one
// infect every one
propagate_stats = population.propagate();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
println!("population: {:?}", stats);
assert_eq!(propagate_stats, [8, 1, 0, 0]);
assert_eq!(stats.normal, 8);
assert_eq!(stats.infected, 0);
assert_eq!(stats.normal, 0);
assert_eq!(stats.infected, 8);
assert_eq!(stats.immune, 1);
assert_eq!(stats.dead, 0);
// cure every one
propagate_stats = population.propagate();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
println!("population: {:?}", stats);
assert_eq!(propagate_stats, [0, 8, 1, 0]);
assert_eq!(stats.normal, 0);
assert_eq!(stats.infected, 0);
assert_eq!(stats.immune, 9);
assert_eq!(stats.dead, 0);
// then
for _x in 0..100 {
propagate_stats = population.propagate();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
println!("population: {:?}", stats);
assert_eq!(propagate_stats, [8, 0, 1, 0]);
assert_eq!(stats.normal, 8);
assert_eq!(propagate_stats, [0, 0, 9, 0]);
assert_eq!(stats.normal, 0);
assert_eq!(stats.infected, 0);
assert_eq!(stats.immune, 1);
assert_eq!(stats.immune, 9);
assert_eq!(stats.dead, 0);
}
}