Compare commits

...

2 Commits

Author SHA1 Message Date
c056b4f269 doc: add roll 4d6 keep 3 lowest example 2025-11-19 10:27:30 +01:00
3a3c1487bb feat: add keep_lowest method to Outcome 2025-11-19 10:19:47 +01:00
3 changed files with 32 additions and 0 deletions

View 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}");
}

View File

@@ -2,6 +2,7 @@ pub mod die_roll;
pub mod handful_grab; pub mod handful_grab;
pub mod handful_roll; pub mod handful_roll;
pub mod outcome_keep_highest; pub mod outcome_keep_highest;
pub mod outcome_keep_lowest;
pub mod outcome_max; pub mod outcome_max;
pub mod outcome_min; pub mod outcome_min;
pub mod outcome_sum; pub mod outcome_sum;

View 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::*; }