Compare commits
7 Commits
f1dfbb0fff
...
c6018d7199
| Author | SHA1 | Date | |
|---|---|---|---|
| c6018d7199 | |||
| 07a2913c76 | |||
| dcf97956c2 | |||
| 3e5afffb33 | |||
| 95766fb897 | |||
| 430cd675be | |||
| a9f9bcac09 |
2
01.workspace/Cargo.lock
generated
2
01.workspace/Cargo.lock
generated
@@ -27,7 +27,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
|
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "oxydice_lib"
|
name = "oxidice_lib"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rand",
|
"rand",
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
members = ["oxydice_lib"]
|
members = ["oxidice_lib"]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "oxydice_lib"
|
name = "oxidice_lib"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
17
01.workspace/oxidice_lib/examples/roll_ose_stats.rs
Normal file
17
01.workspace/oxidice_lib/examples/roll_ose_stats.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
|
fn main() -> Result<(), FailedTo>{
|
||||||
|
let str = Dice::grab(3, 6).roll().sum()?;
|
||||||
|
let int = Dice::grab(3, 6).roll().sum()?;
|
||||||
|
let wis = Dice::grab(3, 6).roll().sum()?;
|
||||||
|
let dex = Dice::grab(3, 6).roll().sum()?;
|
||||||
|
let con = Dice::grab(3, 6).roll().sum()?;
|
||||||
|
let cha = Dice::grab(3, 6).roll().sum()?;
|
||||||
|
println!("STR: {str}");
|
||||||
|
println!("INT: {int}");
|
||||||
|
println!("WIS: {wis}");
|
||||||
|
println!("DEX: {dex}");
|
||||||
|
println!("CON: {con}");
|
||||||
|
println!("CHA: {cha}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ impl Dice {
|
|||||||
}
|
}
|
||||||
Self {
|
Self {
|
||||||
handful,
|
handful,
|
||||||
..Self::default()
|
roll_results: Vec::<u16>::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn and_grab(mut self, dice: u16, sides: u16) -> Self {
|
pub fn and_grab(mut self, dice: u16, sides: u16) -> Self {
|
||||||
11
01.workspace/oxidice_lib/src/imp/dice_roll.rs
Normal file
11
01.workspace/oxidice_lib/src/imp/dice_roll.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl Dice {
|
||||||
|
pub fn roll(mut self) -> Self {
|
||||||
|
self.roll_results.clear();
|
||||||
|
for die in &self.handful {
|
||||||
|
self.roll_results.push(die.roll())
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
16
01.workspace/oxidice_lib/src/imp/dice_sum.rs
Normal file
16
01.workspace/oxidice_lib/src/imp/dice_sum.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl Dice {
|
||||||
|
pub fn sum(self) -> Result<Sum, FailedTo> {
|
||||||
|
let mut result = Sum::default();
|
||||||
|
for roll_result in self.roll_results {
|
||||||
|
result.roll_results.push(roll_result);
|
||||||
|
result.sum = result
|
||||||
|
.sum
|
||||||
|
.checked_add(roll_result.into())
|
||||||
|
.and_then(|r| r.checked_add(1u64))
|
||||||
|
.ok_or(FailedTo::Sum)?;
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,3 +6,6 @@ impl Die {
|
|||||||
rng.random_range(0..self.sides)
|
rng.random_range(0..self.sides)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests { use super::*; }
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
pub mod dice_grab;
|
pub mod dice_grab;
|
||||||
pub mod dice_roll;
|
pub mod dice_roll;
|
||||||
pub mod die_new;
|
pub mod dice_sum;
|
||||||
pub mod die_roll;
|
pub mod die_roll;
|
||||||
|
pub mod sum_display;
|
||||||
17
01.workspace/oxidice_lib/src/imp/sum_display.rs
Normal file
17
01.workspace/oxidice_lib/src/imp/sum_display.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl fmt::Display for Sum {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
|
let str_results: Vec<String> = self
|
||||||
|
.roll_results
|
||||||
|
.iter()
|
||||||
|
.map(|r| r.checked_add(1))
|
||||||
|
.map(|o| o.unwrap_or(0))
|
||||||
|
.map(|r| r.to_string())
|
||||||
|
.collect();
|
||||||
|
write!(f, "[{}]", str_results.join(", "))?;
|
||||||
|
write!(f, " -> ")?;
|
||||||
|
write!(f, "{}", self.sum)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,8 +11,11 @@ mod tst;
|
|||||||
|
|
||||||
pub(crate) use crate::str::dice::O as Dice;
|
pub(crate) use crate::str::dice::O as Dice;
|
||||||
pub(crate) use crate::str::die::O as Die;
|
pub(crate) use crate::str::die::O as Die;
|
||||||
pub(crate) use crate::str::roll_result::O as RollResult;
|
pub(crate) use crate::str::failed_to::E as FailedTo;
|
||||||
|
pub(crate) use crate::str::sum::O as Sum;
|
||||||
|
|
||||||
pub mod dice {
|
pub mod dice {
|
||||||
pub use crate::str::dice::O as Dice;
|
pub use crate::str::dice::O as Dice;
|
||||||
|
pub use crate::str::sum::O as Sum;
|
||||||
|
pub use crate::str::failed_to::E as FailedTo;
|
||||||
}
|
}
|
||||||
18
01.workspace/oxidice_lib/src/str/dice.rs
Normal file
18
01.workspace/oxidice_lib/src/str/dice.rs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
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 {
|
||||||
|
// fn fmt(&self, _f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
||||||
|
// todo!();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests {
|
||||||
|
// use super::*;
|
||||||
|
// }
|
||||||
@@ -4,3 +4,9 @@ use crate::*;
|
|||||||
pub struct O {
|
pub struct O {
|
||||||
pub(crate) sides: u16,
|
pub(crate) sides: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Die {
|
||||||
|
pub fn new(sides: u16) -> Self {
|
||||||
|
Self { sides }
|
||||||
|
}
|
||||||
|
}
|
||||||
51
01.workspace/oxidice_lib/src/str/failed_to.rs
Normal file
51
01.workspace/oxidice_lib/src/str/failed_to.rs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
// use crate::*;
|
||||||
|
// use std::fmt::Display;
|
||||||
|
// use std::str::FromStr;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
|
||||||
|
pub enum E {
|
||||||
|
Sum,
|
||||||
|
}
|
||||||
|
|
||||||
|
// impl Display for E {
|
||||||
|
// fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
|
||||||
|
// match self {
|
||||||
|
// Self::NoValue => write!(f, "NoValue"),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// impl FromStr for E {
|
||||||
|
// type Err = Box<dyn std::error::Error>;
|
||||||
|
// fn from_str(value: &str) -> std::result::Result<Self, Box<dyn std::error::Error>> {
|
||||||
|
// match value {
|
||||||
|
// "NoValue" => Ok(Self::NoValue),
|
||||||
|
// _ => unreachable!(),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests {
|
||||||
|
// use super::*;
|
||||||
|
// #[test]
|
||||||
|
// fn check_001() {
|
||||||
|
// for value in [E::NoValue] {
|
||||||
|
// match value {
|
||||||
|
// E::NoValue => assert_eq!(
|
||||||
|
// E::NoValue,
|
||||||
|
// E::from_str("NoValue").unwrap()
|
||||||
|
// ),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn check_002() {
|
||||||
|
// for value in [E::NoValue] {
|
||||||
|
// match value {
|
||||||
|
// E::NoValue => assert_eq!(&E::NoValue.to_string(), "NoValue"),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
4
01.workspace/oxidice_lib/src/str/mod.rs
Normal file
4
01.workspace/oxidice_lib/src/str/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
pub mod dice;
|
||||||
|
pub mod die;
|
||||||
|
pub mod failed_to;
|
||||||
|
pub mod sum;
|
||||||
18
01.workspace/oxidice_lib/src/str/sum.rs
Normal file
18
01.workspace/oxidice_lib/src/str/sum.rs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Default, PartialEq, Clone)]
|
||||||
|
pub struct O {
|
||||||
|
pub(crate) roll_results: Vec<u16>,
|
||||||
|
pub(crate) sum: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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::*;
|
||||||
|
// }
|
||||||
28
01.workspace/oxidice_lib/src/tst/dice_grab.rs
Normal file
28
01.workspace/oxidice_lib/src/tst/dice_grab.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
01.workspace/oxidice_lib/src/tst/dice_roll.rs
Normal file
15
01.workspace/oxidice_lib/src/tst/dice_roll.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::*;
|
||||||
|
#[test]
|
||||||
|
pub fn roll_should_yield_correct_number_or_results() {
|
||||||
|
let dice = Dice::grab(3, 6);
|
||||||
|
let dice = dice.roll();
|
||||||
|
assert_eq!(dice.roll_results.len(), 3);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn reroll_should_yield_correct_number_of_results() {
|
||||||
|
let dice = Dice::grab(3, 6).roll().roll();
|
||||||
|
assert_eq!(dice.roll_results.len(), 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
16
01.workspace/oxidice_lib/src/tst/dice_sum.rs
Normal file
16
01.workspace/oxidice_lib/src/tst/dice_sum.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::*;
|
||||||
|
#[test]
|
||||||
|
pub fn dice_sum_should_yield_the_correct_sum() {
|
||||||
|
let result = Dice::grab(6, 1).roll().sum().unwrap();
|
||||||
|
assert_eq!(result.sum, 6);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn dice_sum_should_list_the_correct_results() {
|
||||||
|
let result = Dice::grab(6, 1).roll().sum().unwrap();
|
||||||
|
for idx in 1..6 {
|
||||||
|
assert_eq!(result.roll_results[idx], 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
01.workspace/oxidice_lib/src/tst/die_roll.rs
Normal file
11
01.workspace/oxidice_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
01.workspace/oxidice_lib/src/tst/mod.rs
Normal file
5
01.workspace/oxidice_lib/src/tst/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pub mod dice_grab;
|
||||||
|
pub mod dice_roll;
|
||||||
|
pub mod dice_sum;
|
||||||
|
pub mod die_roll;
|
||||||
|
pub mod sum_display;
|
||||||
9
01.workspace/oxidice_lib/src/tst/sum_display.rs
Normal file
9
01.workspace/oxidice_lib/src/tst/sum_display.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::*;
|
||||||
|
#[test]
|
||||||
|
pub fn sum_display_format_should_be_correct() {
|
||||||
|
let result = Dice::grab(3, 1).roll().sum().unwrap();
|
||||||
|
assert_eq!(&result.to_string(), "[1, 1, 1] -> 3");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
use oxydice_lib::*;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let mut dice = dice::Dice::grab(3, 6);
|
|
||||||
dice.roll();
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
use crate::*;
|
|
||||||
|
|
||||||
impl Die {
|
|
||||||
pub fn new(sides: u16) -> Die {
|
|
||||||
Self { sides }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
use crate::*;
|
|
||||||
|
|
||||||
#[derive(Debug, Default, PartialEq, Clone)]
|
|
||||||
pub struct O {
|
|
||||||
pub(crate) handful: Vec<Die>,
|
|
||||||
pub(crate) result: RollResult,
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
pub mod dice;
|
|
||||||
pub mod die;
|
|
||||||
pub mod roll_result;
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
use crate::*;
|
|
||||||
|
|
||||||
#[derive(Debug, Default, PartialEq, Clone)]
|
|
||||||
pub struct O {
|
|
||||||
pub(crate) die_results: Vec<u16>,
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
#[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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
#[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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
pub mod dice_grab;
|
|
||||||
pub mod dice_roll;
|
|
||||||
Reference in New Issue
Block a user