feat: add roll method to die and dice

This commit is contained in:
2025-11-15 15:37:57 +01:00
parent 430cd675be
commit 95766fb897
9 changed files with 60 additions and 1 deletions

View File

@@ -6,7 +6,10 @@ impl Dice {
for _ in 1..=dice { for _ in 1..=dice {
handful.push(Die::new(sides)); handful.push(Die::new(sides));
} }
Self { handful } Self {
handful,
roll_results: Vec::<u16>::new(),
}
} }
pub fn and_grab(mut self, dice: u16, sides: u16) -> Self { pub fn and_grab(mut self, dice: u16, sides: u16) -> Self {
for _ in 1..=dice { for _ in 1..=dice {

View File

@@ -0,0 +1,10 @@
use crate::*;
impl Dice {
pub fn roll(&mut self) {
self.roll_results.clear();
for die in &self.handful {
self.roll_results.push(die.roll())
}
}
}

View File

@@ -0,0 +1,11 @@
use crate::*;
impl Die {
pub fn roll(&self) -> u16 {
let mut rng = rand::rng();
rng.random_range(0..self.sides)
}
}
// #[cfg(test)]
// mod unit_tests { use super::*; }

View File

@@ -1 +1,3 @@
pub mod dice_grab; pub mod dice_grab;
pub mod dice_roll;
pub mod die_roll;

View File

@@ -1,5 +1,7 @@
use std::*; use std::*;
use rand::*;
mod fun; mod fun;
mod imp; mod imp;
mod mcr; mod mcr;

View File

@@ -3,6 +3,7 @@ use crate::*;
#[derive(Debug, Default, PartialEq, Clone)] #[derive(Debug, Default, PartialEq, Clone)]
pub struct O { pub struct O {
pub(crate) handful: Vec<Die>, pub(crate) handful: Vec<Die>,
pub(crate) roll_results: Vec<u16>,
} }
// impl std::fmt::Display for O { // impl std::fmt::Display for O {

View File

@@ -0,0 +1,17 @@
#[cfg(test)]
mod tests {
use crate::*;
#[test]
pub fn roll_should_yield_correct_number_or_results() {
let mut dice = Dice::grab(3, 6);
dice.roll();
assert_eq!(dice.roll_results.len(), 3);
}
#[test]
fn reroll_should_yield_correct_number_of_results() {
let mut dice = Dice::grab(3, 6);
dice.roll();
dice.roll();
assert_eq!(dice.roll_results.len(), 3);
}
}

View File

@@ -0,0 +1,11 @@
#[cfg(test)]
mod tests {
use crate::*;
#[test]
pub fn die_roll_should_always_stay_inside_range() {
let die = Die::new(6);
for _ in 1..=10000 {
assert!(die.roll() < 6);
}
}
}

View File

@@ -1 +1,3 @@
pub mod dice_grab; pub mod dice_grab;
pub mod die_roll;
pub mod dice_roll;