forked from Maxluli/RustyPropagation
Compare commits
2 Commits
master
..
4b88165c31
| Author | SHA1 | Date | |
|---|---|---|---|
|
4b88165c31
|
|||
|
5eddad02ee
|
@@ -1,9 +0,0 @@
|
||||
# Rusty Propagation
|
||||
Run :
|
||||
```
|
||||
cargo run -q
|
||||
```
|
||||
Speed run :
|
||||
```
|
||||
cargo run -q --release
|
||||
```
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
// use crate::prelude::*;
|
||||
|
||||
// #[derive(Copy, Clone, PartialEq)]
|
||||
#[derive(PartialEq)]
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum State {
|
||||
Normal,
|
||||
Infected,
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ fn main() {
|
||||
|
||||
|
||||
let disease = Disease::new(20,10,5,String::from("Covid 44"));
|
||||
let mut population = Population::new(20,10,5,1000,1000,disease);
|
||||
let mut population = Population::new(20,10,5,300,300,disease);
|
||||
//population.change_disease(disease);
|
||||
println!("Before Filling");
|
||||
//population.display();
|
||||
|
||||
+34
-35
@@ -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);
|
||||
@@ -50,6 +60,7 @@ impl Population{
|
||||
//One solution to this issue would be to have if else else statements but in this case for example 20% would be lower with the last because its 20% on the remaining population and not of the all
|
||||
//In other words I did it that way but it can be changed just its not the right method to have perfect ratios
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
let mut i: i32 = 0;
|
||||
for x in self.humans.iter_mut() {
|
||||
if rng.gen_range(0..CORRECTED_PERCENTAGE) < self.start_infected_ratio as i32
|
||||
@@ -76,7 +87,6 @@ impl Population{
|
||||
let mut stats: [i32;4] = [0,0,0,0];
|
||||
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
|
||||
|
||||
/*
|
||||
for h in self.humans.iter() {
|
||||
match h.present_state{
|
||||
State::Normal => {stats[0] += 1;}
|
||||
@@ -85,21 +95,7 @@ impl Population{
|
||||
State::Dead => {stats[3] += 1;}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
for x in 0..self.width{
|
||||
for y in 0..self.height{
|
||||
let idx = human_idx(x as i32, y as i32, self.width as i32);
|
||||
match self.humans[idx].present_state{
|
||||
State::Normal => {stats[0] += 1;}
|
||||
State::Infected => {people_to_check.push(Point{x:x,y:y});stats[1]+=1;}
|
||||
State::Immune => {stats[2] += 1;}
|
||||
State::Dead => {stats[3] += 1;}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//for pos in &people_to_check {
|
||||
// for pos in &people_to_check {
|
||||
for pos in people_to_check.iter() {
|
||||
//people_to_check.iter().map(|pos|{
|
||||
//get all the other people next to me and check if i die cure or infect
|
||||
@@ -181,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|{
|
||||
@@ -204,32 +201,34 @@ 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
|
||||
}
|
||||
pub fn roll(&self,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");
|
||||
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