feat: add keep_highest method to Outcome
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
pub mod die_roll;
|
||||
pub mod handful_grab;
|
||||
pub mod handful_roll;
|
||||
pub mod outcome_keep_highest;
|
||||
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::*; }
|
||||
Reference in New Issue
Block a user