Bare minimum with renderer

This commit is contained in:
2022-04-29 13:58:45 +02:00
parent 404c5b91a7
commit 32f58b03cf
4 changed files with 70 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
use crate::prelude::*;
use rand::Rng;
pub const MUTATION_TRAIT_INCREASE_PROBABILITY:i32 = 50;
pub const MUTATION_TRAIT_CHANGE_AMOUNT:i32 = 20;

View File

@@ -7,6 +7,7 @@ pub enum State {
Dead,
Immune,
}
#[derive(Clone)]
pub struct Human {
pub present_state : State,
pub x : i32,
@@ -20,4 +21,11 @@ impl Human{
y : pos_y
}
}
pub fn new_empty() -> Self{
Self{
present_state:State::Normal,
x : 0,
y : 0,
}
}
}

View File

@@ -1,13 +1,13 @@
use console::Term;
mod human;
mod disease;
mod population;
mod prelude {
pub use crate::human::*;
pub use crate::disease::*;
pub use crate::population::*;
pub use rand::Rng;
pub use console::Term;
pub use console::style;
pub const CORRECTED_PERCENTAGE:i32 = 101;
}
@@ -17,4 +17,14 @@ fn main() {
let term = Term::stdout();
term.write_line("********** Rusty Propagation (Console) 2022 **********").expect("Oops Looks like we have a problem here...");
term.write_line("Press any key to start the propagation").expect("Oops Looks like we have a problem here...");
let disease = Disease::new(20,10,5,String::from("Covid 44"));
let mut population = Population::new(20,10,5,30,30,disease);
println!("Before Filling");
population.display();
population.generate();
println!("After Filling");
population.display();
}

View File

@@ -16,13 +16,13 @@ pub fn human_idx(x: i32, y: i32,width:i32) -> usize {
impl Population{
pub fn new(start_infected_ratio:u32,start_immune_ratio:u32,start_dead_ratio:u32,width:u32,height:u32,plague:Disease)->Self{
let mut the_humans:Vec<Human>;
//filling the population
// could not use this because each human need to know his position in the vector
//humans : vec![Human::new(State::Normal, pos_x :i32, pos_y :i32); width*height],
//we first fill the vector with empty humans to prevent any pointer to null
the_humans = vec![Human::new_empty();(width*height)as usize];
//this is not sufficient, every Human should know its position so ...
for x in 0..width{
for y in 0..height{
the_humans[human_idx(x as i32,y as i32,width as i32)] = Human::new(State::Normal,x as i32,y as i32);
let index = human_idx(x as i32,y as i32,width as i32);
the_humans[index] = Human::new(State::Normal, x as i32, y as i32);
}
}
Self{
@@ -39,4 +39,47 @@ impl Population{
pub fn change_disease(&mut self, plague:Disease){
self.plague = plague;
}
pub fn generate(&mut self){
//The ratios will not be exact, for example someone who wants 100% infected 100% immune and 100% dead, he will have 100% dead because they are overwriting each others
//Maybe consider limiting the total to not exceed 100 in the view
//Other thing, there will always be more of the last one because someone who is already infected for example could be then put to immune or dead
//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();
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);
if rng.gen_range(0..CORRECTED_PERCENTAGE) < self.start_infected_ratio as i32
{
self.humans[index].present_state = State::Infected;
}
if rng.gen_range(0..CORRECTED_PERCENTAGE) < self.start_immune_ratio as i32
{
self.humans[index].present_state = State::Immune;
}
if rng.gen_range(0..CORRECTED_PERCENTAGE) < self.start_dead_ratio as i32
{
self.humans[index].present_state = State::Dead;
}
}
}
}
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");
}
}
}