take roll out of Population

This commit is contained in:
2022-05-03 16:37:36 +02:00
parent 9940608781
commit e4076cba12
+10 -8
View File
@@ -151,11 +151,11 @@ impl Population {
//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);
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
people_to_cure.push(Point { x: pos.x, y: pos.y });
} else {
if Population::roll(self.plague.death_rate) {
if roll(self.plague.death_rate) {
//cheks if the man dies
println!("someone should die");
people_to_kill.push(Point { x: pos.x, y: pos.y });
@@ -204,7 +204,7 @@ impl Population {
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 Population::roll(self.plague.infection_rate) {
if roll(self.plague.infection_rate) {
people_to_infect.push(Point {
x: poss_infected_pos.x,
y: poss_infected_pos.y,
@@ -293,10 +293,7 @@ impl Population {
}
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){
// let sprite = "#";
// 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)]
mod tests {
use super::*;
@@ -431,7 +433,7 @@ mod tests {
let tries = 1000;
let mut result = 0;
for _x in 0..1000 {
if Population::roll(rate) == expected {
if roll(rate) == expected {
result += 1;
}
}