forked from Maxluli/RustyPropagation
take roll out of Population
This commit is contained in:
+10
-8
@@ -151,11 +151,11 @@ impl Population {
|
|||||||
//now we can start to check if people would be infected or not
|
//now we can start to check if people would be infected or not
|
||||||
//let idx = human_idx(pos.x as i32, pos.y as i32, self.width as i32);
|
//let idx = human_idx(pos.x as i32, pos.y as i32, self.width as i32);
|
||||||
if pos.x > 0 && pos.x < self.width - 1 && pos.y > 0 && pos.y < self.height - 1 {
|
if pos.x > 0 && pos.x < self.width - 1 && pos.y > 0 && pos.y < self.height - 1 {
|
||||||
if Population::roll(self.plague.curing_rate) {
|
if roll(self.plague.curing_rate) {
|
||||||
//checks if the man recovers
|
//checks if the man recovers
|
||||||
people_to_cure.push(Point { x: pos.x, y: pos.y });
|
people_to_cure.push(Point { x: pos.x, y: pos.y });
|
||||||
} else {
|
} else {
|
||||||
if Population::roll(self.plague.death_rate) {
|
if roll(self.plague.death_rate) {
|
||||||
//cheks if the man dies
|
//cheks if the man dies
|
||||||
println!("someone should die");
|
println!("someone should die");
|
||||||
people_to_kill.push(Point { x: pos.x, y: pos.y });
|
people_to_kill.push(Point { x: pos.x, y: pos.y });
|
||||||
@@ -204,7 +204,7 @@ impl Population {
|
|||||||
let inf_idx =
|
let inf_idx =
|
||||||
human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
|
human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
|
||||||
if self.humans[inf_idx].present_state == State::Normal {
|
if self.humans[inf_idx].present_state == State::Normal {
|
||||||
if Population::roll(self.plague.infection_rate) {
|
if roll(self.plague.infection_rate) {
|
||||||
people_to_infect.push(Point {
|
people_to_infect.push(Point {
|
||||||
x: poss_infected_pos.x,
|
x: poss_infected_pos.x,
|
||||||
y: poss_infected_pos.y,
|
y: poss_infected_pos.y,
|
||||||
@@ -293,10 +293,7 @@ impl Population {
|
|||||||
}
|
}
|
||||||
stats
|
stats
|
||||||
}
|
}
|
||||||
pub fn roll(probability: u32) -> bool {
|
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32
|
|
||||||
}
|
|
||||||
// pub fn display(&mut self){
|
// pub fn display(&mut self){
|
||||||
// let sprite = "#";
|
// let sprite = "#";
|
||||||
// print!("\n");
|
// print!("\n");
|
||||||
@@ -316,6 +313,11 @@ impl Population {
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn roll(probability: u32) -> bool {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -431,7 +433,7 @@ mod tests {
|
|||||||
let tries = 1000;
|
let tries = 1000;
|
||||||
let mut result = 0;
|
let mut result = 0;
|
||||||
for _x in 0..1000 {
|
for _x in 0..1000 {
|
||||||
if Population::roll(rate) == expected {
|
if roll(rate) == expected {
|
||||||
result += 1;
|
result += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user