remove casts and correct 0 limit situation on roll()

This commit is contained in:
2022-05-03 16:52:12 +02:00
parent e4076cba12
commit e6e67da8c1
2 changed files with 62 additions and 56 deletions
+5 -5
View File
@@ -5,14 +5,14 @@
// #[derive(Debug)]
pub struct Disease {
pub infection_rate: u32,
pub curing_rate: u32,
pub death_rate: u32,
pub traits: Vec<u32>,
pub infection_rate: i32,
pub curing_rate: i32,
pub death_rate: i32,
pub traits: Vec<i32>,
pub name: String,
}
impl Disease {
pub fn new(infection_r: u32, curing_r: u32, death_r: u32, the_name: String) -> Self {
pub fn new(infection_r: i32, curing_r: i32, death_r: i32, the_name: String) -> Self {
Self {
infection_rate: infection_r,
curing_rate: curing_r,
+57 -51
View File
@@ -6,29 +6,10 @@ pub struct Point {
y: i32,
}
#[derive(Debug)]
struct Stats {
normal: i32,
infected: i32,
immune: i32,
dead: i32,
}
impl Stats {
fn new() -> Stats {
Stats {
normal: 0,
infected: 0,
immune: 0,
dead: 0,
}
}
}
pub struct Population {
pub start_infected_ratio: u32,
pub start_immune_ratio: u32,
pub start_dead_ratio: u32,
pub start_infected_ratio: i32,
pub start_immune_ratio: i32,
pub start_dead_ratio: i32,
pub humans: Vec<Human>,
pub width: i32,
pub height: i32,
@@ -40,32 +21,12 @@ pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
((y * width) + x) as usize
}
fn humans_stats(humans: &Vec<Human>) -> Stats {
let mut stats: Stats = Stats::new();
for human in humans.iter() {
match human.present_state {
State::Normal => {
stats.normal += 1;
}
State::Infected => {
stats.infected += 1;
}
State::Immune => {
stats.immune += 1;
}
State::Dead => {
stats.dead += 1;
}
}
}
stats
}
impl Population {
pub fn new(
start_infected_ratio: u32,
start_immune_ratio: u32,
start_dead_ratio: u32,
start_infected_ratio: i32,
start_immune_ratio: i32,
start_dead_ratio: i32,
width: i32,
height: i32,
plague: Disease,
@@ -313,9 +274,13 @@ impl Population {
// }
}
pub fn roll(probability: u32) -> bool {
let mut rng = rand::thread_rng();
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32
pub fn roll(probability: i32) -> bool {
if probability > 0 {
let mut rng = rand::thread_rng();
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability
} else {
false
}
}
#[cfg(test)]
@@ -323,6 +288,46 @@ mod tests {
use super::*;
use parameterized::parameterized;
#[derive(Debug)]
struct Stats {
normal: i32,
infected: i32,
immune: i32,
dead: i32,
}
impl Stats {
fn new() -> Stats {
Stats {
normal: 0,
infected: 0,
immune: 0,
dead: 0,
}
}
}
fn humans_stats(humans: &Vec<Human>) -> Stats {
let mut stats: Stats = Stats::new();
for human in humans.iter() {
match human.present_state {
State::Normal => {
stats.normal += 1;
}
State::Infected => {
stats.infected += 1;
}
State::Immune => {
stats.immune += 1;
}
State::Dead => {
stats.dead += 1;
}
}
}
stats
}
#[parameterized(x = {
2, 3, 5
}, y = {
@@ -429,9 +434,10 @@ mod tests {
}
#[parameterized(rate = {0, 100}, expected = {false, true})]
fn roll_test(rate: u32, expected: bool) {
fn roll_test(rate: i32, expected: bool) {
let tries = 1000;
let mut result = 0;
println!("Testing roll, rate {}, expected {}", rate, expected);
for _x in 0..1000 {
if roll(rate) == expected {
result += 1;
@@ -442,8 +448,8 @@ mod tests {
#[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 1})]
fn propage_test(
infection_rate: u32,
death_rate: u32,
infection_rate: i32,
death_rate: i32,
infected_expected: i32,
) {
let disease: Disease;