feat: add Die and Dice structs with grab and and_grab methods
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
|
||||
17
01.workspace/oxydice_lib/src/imp/dice_grab.rs
Normal file
17
01.workspace/oxydice_lib/src/imp/dice_grab.rs
Normal 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
|
||||
}
|
||||
}
|
||||
7
01.workspace/oxydice_lib/src/imp/die_new.rs
Normal file
7
01.workspace/oxydice_lib/src/imp/die_new.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use crate::*;
|
||||
|
||||
impl Die {
|
||||
pub fn new(sides: u16) -> Die {
|
||||
Self { sides }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod dice_grab;
|
||||
pub mod die_new;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
|
||||
6
01.workspace/oxydice_lib/src/str/dice.rs
Normal file
6
01.workspace/oxydice_lib/src/str/dice.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use crate::*;
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Clone)]
|
||||
pub struct O {
|
||||
pub(crate) handful: Vec<Die>,
|
||||
}
|
||||
6
01.workspace/oxydice_lib/src/str/die.rs
Normal file
6
01.workspace/oxydice_lib/src/str/die.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use crate::*;
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Clone)]
|
||||
pub struct O {
|
||||
pub(crate) sides: u16,
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod dice;
|
||||
pub mod die;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
|
||||
23
01.workspace/oxydice_lib/src/tst/dice_grab.rs
Normal file
23
01.workspace/oxydice_lib/src/tst/dice_grab.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
|
||||
pub mod dice_grab;
|
||||
|
||||
Reference in New Issue
Block a user