feat: add roll method to Dice

This commit is contained in:
2025-11-15 12:41:41 +01:00
parent dd22385aff
commit f1dfbb0fff
11 changed files with 58 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
use oxydice_lib::*;
fn main() {
let dice = dice::Dice::grab(3, 6);
let mut dice = dice::Dice::grab(3, 6);
dice.roll();
}

View File

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

View File

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

View File

@@ -0,0 +1,8 @@
use crate::*;
impl Die {
pub fn roll(&self) -> u16 {
let mut rng = rand::rng();
rng.random_range(0..self.sides)
}
}

View File

@@ -1,2 +1,4 @@
pub mod dice_grab;
pub mod dice_roll;
pub mod die_new;
pub mod die_roll;

View File

@@ -1,5 +1,7 @@
use std::*;
use rand::*;
mod fun;
mod imp;
mod mcr;
@@ -9,6 +11,7 @@ mod tst;
pub(crate) use crate::str::dice::O as Dice;
pub(crate) use crate::str::die::O as Die;
pub(crate) use crate::str::roll_result::O as RollResult;
pub mod dice {
pub use crate::str::dice::O as Dice;

View File

@@ -3,4 +3,5 @@ use crate::*;
#[derive(Debug, Default, PartialEq, Clone)]
pub struct O {
pub(crate) handful: Vec<Die>,
pub(crate) result: RollResult,
}

View File

@@ -1,2 +1,3 @@
pub mod dice;
pub mod die;
pub mod roll_result;

View File

@@ -0,0 +1,6 @@
use crate::*;
#[derive(Debug, Default, PartialEq, Clone)]
pub struct O {
pub(crate) die_results: Vec<u16>,
}

View File

@@ -0,0 +1,20 @@
#[cfg(test)]
mod tests {
use crate::*;
#[test]
pub fn dice_roll_should_yield_always_the_proper_number_of_result() {
let mut dice = Dice::grab(3, 6);
for _ in 1..=1000 {
dice.roll();
assert_eq!(dice.result.die_results.len(), 3);
}
}
#[test]
pub fn dice_roll_should_yield_valid_results() {
let mut dice = Dice::grab(3, 6);
for _ in 1..=10000 {
dice.roll();
assert!(dice.result.die_results[0] < 6);
}
}
}

View File

@@ -1 +1,2 @@
pub mod dice_grab;
pub mod dice_roll;