feat: add Die and Dice structs with grab and and_grab methods

This commit is contained in:
2025-11-15 12:01:24 +01:00
parent ab757e3080
commit 589c25cdcd
12 changed files with 71 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,7 @@
use crate::*;
impl Die {
pub fn new(sides: u16) -> Die {
Self { sides }
}
}

View File

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

View File

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

View File

@@ -0,0 +1 @@

View File

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

View File

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

View File

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

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,23 @@
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn dice_grab_should_create_a_new_handful_of_dice() {
let dice_handful = Dice::grab(3, 6);
assert_eq!(dice_handful.handful.len(), 3);
for die in dice_handful.handful {
assert_eq!(die.sides, 6);
}
}
#[test]
fn test_dice_and_grab_should_add_more_dice_to_handful() {
let initial_dice = Dice::grab(2, 6);
let initial_count = initial_dice.handful.len();
let new_dice_handful = initial_dice.and_grab(2, 8);
assert_eq!(new_dice_handful.handful.len(), initial_count + 2);
// Check the sides of the newly added dice
for i in initial_count..new_dice_handful.handful.len() {
assert_eq!(new_dice_handful.handful[i].sides, 8);
}
}
}

View File

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