Compare commits
31 Commits
c6018d7199
...
next
| Author | SHA1 | Date | |
|---|---|---|---|
| c056b4f269 | |||
| 3a3c1487bb | |||
| d29fa81fbb | |||
| 8c45bf629c | |||
| 5f10a57d35 | |||
| f5e5ee2c91 | |||
| 98249789ee | |||
| 8247fe9241 | |||
| 2ed6d79b29 | |||
| 23185bfd86 | |||
| 4d4810b52b | |||
| e12e954d19 | |||
| 331fd4d93a | |||
| a8fbfabbcc | |||
| 1688b2acc2 | |||
| 32eafd26bc | |||
| 768f021a46 | |||
| 87b837aa45 | |||
| 8620cc124a | |||
| 85194b3f0f | |||
| 0eddb019dd | |||
| a6c44bce22 | |||
| 201cafe329 | |||
| 00b0463465 | |||
| 11a6f4c592 | |||
| a0f3d98fa2 | |||
| 13d77f136e | |||
| 97a0fbb163 | |||
| 85824b87ff | |||
| e0fe0786ac | |||
| 9aac3d6bed |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
01.workspace/target
|
||||||
148
01.workspace/Cargo.lock
generated
Normal file
148
01.workspace/Cargo.lock
generated
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.3.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"r-efi",
|
||||||
|
"wasip2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.177"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "oxidice_lib"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.103"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.42"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "r-efi"
|
||||||
|
version = "5.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.9.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
|
||||||
|
dependencies = [
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.9.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.110"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a99801b5bd34ede4cf3fc688c5919368fea4e4814a4664359503e6015b280aea"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.22"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasip2"
|
||||||
|
version = "1.0.1+wasi-0.2.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7"
|
||||||
|
dependencies = [
|
||||||
|
"wit-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wit-bindgen"
|
||||||
|
version = "0.46.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy"
|
||||||
|
version = "0.8.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0894878a5fa3edfd6da3f88c4805f4c8558e2b996227a3d864f47fe11e38282c"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy-derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy-derive"
|
||||||
|
version = "0.8.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
5
01.workspace/Cargo.toml
Normal file
5
01.workspace/Cargo.toml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
[workspace]
|
||||||
|
|
||||||
|
resolver = "3"
|
||||||
|
|
||||||
|
members = ["oxidice_lib"]
|
||||||
7
01.workspace/oxidice_lib/Cargo.toml
Normal file
7
01.workspace/oxidice_lib/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "oxidice_lib"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.9.2"
|
||||||
6
01.workspace/oxidice_lib/examples/roll_3d6.rs
Normal file
6
01.workspace/oxidice_lib/examples/roll_3d6.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let outcome = Handful::grab(3, 6).roll();
|
||||||
|
println!("{outcome}");
|
||||||
|
}
|
||||||
7
01.workspace/oxidice_lib/examples/roll_4D_Fudge.rs
Normal file
7
01.workspace/oxidice_lib/examples/roll_4D_Fudge.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let roll = Handful::grab_range(4, -1..=1).roll();
|
||||||
|
let sum = roll.clone().sum().unwrap();
|
||||||
|
println!("{} -> {}", roll, sum);
|
||||||
|
}
|
||||||
11
01.workspace/oxidice_lib/examples/roll_4d6_keep_3.rs
Normal file
11
01.workspace/oxidice_lib/examples/roll_4d6_keep_3.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let roll = Handful::grab(4, 6).roll();
|
||||||
|
print!("{roll} -> ");
|
||||||
|
let roll = roll.keep_highest(3).unwrap();
|
||||||
|
print!("{roll} -> ");
|
||||||
|
let roll = roll.sum().unwrap();
|
||||||
|
print!("{roll}");
|
||||||
|
println!();
|
||||||
|
}
|
||||||
10
01.workspace/oxidice_lib/examples/roll_4d6_keep_3_lowest.rs
Normal file
10
01.workspace/oxidice_lib/examples/roll_4d6_keep_3_lowest.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let result = Handful::grab(4, 6).roll();
|
||||||
|
print!("{result} -> ");
|
||||||
|
let result = result.keep_lowest(3).unwrap();
|
||||||
|
print!("{result} -> ");
|
||||||
|
let result = result.sum().unwrap();
|
||||||
|
println!("{result}");
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let roll_result = Handful::grab(2, 20).roll();
|
||||||
|
let result = roll_result.clone().max().unwrap();
|
||||||
|
println!("{} -> {}", roll_result, result);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let roll_result = Handful::grab(2, 20).roll();
|
||||||
|
let result = roll_result.clone().min().unwrap();
|
||||||
|
println!("{} -> {}", roll_result, result);
|
||||||
|
}
|
||||||
6
01.workspace/oxidice_lib/examples/roll_exploding_d6.rs
Normal file
6
01.workspace/oxidice_lib/examples/roll_exploding_d6.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let roll = Handful::grab_exploding(1, 6, 6).roll();
|
||||||
|
println!("{roll}");
|
||||||
|
}
|
||||||
16
01.workspace/oxidice_lib/examples/roll_ose_stats.rs
Normal file
16
01.workspace/oxidice_lib/examples/roll_ose_stats.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let str = Handful::grab(3, 6).roll().sum().unwrap();
|
||||||
|
let int = Handful::grab(3, 6).roll().sum().unwrap();
|
||||||
|
let wis = Handful::grab(3, 6).roll().sum().unwrap();
|
||||||
|
let dex = Handful::grab(3, 6).roll().sum().unwrap();
|
||||||
|
let con = Handful::grab(3, 6).roll().sum().unwrap();
|
||||||
|
let cha = Handful::grab(3, 6).roll().sum().unwrap();
|
||||||
|
println!("STR: {}", str);
|
||||||
|
println!("INT: {}", int);
|
||||||
|
println!("WIS: {}", wis);
|
||||||
|
println!("DEX: {}", dex);
|
||||||
|
println!("CON: {}", con);
|
||||||
|
println!("CHA: {}", cha);
|
||||||
|
}
|
||||||
1
01.workspace/oxidice_lib/src/fun/mod.rs
Normal file
1
01.workspace/oxidice_lib/src/fun/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
47
01.workspace/oxidice_lib/src/imp/die_roll.rs
Normal file
47
01.workspace/oxidice_lib/src/imp/die_roll.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
use crate::*;
|
||||||
|
use std::ops::*;
|
||||||
|
|
||||||
|
fn roll_sides(sides: u16) -> Vec<i32> {
|
||||||
|
let mut rng = rand::rng();
|
||||||
|
vec![rng.random_range(1..=sides as i32)]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn roll_range(range: RangeInclusive<i32>) -> Vec<i32> {
|
||||||
|
let mut rng = rand::rng();
|
||||||
|
vec![rng.random_range(range)]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn roll_exploding(sides: u16, threshold: i32) -> Vec<i32> {
|
||||||
|
let mut rng = rand::rng();
|
||||||
|
let mut results = Vec::<i32>::new();
|
||||||
|
loop {
|
||||||
|
let result: i32 = rng.random_range(1..=sides as i32);
|
||||||
|
results.push(result);
|
||||||
|
if result < threshold {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
results
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Die {
|
||||||
|
pub fn roll(&self) -> Vec<i32> {
|
||||||
|
match self {
|
||||||
|
Die::Exploding(sides, threshold) => roll_exploding(*sides, *threshold),
|
||||||
|
Die::Range(range) => roll_range(range.clone()),
|
||||||
|
Die::Sides(sides) => roll_sides(*sides),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn check_roll_range() {
|
||||||
|
let die = Die::Range(5..=5);
|
||||||
|
for _ in 1..=1000 {
|
||||||
|
assert_eq!(die.roll(), [5]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
01.workspace/oxidice_lib/src/imp/handful_grab.rs
Normal file
26
01.workspace/oxidice_lib/src/imp/handful_grab.rs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
use crate::*;
|
||||||
|
use std::ops::*;
|
||||||
|
|
||||||
|
impl Handful {
|
||||||
|
pub fn grab(dice_num: u16, sides: u16) -> Self {
|
||||||
|
let mut dice = Vec::<Die>::new();
|
||||||
|
for _ in 1..=dice_num {
|
||||||
|
dice.push(Die::Sides(sides));
|
||||||
|
}
|
||||||
|
Self { dice }
|
||||||
|
}
|
||||||
|
pub fn grab_range(dice_num: u16, range: RangeInclusive<i32>) -> Self {
|
||||||
|
let mut dice = Vec::<Die>::new();
|
||||||
|
for _ in 1..=dice_num {
|
||||||
|
dice.push(Die::Range(range.clone()));
|
||||||
|
}
|
||||||
|
Self { dice }
|
||||||
|
}
|
||||||
|
pub fn grab_exploding(dice_num: u16, sides: u16, threshold: i32) -> Self {
|
||||||
|
let mut dice = Vec::<Die>::new();
|
||||||
|
for _ in 1..=dice_num {
|
||||||
|
dice.push(Die::Exploding(sides, threshold));
|
||||||
|
}
|
||||||
|
Self { dice }
|
||||||
|
}
|
||||||
|
}
|
||||||
14
01.workspace/oxidice_lib/src/imp/handful_roll.rs
Normal file
14
01.workspace/oxidice_lib/src/imp/handful_roll.rs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl Handful {
|
||||||
|
pub fn roll(self) -> Outcome {
|
||||||
|
let mut die_rolls = Vec::<i32>::new();
|
||||||
|
for die in self.dice {
|
||||||
|
die_rolls.append(&mut die.roll());
|
||||||
|
}
|
||||||
|
Outcome::List(die_rolls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests { use super::*; }
|
||||||
8
01.workspace/oxidice_lib/src/imp/mod.rs
Normal file
8
01.workspace/oxidice_lib/src/imp/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
pub mod die_roll;
|
||||||
|
pub mod handful_grab;
|
||||||
|
pub mod handful_roll;
|
||||||
|
pub mod outcome_keep_highest;
|
||||||
|
pub mod outcome_keep_lowest;
|
||||||
|
pub mod outcome_max;
|
||||||
|
pub mod outcome_min;
|
||||||
|
pub mod outcome_sum;
|
||||||
22
01.workspace/oxidice_lib/src/imp/outcome_keep_highest.rs
Normal file
22
01.workspace/oxidice_lib/src/imp/outcome_keep_highest.rs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
use crate::*;
|
||||||
|
use std::cmp::*;
|
||||||
|
|
||||||
|
impl Outcome {
|
||||||
|
pub fn keep_highest(self, dice_num: u16) -> Result<Outcome, FailedTo> {
|
||||||
|
if dice_num == 0 {
|
||||||
|
return Err(FailedTo::ProcessInput);
|
||||||
|
}
|
||||||
|
match self {
|
||||||
|
Outcome::Scalar(value) => Ok(Outcome::List(vec![value])),
|
||||||
|
Outcome::List(mut values) => {
|
||||||
|
values.sort_by_key(|&num| Reverse(num));
|
||||||
|
Ok(Outcome::List(
|
||||||
|
values.into_iter().take(dice_num as usize).collect(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests { use super::*; }
|
||||||
21
01.workspace/oxidice_lib/src/imp/outcome_keep_lowest.rs
Normal file
21
01.workspace/oxidice_lib/src/imp/outcome_keep_lowest.rs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
impl Outcome {
|
||||||
|
pub fn keep_lowest(self, dice_num: u16) -> Result<Outcome, FailedTo> {
|
||||||
|
if dice_num == 0 {
|
||||||
|
return Err(FailedTo::ProcessInput);
|
||||||
|
}
|
||||||
|
match self {
|
||||||
|
Outcome::Scalar(value) => Ok(Outcome::List(vec![value])),
|
||||||
|
Outcome::List(mut values) => {
|
||||||
|
values.sort();
|
||||||
|
Ok(Outcome::List(
|
||||||
|
values.into_iter().take(dice_num as usize).collect(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// #[cfg(test)]
|
||||||
|
// mod unit_tests { use super::*; }
|
||||||
41
01.workspace/oxidice_lib/src/imp/outcome_max.rs
Normal file
41
01.workspace/oxidice_lib/src/imp/outcome_max.rs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
fn max_of(values: Vec<i32>) -> Result<Outcome, FailedTo> {
|
||||||
|
if values.is_empty() {
|
||||||
|
return Err(FailedTo::ProcessInput);
|
||||||
|
}
|
||||||
|
let ret = Ok(Outcome::Scalar(i32::MIN));
|
||||||
|
ret.and_then(|_| {
|
||||||
|
values
|
||||||
|
.iter()
|
||||||
|
.max()
|
||||||
|
.ok_or(FailedTo::FindMax)
|
||||||
|
.map(|min| Outcome::Scalar(*min))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Outcome {
|
||||||
|
pub fn max(self) -> Result<Outcome, FailedTo> {
|
||||||
|
match self {
|
||||||
|
Outcome::Scalar(value) => Ok(Outcome::Scalar(value)),
|
||||||
|
Outcome::List(values) => max_of(values),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod unit_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn check_max() {
|
||||||
|
let outcome = Handful::grab(2, 20).roll();
|
||||||
|
let max = outcome.clone().max().unwrap();
|
||||||
|
match outcome {
|
||||||
|
Outcome::List(values) => {
|
||||||
|
let expected_max = values.iter().max().unwrap();
|
||||||
|
assert_eq!(max, Outcome::Scalar(*expected_max));
|
||||||
|
}
|
||||||
|
_ => panic!("outcome is not a list"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
40
01.workspace/oxidice_lib/src/imp/outcome_min.rs
Normal file
40
01.workspace/oxidice_lib/src/imp/outcome_min.rs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
fn min_of(values: Vec<i32>) -> Result<Outcome, FailedTo> {
|
||||||
|
if values.is_empty() {
|
||||||
|
return Err(FailedTo::ProcessInput);
|
||||||
|
}
|
||||||
|
Ok(Outcome::Scalar(i32::MIN)).and_then(|_| {
|
||||||
|
values
|
||||||
|
.iter()
|
||||||
|
.min()
|
||||||
|
.ok_or(FailedTo::FindMin)
|
||||||
|
.map(|min| Outcome::Scalar(*min))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Outcome {
|
||||||
|
pub fn min(self) -> Result<Outcome, FailedTo> {
|
||||||
|
match self {
|
||||||
|
Outcome::Scalar(value) => Ok(Outcome::Scalar(value)),
|
||||||
|
Outcome::List(values) => min_of(values),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod unit_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn check_min() {
|
||||||
|
let roll_result = Handful::grab(2, 20).roll();
|
||||||
|
let min_result = roll_result.clone().min().unwrap();
|
||||||
|
match roll_result {
|
||||||
|
Outcome::List(values) => {
|
||||||
|
let min = values.iter().min().unwrap();
|
||||||
|
assert_eq!(min_result, Outcome::Scalar(*min));
|
||||||
|
}
|
||||||
|
_ => panic!("result is not a list"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
37
01.workspace/oxidice_lib/src/imp/outcome_sum.rs
Normal file
37
01.workspace/oxidice_lib/src/imp/outcome_sum.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
fn sum_of(values: Vec<i32>) -> Result<Outcome, FailedTo> {
|
||||||
|
if values.is_empty() {
|
||||||
|
return Ok(Outcome::Scalar(0));
|
||||||
|
}
|
||||||
|
let mut ret = Ok(Outcome::Scalar(0));
|
||||||
|
for value in values {
|
||||||
|
ret = ret.and_then(|outcome: Outcome| match outcome {
|
||||||
|
Outcome::List(_) => Err(FailedTo::ProcessInput),
|
||||||
|
Outcome::Scalar(acc) => acc
|
||||||
|
.checked_add(value)
|
||||||
|
.map(|new_acc: i32| Outcome::Scalar(new_acc))
|
||||||
|
.ok_or(FailedTo::SumValues),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Outcome {
|
||||||
|
pub fn sum(self) -> Result<Outcome, FailedTo> {
|
||||||
|
match self {
|
||||||
|
Outcome::Scalar(value) => Ok(Outcome::Scalar(value)),
|
||||||
|
Outcome::List(values) => sum_of(values),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod unit_tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn check_sum() {
|
||||||
|
let outcome = Handful::grab(5, 1).roll().sum();
|
||||||
|
assert_eq!(outcome, Ok(Outcome::Scalar(5)));
|
||||||
|
}
|
||||||
|
}
|
||||||
19
01.workspace/oxidice_lib/src/lib.rs
Normal file
19
01.workspace/oxidice_lib/src/lib.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
use rand::*;
|
||||||
|
use std::*;
|
||||||
|
|
||||||
|
mod fun;
|
||||||
|
mod imp;
|
||||||
|
mod mcr;
|
||||||
|
mod str;
|
||||||
|
mod trt;
|
||||||
|
mod tst;
|
||||||
|
|
||||||
|
pub(crate) use crate::str::die::E as Die;
|
||||||
|
pub(crate) use crate::str::failed_to::E as FailedTo;
|
||||||
|
pub(crate) use crate::str::handful::O as Handful;
|
||||||
|
pub(crate) use crate::str::outcome::E as Outcome;
|
||||||
|
|
||||||
|
pub mod dice {
|
||||||
|
pub use crate::str::handful::O as Handful;
|
||||||
|
pub use crate::str::outcome::E as Outcome;
|
||||||
|
}
|
||||||
1
01.workspace/oxidice_lib/src/mcr/mod.rs
Normal file
1
01.workspace/oxidice_lib/src/mcr/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
54
01.workspace/oxidice_lib/src/str/die.rs
Normal file
54
01.workspace/oxidice_lib/src/str/die.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
use crate::*;
|
||||||
|
use std::ops::*;
|
||||||
|
// use std::fmt::Display;
|
||||||
|
// use std::str::FromStr;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
||||||
|
pub enum E {
|
||||||
|
Exploding(u16, i32),
|
||||||
|
Range(RangeInclusive<i32>),
|
||||||
|
Sides(u16),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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"),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
54
01.workspace/oxidice_lib/src/str/failed_to.rs
Normal file
54
01.workspace/oxidice_lib/src/str/failed_to.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
// use crate::*;
|
||||||
|
// use std::fmt::Display;
|
||||||
|
// use std::str::FromStr;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
|
||||||
|
pub enum E {
|
||||||
|
FindMax,
|
||||||
|
FindMin,
|
||||||
|
ProcessInput,
|
||||||
|
SumValues,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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"),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
17
01.workspace/oxidice_lib/src/str/handful.rs
Normal file
17
01.workspace/oxidice_lib/src/str/handful.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Default, PartialEq, Clone)]
|
||||||
|
pub struct O {
|
||||||
|
pub(crate) dice: 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::*;
|
||||||
|
// }
|
||||||
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 die;
|
||||||
|
pub mod failed_to;
|
||||||
|
pub mod handful;
|
||||||
|
pub mod outcome;
|
||||||
60
01.workspace/oxidice_lib/src/str/outcome.rs
Normal file
60
01.workspace/oxidice_lib/src/str/outcome.rs
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
use crate::*;
|
||||||
|
use std::fmt::Display;
|
||||||
|
// use std::str::FromStr;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Hash)]
|
||||||
|
pub enum E {
|
||||||
|
Scalar(i32),
|
||||||
|
List(Vec<i32>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for E {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
|
||||||
|
match self {
|
||||||
|
Self::Scalar(value) => write!(f, "{}", value),
|
||||||
|
Self::List(value) => {
|
||||||
|
let str_list = value
|
||||||
|
.iter()
|
||||||
|
.map(|item| item.to_string())
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(", ");
|
||||||
|
write!(f, "[{}]", str_list)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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"),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
1
01.workspace/oxidice_lib/src/trt/mod.rs
Normal file
1
01.workspace/oxidice_lib/src/trt/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
1
01.workspace/oxidice_lib/src/tst/mod.rs
Normal file
1
01.workspace/oxidice_lib/src/tst/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
Reference in New Issue
Block a user