forked from Maxluli/RustyPropagation
Compare commits
4 Commits
7193fb2b16
...
a2bbd1d431
| Author | SHA1 | Date | |
|---|---|---|---|
|
a2bbd1d431
|
|||
|
b456a9fb63
|
|||
|
89020eeb21
|
|||
|
5affe83fdc
|
+13
-13
@@ -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{
|
||||
// impl Human {
|
||||
// // humans with state "Normal"
|
||||
// pub fn new(pos_x: i32, pos_y: i32) -> Self {
|
||||
// Self {
|
||||
// present_state: State::Normal,
|
||||
// x : 0,
|
||||
// y : 0,
|
||||
// x: pos_x,
|
||||
// y: pos_y,
|
||||
// }
|
||||
// }
|
||||
}
|
||||
// // pub fn new_empty() -> Self{
|
||||
// // Self{
|
||||
// // present_state:State::Normal,
|
||||
// // x : 0,
|
||||
// // y : 0,
|
||||
// // }
|
||||
// // }
|
||||
// }
|
||||
|
||||
@@ -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);
|
||||
|
||||
+69
-51
@@ -85,9 +85,16 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
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.push_if_inside(
|
||||
&mut possible_infections,
|
||||
) || //Top Left
|
||||
self.is_inside_and_infected(
|
||||
Point {
|
||||
x: pos.x,
|
||||
y: pos.y - 1,
|
||||
},
|
||||
); //Top
|
||||
self.push_if_inside(
|
||||
&mut possible_infections,
|
||||
) || //Top
|
||||
self.is_inside_and_infected(
|
||||
Point {
|
||||
x: pos.x + 1,
|
||||
y: pos.y - 1,
|
||||
},
|
||||
); //Top Right
|
||||
self.push_if_inside(
|
||||
&mut possible_infections,
|
||||
) || //Top Right
|
||||
self.is_inside_and_infected(
|
||||
Point {
|
||||
x: pos.x - 1,
|
||||
y: pos.y,
|
||||
},
|
||||
); //Left
|
||||
self.push_if_inside(
|
||||
&mut possible_infections,
|
||||
) || //Left
|
||||
self.is_inside_and_infected(
|
||||
Point {
|
||||
x: pos.x + 1,
|
||||
y: pos.y,
|
||||
},
|
||||
); //Right
|
||||
self.push_if_inside(
|
||||
&mut possible_infections,
|
||||
) || //Right
|
||||
self.is_inside_and_infected(
|
||||
Point {
|
||||
x: pos.x - 1,
|
||||
y: pos.y + 1,
|
||||
},
|
||||
); //Bottom Left
|
||||
self.push_if_inside(
|
||||
&mut possible_infections,
|
||||
) || //Bottom Left
|
||||
self.is_inside_and_infected(
|
||||
Point {
|
||||
x: pos.x,
|
||||
y: pos.y + 1,
|
||||
},
|
||||
); //Bottom
|
||||
self.push_if_inside(
|
||||
&mut possible_infections,
|
||||
) || //Bottom
|
||||
self.is_inside_and_infected(
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user