review: change Die to enum to handle range and value list in the future

This commit is contained in:
2025-11-16 10:01:13 +01:00
parent 4d4810b52b
commit 23185bfd86
6 changed files with 49 additions and 21 deletions

View File

@@ -1,10 +0,0 @@
use crate::*;
impl Die {
pub fn new(sides: u16) -> Self {
Self { sides }
}
}
// #[cfg(test)]
// mod unit_tests { use super::*; }

View File

@@ -2,7 +2,12 @@ use crate::*;
impl Die {
pub fn roll(&self) -> i32 {
let mut rng = rand::rng();
rng.random_range(1..=self.sides) as i32
fn roll_sides(sides: u16) -> i32 {
let mut rng = rand::rng();
rng.random_range(1..=sides) as i32
}
match self {
Die::Sides(sides) => roll_sides(*sides),
}
}
}

View File

@@ -4,7 +4,7 @@ impl Handful {
pub fn grab(dice_num: u16, sides: u16) -> Self {
let mut dice = Vec::<Die>::new();
for _ in 1..=dice_num {
dice.push(Die::new(sides));
dice.push(Die::Sides(sides));
}
Self { dice }
}

View File

@@ -1,4 +1,3 @@
pub mod die_new;
pub mod die_roll;
pub mod handful_grab;
pub mod handful_roll;