forked from Maxluli/RustyPropagation
Compare commits
2 Commits
master
...
4b88165c31
| Author | SHA1 | Date | |
|---|---|---|---|
|
4b88165c31
|
|||
|
5eddad02ee
|
+1
-1
@@ -1,7 +1,7 @@
|
||||
// use crate::prelude::*;
|
||||
|
||||
// #[derive(Copy, Clone, PartialEq)]
|
||||
#[derive(PartialEq)]
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum State {
|
||||
Normal,
|
||||
Infected,
|
||||
|
||||
+33
-19
@@ -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);
|
||||
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
|
||||
//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");
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user