forked from Maxluli/RustyPropagation
Started C# porting git add . NOT WORKING FOR NOW
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
use crate::prelude::*;
|
||||
use rand::Rng;
|
||||
|
||||
pub const MUTATION_TRAIT_INCREASE_PROBABILITY:i32 = 50;
|
||||
pub const MUTATION_TRAIT_CHANGE_AMOUNT:i32 = 20;
|
||||
|
||||
pub struct Disease {
|
||||
pub infection_rate:u32,
|
||||
pub curing_rate:u32,
|
||||
pub death_rate:u32,
|
||||
pub traits: Vec<u32>,
|
||||
pub name: String,
|
||||
}
|
||||
impl Disease{
|
||||
pub fn new(infection_r:u32,curing_r:u32,death_r:u32,the_name:String) -> Self{
|
||||
Self{
|
||||
infection_rate : infection_r,
|
||||
curing_rate : curing_r,
|
||||
death_rate : death_r,
|
||||
name : the_name,
|
||||
traits : vec![infection_r,curing_r,death_r],
|
||||
}
|
||||
}
|
||||
pub fn mutate(&mut self){
|
||||
let mut rng = rand::thread_rng();
|
||||
for i in 0..self.traits.len(){
|
||||
let mut new_ratio:i32 = self.traits[i] as i32;
|
||||
|
||||
if rng.gen_range(0..CORRECTED_PERCENTAGE) >= MUTATION_TRAIT_INCREASE_PROBABILITY{
|
||||
new_ratio += MUTATION_TRAIT_CHANGE_AMOUNT;
|
||||
}else{
|
||||
new_ratio -= MUTATION_TRAIT_CHANGE_AMOUNT;
|
||||
}
|
||||
if new_ratio < 0{
|
||||
new_ratio = 0;
|
||||
}
|
||||
new_ratio = new_ratio % CORRECTED_PERCENTAGE;
|
||||
self.traits[i] = new_ratio as u32;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub enum State {
|
||||
Normal,
|
||||
Infected,
|
||||
Dead,
|
||||
Immune,
|
||||
}
|
||||
pub struct Human {
|
||||
pub present_state : State,
|
||||
pub x : i32,
|
||||
pub y : i32,
|
||||
}
|
||||
impl Human{
|
||||
pub fn new(state : State, pos_x :i32, pos_y :i32) -> Self{
|
||||
Self{
|
||||
present_state : state,
|
||||
x : pos_x,
|
||||
y : pos_y
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
use console::Term;
|
||||
|
||||
mod human;
|
||||
mod disease;
|
||||
mod population;
|
||||
|
||||
mod prelude {
|
||||
pub use crate::human::*;
|
||||
pub use crate::disease::*;
|
||||
pub use crate::population::*;
|
||||
pub const CORRECTED_PERCENTAGE:i32 = 101;
|
||||
}
|
||||
|
||||
use prelude::*;
|
||||
|
||||
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...");
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Population{
|
||||
pub start_infected_ratio:u32,
|
||||
pub start_immune_ratio:u32,
|
||||
pub start_dead_ratio:u32,
|
||||
pub humans:Vec<Human>,
|
||||
pub width:u32,
|
||||
pub height:u32,
|
||||
pub age:u32,
|
||||
pub plague:Disease,
|
||||
}
|
||||
pub fn human_idx(x: i32, y: i32,width:i32) -> usize {
|
||||
((y * width) + x)as 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],
|
||||
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);
|
||||
}
|
||||
}
|
||||
Self{
|
||||
start_infected_ratio:start_infected_ratio,
|
||||
start_immune_ratio:start_immune_ratio,
|
||||
start_dead_ratio:start_dead_ratio,
|
||||
width:width,
|
||||
height:height,
|
||||
plague:plague,
|
||||
age:0,
|
||||
humans:the_humans,
|
||||
}
|
||||
}
|
||||
pub fn change_disease(&mut self, plague:Disease){
|
||||
self.plague = plague;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user