3 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
Maxluli eccb238f5e Merge pull request 'Many fixes:' (#1) from herel/RustyPropagation:fixes into master
Reviewed-on: Maxluli/RustyPropagation#1
2022-05-03 11:07:59 +00:00
2 changed files with 34 additions and 20 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
// use crate::prelude::*;
// #[derive(Copy, Clone, PartialEq)]
#[derive(PartialEq)]
#[derive(PartialEq, Debug)]
pub enum State {
Normal,
Infected,
+32 -18
View File
@@ -21,6 +21,16 @@ pub fn human_idx(x: i32, y: i32, width: i32) -> 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{
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);
@@ -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 {
// println!("To infect: {:?}", infected_position);
//people_to_infect.iter().map(|infected_position|{
@@ -190,9 +201,12 @@ impl Population{
for dead_position in &people_to_kill {
//people_to_kill.iter().map(|dead_position|{
let dead_index = human_idx(dead_position.x, dead_position.y, self.width);
if self.humans[dead_index].present_state == State::Dead {
println!("Already dead");
} else {
self.humans[dead_index].present_state = State::Dead;
}
//DEBUG
//println!("Killed someone");
};
stats
}
@@ -200,21 +214,21 @@ impl Population{
let mut rng = rand::thread_rng();
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32
}
pub fn display(&mut self){
let sprite = "#";
print!("\n");
for x in 0..self.width{
for y in 0..self.height{
let index = human_idx(x as i32,y as i32,self.width as i32);
match self.humans[index].present_state {
State::Normal => print!("{}",style(sprite).green()),
State::Dead => print!("{}",style(sprite).black()),
State::Infected => print!("{}",style(sprite).red()),
State::Immune => print!("{}",style(sprite).blue()),
_ => print!("{}",style(sprite).white()),
}
}
print!("\n");
}
}
// pub fn display(&mut self){
// let sprite = "#";
// print!("\n");
// for x in 0..self.width{
// for y in 0..self.height{
// let index = human_idx(x as i32,y as i32,self.width as i32);
// match self.humans[index].present_state {
// State::Normal => print!("{}",style(sprite).green()),
// State::Dead => print!("{}",style(sprite).black()),
// State::Infected => print!("{}",style(sprite).red()),
// State::Immune => print!("{}",style(sprite).blue()),
// _ => print!("{}",style(sprite).white()),
// }
// }
// print!("\n");
// }
// }
}