feat: add keep_highest method to Outcome

This commit is contained in:
2025-11-16 11:08:20 +01:00
parent 8c45bf629c
commit d29fa81fbb
4 changed files with 35 additions and 1 deletions

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

View File

@@ -1,6 +1,7 @@
pub mod die_roll; 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_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,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::*; }