feat: add keep_highest method to Outcome
This commit is contained in:
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!();
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
use oxidice_lib::dice::*;
|
use oxidice_lib::dice::*;
|
||||||
|
|
||||||
fn main(){
|
fn main() {
|
||||||
let roll = Handful::grab_exploding(1, 6, 6).roll();
|
let roll = Handful::grab_exploding(1, 6, 6).roll();
|
||||||
println!("{roll}");
|
println!("{roll}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
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