2 Commits

Author SHA1 Message Date
herel 4b88165c31 add tests 2022-05-03 13:50:10 +02:00
herel 5eddad02ee check dead state 2022-05-03 13:48:32 +02:00
2 changed files with 34 additions and 20 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
// use crate::prelude::*; // use crate::prelude::*;
// #[derive(Copy, Clone, PartialEq)] // #[derive(Copy, Clone, PartialEq)]
#[derive(PartialEq)] #[derive(PartialEq, Debug)]
pub enum State { pub enum State {
Normal, Normal,
Infected, Infected,
+33 -19
View File
@@ -21,6 +21,16 @@ pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
((y * width) + x)as usize ((y * width) + x)as usize
} }
#[test]
fn popuplation_gen() {
let disease = Disease::new(20,10,5,String::from("Covid 44"));
let population = Population::new(20,10,5,5,7,disease);
assert_eq!(population.humans.len(), 5 * 7);
for human in population.humans.iter() {
assert_eq!(human.present_state, State::Normal);
}
}
impl Population{ impl Population{
pub fn new(start_infected_ratio:u32,start_immune_ratio:u32,start_dead_ratio:u32,width:i32,height:i32,plague:Disease)->Self{ pub fn new(start_infected_ratio:u32,start_immune_ratio:u32,start_dead_ratio:u32,width:i32,height:i32,plague:Disease)->Self{
let mut the_humans: Vec<Human> = Vec::with_capacity((width*height) as usize); let mut the_humans: Vec<Human> = Vec::with_capacity((width*height) as usize);
@@ -167,6 +177,7 @@ impl Population{
// ##@ // ##@
} }
}; };
println!("{} to infect, {} to cure, {} to kill", people_to_infect.len(), people_to_cure.len(), people_to_kill.len());
for infected_position in &people_to_infect { for infected_position in &people_to_infect {
// println!("To infect: {:?}", infected_position); // println!("To infect: {:?}", infected_position);
//people_to_infect.iter().map(|infected_position|{ //people_to_infect.iter().map(|infected_position|{
@@ -190,9 +201,12 @@ impl Population{
for dead_position in &people_to_kill { for dead_position in &people_to_kill {
//people_to_kill.iter().map(|dead_position|{ //people_to_kill.iter().map(|dead_position|{
let dead_index = human_idx(dead_position.x, dead_position.y, self.width); let dead_index = human_idx(dead_position.x, dead_position.y, self.width);
self.humans[dead_index].present_state = State::Dead; if self.humans[dead_index].present_state == State::Dead {
println!("Already dead");
} else {
self.humans[dead_index].present_state = State::Dead;
}
//DEBUG //DEBUG
//println!("Killed someone");
}; };
stats stats
} }
@@ -200,21 +214,21 @@ impl Population{
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32 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");
for x in 0..self.width{ // for x in 0..self.width{
for y in 0..self.height{ // for y in 0..self.height{
let index = human_idx(x as i32,y as i32,self.width as i32); // let index = human_idx(x as i32,y as i32,self.width as i32);
match self.humans[index].present_state { // match self.humans[index].present_state {
State::Normal => print!("{}",style(sprite).green()), // State::Normal => print!("{}",style(sprite).green()),
State::Dead => print!("{}",style(sprite).black()), // State::Dead => print!("{}",style(sprite).black()),
State::Infected => print!("{}",style(sprite).red()), // State::Infected => print!("{}",style(sprite).red()),
State::Immune => print!("{}",style(sprite).blue()), // State::Immune => print!("{}",style(sprite).blue()),
_ => print!("{}",style(sprite).white()), // _ => print!("{}",style(sprite).white()),
} // }
} // }
print!("\n"); // print!("\n");
} // }
} // }
} }