feat: add roll method to die and dice
This commit is contained in:
@@ -6,7 +6,10 @@ impl Dice {
|
||||
for _ in 1..=dice {
|
||||
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 {
|
||||
for _ in 1..=dice {
|
||||
|
||||
10
01.workspace/oxydice_lib/src/imp/dice_roll.rs
Normal file
10
01.workspace/oxydice_lib/src/imp/dice_roll.rs
Normal 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())
|
||||
}
|
||||
}
|
||||
}
|
||||
11
01.workspace/oxydice_lib/src/imp/die_roll.rs
Normal file
11
01.workspace/oxydice_lib/src/imp/die_roll.rs
Normal 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::*; }
|
||||
@@ -1 +1,3 @@
|
||||
pub mod dice_grab;
|
||||
pub mod dice_roll;
|
||||
pub mod die_roll;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use std::*;
|
||||
|
||||
use rand::*;
|
||||
|
||||
mod fun;
|
||||
mod imp;
|
||||
mod mcr;
|
||||
|
||||
@@ -3,6 +3,7 @@ use crate::*;
|
||||
#[derive(Debug, Default, PartialEq, Clone)]
|
||||
pub struct O {
|
||||
pub(crate) handful: Vec<Die>,
|
||||
pub(crate) roll_results: Vec<u16>,
|
||||
}
|
||||
|
||||
// impl std::fmt::Display for O {
|
||||
|
||||
17
01.workspace/oxydice_lib/src/tst/dice_roll.rs
Normal file
17
01.workspace/oxydice_lib/src/tst/dice_roll.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
11
01.workspace/oxydice_lib/src/tst/die_roll.rs
Normal file
11
01.workspace/oxydice_lib/src/tst/die_roll.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
pub mod dice_grab;
|
||||
pub mod die_roll;
|
||||
pub mod dice_roll;
|
||||
|
||||
Reference in New Issue
Block a user