feat: add grab and and_grab methods to dice

This commit is contained in:
2025-11-15 15:26:15 +01:00
parent a9f9bcac09
commit 430cd675be
11 changed files with 88 additions and 0 deletions

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,17 @@
use crate::*;
impl Dice {
pub fn grab(dice: u16, sides: u16) -> Self {
let mut handful = Vec::<Die>::new();
for _ in 1..=dice {
handful.push(Die::new(sides));
}
Self { handful }
}
pub fn and_grab(mut self, dice: u16, sides: u16) -> Self {
for _ in 1..=dice {
self.handful.push(Die::new(sides));
}
self
}
}

View File

@@ -0,0 +1 @@
pub mod dice_grab;

View File

@@ -6,3 +6,10 @@ mod mcr;
mod str; mod str;
mod trt; mod trt;
mod tst; mod tst;
pub(crate) use crate::str::dice::O as Dice;
pub(crate) use crate::str::die::O as Die;
pub mod dice {
pub use crate::str::dice::O as Dice;
}

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,17 @@
use crate::*;
#[derive(Debug, Default, PartialEq, Clone)]
pub struct O {
pub(crate) handful: Vec<Die>,
}
// impl std::fmt::Display for O {
// fn fmt(&self, _f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
// todo!();
// }
// }
// #[cfg(test)]
// mod unit_tests {
// use super::*;
// }

View File

@@ -0,0 +1,12 @@
use crate::*;
#[derive(Debug, Default, PartialEq, Clone)]
pub struct O {
pub(crate) sides: u16,
}
impl Die {
pub fn new(sides: u16) -> Self {
Self { sides }
}
}

View File

@@ -0,0 +1,2 @@
pub mod dice;
pub mod die;

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,28 @@
#[cfg(test)]
mod tests {
use crate::*;
#[test]
pub fn grab_should_set_correct_number_of_dice() {
let dice = Dice::grab(3, 6);
assert_eq!(dice.handful.len(), 3);
}
#[test]
pub fn grab_should_set_correct_number_of_sides() {
let dice = Dice::grab(3, 6);
for idx in 0..=2 {
assert_eq!(dice.handful[idx].sides, 6);
}
}
#[test]
fn and_grab_should_add_correct_number_of_dice() {
let dice = Dice::grab(3, 6).and_grab(2, 8);
assert_eq!(dice.handful.len(), 5);
}
#[test]
fn and_grab_should_set_correct_number_of_sides() {
let dice = Dice::grab(3, 6).and_grab(2, 8);
for idx in 3..4 {
assert_eq!(dice.handful[idx].sides, 8);
}
}
}

View File

@@ -0,0 +1 @@
pub mod dice_grab;