forked from Maxluli/RustyPropagation
Compare commits
4 Commits
7193fb2b16
...
a2bbd1d431
| Author | SHA1 | Date | |
|---|---|---|---|
|
a2bbd1d431
|
|||
|
b456a9fb63
|
|||
|
89020eeb21
|
|||
|
5affe83fdc
|
+17
-17
@@ -14,20 +14,20 @@ pub struct Human {
|
|||||||
pub x: i32,
|
pub x: i32,
|
||||||
pub y: i32,
|
pub y: i32,
|
||||||
}
|
}
|
||||||
impl Human {
|
// impl Human {
|
||||||
// humans with state "Normal"
|
// // humans with state "Normal"
|
||||||
pub fn new(pos_x: i32, pos_y: i32) -> Self {
|
// pub fn new(pos_x: i32, pos_y: i32) -> Self {
|
||||||
Self {
|
// Self {
|
||||||
present_state: State::Normal,
|
// present_state: State::Normal,
|
||||||
x: pos_x,
|
// x: pos_x,
|
||||||
y: pos_y,
|
// y: pos_y,
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
// pub fn new_empty() -> Self{
|
// // pub fn new_empty() -> Self{
|
||||||
// Self{
|
// // Self{
|
||||||
// present_state:State::Normal,
|
// // present_state:State::Normal,
|
||||||
// x : 0,
|
// // x : 0,
|
||||||
// y : 0,
|
// // y : 0,
|
||||||
// }
|
// // }
|
||||||
// }
|
// // }
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ fn main() {
|
|||||||
let term = Term::stdout();
|
let term = Term::stdout();
|
||||||
term.write_line("********** Rusty Propagation (Console) 2022 **********")
|
term.write_line("********** Rusty Propagation (Console) 2022 **********")
|
||||||
.expect("Oops Looks like we have a problem here...");
|
.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 disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
||||||
let mut population = Population::new(20, 10, 5, 1000, 1000, disease);
|
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) {
|
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) {
|
if roll(self.plague.death_rate) {
|
||||||
//cheks if the man dies
|
//cheks if the man dies
|
||||||
people_to_kill.push(Point { x: pos.x, y: pos.y });
|
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});
|
for pos in self.humans.iter() {
|
||||||
|
if pos.present_state == State::Normal {
|
||||||
self.push_if_inside(
|
let infected: bool = self.is_inside_and_infected(
|
||||||
&mut possible_infections,
|
|
||||||
Point {
|
Point {
|
||||||
x: pos.x - 1,
|
x: pos.x - 1,
|
||||||
y: pos.y - 1,
|
y: pos.y - 1,
|
||||||
},
|
},
|
||||||
); //Top Left
|
) || //Top Left
|
||||||
self.push_if_inside(
|
self.is_inside_and_infected(
|
||||||
&mut possible_infections,
|
|
||||||
Point {
|
Point {
|
||||||
x: pos.x,
|
x: pos.x,
|
||||||
y: pos.y - 1,
|
y: pos.y - 1,
|
||||||
},
|
},
|
||||||
); //Top
|
) || //Top
|
||||||
self.push_if_inside(
|
self.is_inside_and_infected(
|
||||||
&mut possible_infections,
|
|
||||||
Point {
|
Point {
|
||||||
x: pos.x + 1,
|
x: pos.x + 1,
|
||||||
y: pos.y - 1,
|
y: pos.y - 1,
|
||||||
},
|
},
|
||||||
); //Top Right
|
) || //Top Right
|
||||||
self.push_if_inside(
|
self.is_inside_and_infected(
|
||||||
&mut possible_infections,
|
|
||||||
Point {
|
Point {
|
||||||
x: pos.x - 1,
|
x: pos.x - 1,
|
||||||
y: pos.y,
|
y: pos.y,
|
||||||
},
|
},
|
||||||
); //Left
|
) || //Left
|
||||||
self.push_if_inside(
|
self.is_inside_and_infected(
|
||||||
&mut possible_infections,
|
|
||||||
Point {
|
Point {
|
||||||
x: pos.x + 1,
|
x: pos.x + 1,
|
||||||
y: pos.y,
|
y: pos.y,
|
||||||
},
|
},
|
||||||
); //Right
|
) || //Right
|
||||||
self.push_if_inside(
|
self.is_inside_and_infected(
|
||||||
&mut possible_infections,
|
|
||||||
Point {
|
Point {
|
||||||
x: pos.x - 1,
|
x: pos.x - 1,
|
||||||
y: pos.y + 1,
|
y: pos.y + 1,
|
||||||
},
|
},
|
||||||
); //Bottom Left
|
) || //Bottom Left
|
||||||
self.push_if_inside(
|
self.is_inside_and_infected(
|
||||||
&mut possible_infections,
|
|
||||||
Point {
|
Point {
|
||||||
x: pos.x,
|
x: pos.x,
|
||||||
y: pos.y + 1,
|
y: pos.y + 1,
|
||||||
},
|
},
|
||||||
); //Bottom
|
) || //Bottom
|
||||||
self.push_if_inside(
|
self.is_inside_and_infected(
|
||||||
&mut possible_infections,
|
|
||||||
Point {
|
Point {
|
||||||
x: pos.x + 1,
|
x: pos.x + 1,
|
||||||
y: pos.y + 1,
|
y: pos.y + 1,
|
||||||
},
|
},
|
||||||
); //Bottom Right
|
);
|
||||||
for poss_infected_pos in possible_infections.iter() {
|
if infected {
|
||||||
//possible_infections.iter().map(|poss_infected_pos|{
|
people_to_infect.push(Point { x: pos.x, y: pos.y });
|
||||||
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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// ); //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() {
|
for infected_position in people_to_infect.iter() {
|
||||||
// println!("To infect: {:?}", infected_position);
|
// println!("To infect: {:?}", infected_position);
|
||||||
@@ -343,7 +345,11 @@ mod tests {
|
|||||||
let mut stats: Stats;
|
let mut stats: Stats;
|
||||||
|
|
||||||
for _ in 0..10 {
|
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);
|
stats = humans_stats(&humans);
|
||||||
assert_eq!(stats.normal, 10);
|
assert_eq!(stats.normal, 10);
|
||||||
@@ -558,26 +564,38 @@ mod tests {
|
|||||||
println!("stats after init: {:?}", stats);
|
println!("stats after init: {:?}", stats);
|
||||||
assert_eq!(stats.normal, 8);
|
assert_eq!(stats.normal, 8);
|
||||||
|
|
||||||
// kill every one
|
// infect every one
|
||||||
propagate_stats = population.propagate();
|
propagate_stats = population.propagate();
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
println!("population: {:?}", stats);
|
println!("population: {:?}", stats);
|
||||||
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
||||||
assert_eq!(stats.normal, 8);
|
assert_eq!(stats.normal, 0);
|
||||||
assert_eq!(stats.infected, 0);
|
assert_eq!(stats.infected, 8);
|
||||||
assert_eq!(stats.immune, 1);
|
assert_eq!(stats.immune, 1);
|
||||||
assert_eq!(stats.dead, 0);
|
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 {
|
for _x in 0..100 {
|
||||||
propagate_stats = population.propagate();
|
propagate_stats = population.propagate();
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
println!("population: {:?}", stats);
|
println!("population: {:?}", stats);
|
||||||
assert_eq!(propagate_stats, [8, 0, 1, 0]);
|
assert_eq!(propagate_stats, [0, 0, 9, 0]);
|
||||||
assert_eq!(stats.normal, 8);
|
assert_eq!(stats.normal, 0);
|
||||||
assert_eq!(stats.infected, 0);
|
assert_eq!(stats.infected, 0);
|
||||||
assert_eq!(stats.immune, 1);
|
assert_eq!(stats.immune, 9);
|
||||||
assert_eq!(stats.dead, 0);
|
assert_eq!(stats.dead, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user