forked from Maxluli/RustyPropagation
Started C# porting git add . NOT WORKING FOR NOW
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
156
Cargo.lock
generated
Normal file
156
Cargo.lock
generated
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "console"
|
||||||
|
version = "0.15.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a28b32d32ca44b70c3e4acd7db1babf555fa026e385fb95f18028f88848b3c31"
|
||||||
|
dependencies = [
|
||||||
|
"encode_unicode",
|
||||||
|
"libc",
|
||||||
|
"once_cell",
|
||||||
|
"regex",
|
||||||
|
"terminal_size",
|
||||||
|
"unicode-width",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "encode_unicode"
|
||||||
|
version = "0.3.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.2.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.124"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "21a41fed9d98f27ab1c6d161da622a4fa35e8a54a8adc24bbf3ddd0ef70b0e50"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.5.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
|
||||||
|
dependencies = [
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.6.25"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rusty_propagation"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"console",
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "terminal_size"
|
||||||
|
version = "0.1.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-width"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.10.2+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-i686-pc-windows-gnu",
|
||||||
|
"winapi-x86_64-pc-windows-gnu",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-i686-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "rusty_propagation"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
console = "0.15.0"
|
||||||
|
rand = "0.8.5"
|
||||||
41
src/disease.rs
Normal file
41
src/disease.rs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/human.rs
Normal file
23
src/human.rs
Normal file
@@ -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
src/main.rs
Normal file
20
src/main.rs
Normal file
@@ -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...");
|
||||||
|
}
|
||||||
42
src/population.rs
Normal file
42
src/population.rs
Normal file
@@ -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