Keep/Drop Function #92

Manually merged
kg333 merged 12 commits from kg333/tenebrous-dicebot:keep_drop_function into master 2021-09-26 14:05:57 +00:00
2 changed files with 40 additions and 20 deletions
Showing only changes of commit dc242182f4 - Show all commits

View File

@ -41,19 +41,21 @@ fn parse_dice(input: &str) -> IResult<&str, Dice> {
// if ok, keep expression is present // if ok, keep expression is present
Ok(r) => { Ok(r) => {
kg333 marked this conversation as resolved Outdated

Rust supports tuple destructuring in pattern matching, which may be useful here and the other match:

Ok((rest, (_, keep_amount)) => ...

rest being the rest of the input in this case.

Rust supports tuple destructuring in pattern matching, which may be useful here and the other match: ```rust Ok((rest, (_, keep_amount)) => ... ``` `rest` being the rest of the input in this case.
Outdated
Review

Ooh, that's much more human-readable, thanks!

Ooh, that's much more human-readable, thanks!
input = r.0; input = r.0;
// don't allow keep greater than number of dice, and don't allow keep zero keep = r.1.1;
if r.1.1 <= count && r.1.1 != "0" {
keep = r.1.1;
} else {
keep = count;
}
} }
// otherwise absent and keep all dice // otherwise absent and keep all dice
Err(_) => keep = count, Err(_) => keep = count,
}; };
// don't allow keep greater than number of dice, and don't allow keep zero
let mut keep: u32 = keep.parse().unwrap();
let count: u32 = count.parse().unwrap();
if keep > count || keep == 0 {
keep = count;
}
Ok(( Ok((
input, input,
Dice::new(count.parse().unwrap(), sides.parse().unwrap(), keep.parse().unwrap()), Dice::new(count, sides.parse().unwrap(), keep),
)) ))
} }
kg333 marked this conversation as resolved Outdated

You can make this a bit more concise with yet another match expression:

Ok(i) => match i {
    _i if _i > count || _i == 0 => KeepOrDrop::Keep(count),
    i => KeepOrDrop::Keep(i)
}

This is mostly a stylistic choice though.

You can make this a bit more concise with yet another match expression: ```rust Ok(i) => match i { _i if _i > count || _i == 0 => KeepOrDrop::Keep(count), i => KeepOrDrop::Keep(i) } ``` This is mostly a stylistic choice though.
@ -130,6 +132,10 @@ mod tests {
assert_eq!(parse_dice("8d7"), Ok(("", Dice::new(8, 7, 8)))); assert_eq!(parse_dice("8d7"), Ok(("", Dice::new(8, 7, 8))));
assert_eq!(parse_dice("2d20k1"), Ok(("", Dice::new(2, 20, 1)))); assert_eq!(parse_dice("2d20k1"), Ok(("", Dice::new(2, 20, 1))));
assert_eq!(parse_dice("100d10k90"), Ok(("", Dice::new(100, 10, 90)))); assert_eq!(parse_dice("100d10k90"), Ok(("", Dice::new(100, 10, 90))));
assert_eq!(parse_dice("11d10k10"), Ok(("", Dice::new(11, 10, 10))));
assert_eq!(parse_dice("12d10k11"), Ok(("", Dice::new(12, 10, 11))));
assert_eq!(parse_dice("12d10k13"), Ok(("", Dice::new(12, 10, 12))));
assert_eq!(parse_dice("12d10k0"), Ok(("", Dice::new(12, 10, 12))));
} }
#[test] #[test]

View File

@ -33,7 +33,7 @@ impl DiceRoll {
// only count kept dice in total // only count kept dice in total
pub fn total(&self) -> u32 { pub fn total(&self) -> u32 {
self.0[..=(self.1-1)].iter().sum() self.0[..self.1].iter().sum()
} }
} }
@ -211,18 +211,22 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn dice_roll_display_test() { fn dice_roll_display_test() {
assert_eq!(DiceRoll(vec![1, 3, 4]).to_string(), "8 (1 + 3 + 4)"); assert_eq!(DiceRoll(vec![1, 3, 4], 3).to_string(), "8 (1 + 3 + 4)");
assert_eq!(DiceRoll(vec![]).to_string(), "0"); assert_eq!(DiceRoll(vec![], 0).to_string(), "0");
assert_eq!( assert_eq!(
DiceRoll(vec![4, 7, 2, 10]).to_string(), DiceRoll(vec![4, 7, 2, 10], 4).to_string(),
"23 (4 + 7 + 2 + 10)" "23 (4 + 7 + 2 + 10)"
); );
assert_eq!(
DiceRoll(vec![20, 13, 11, 10], 3).to_string(),
"44 (20 + 13 + 11 + [10])"
);
} }
#[test] #[test]
fn element_roll_display_test() { fn element_roll_display_test() {
assert_eq!( assert_eq!(
ElementRoll::Dice(DiceRoll(vec![1, 3, 4])).to_string(), ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3)).to_string(),
"8 (1 + 3 + 4)" "8 (1 + 3 + 4)"
); );
assert_eq!(ElementRoll::Bonus(7).to_string(), "7"); assert_eq!(ElementRoll::Bonus(7).to_string(), "7");
@ -231,11 +235,11 @@ mod tests {
#[test] #[test]
fn signed_element_roll_display_test() { fn signed_element_roll_display_test() {
assert_eq!( assert_eq!(
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(), SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3))).to_string(),
"8 (1 + 3 + 4)" "8 (1 + 3 + 4)"
); );
assert_eq!( assert_eq!(
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))).to_string(), SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3))).to_string(),
"-8 (1 + 3 + 4)" "-8 (1 + 3 + 4)"
); );
assert_eq!( assert_eq!(
@ -252,14 +256,14 @@ mod tests {
fn element_expression_roll_display_test() { fn element_expression_roll_display_test() {
assert_eq!( assert_eq!(
ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Dice( ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Dice(
DiceRoll(vec![1, 3, 4]) DiceRoll(vec![1, 3, 4], 3)
)),]) )),])
.to_string(), .to_string(),
"8 (1 + 3 + 4)" "8 (1 + 3 + 4)"
); );
assert_eq!( assert_eq!(
ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Dice( ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Dice(
DiceRoll(vec![1, 3, 4]) DiceRoll(vec![1, 3, 4], 3)
)),]) )),])
.to_string(), .to_string(),
"-8 (1 + 3 + 4)" "-8 (1 + 3 + 4)"
@ -276,8 +280,8 @@ mod tests {
); );
assert_eq!( assert_eq!(
ElementExpressionRoll(vec![ ElementExpressionRoll(vec![
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))), SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3))),
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 2]))), SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 2], 2))),
SignedElementRoll::Positive(ElementRoll::Bonus(4)), SignedElementRoll::Positive(ElementRoll::Bonus(4)),
SignedElementRoll::Negative(ElementRoll::Bonus(7)), SignedElementRoll::Negative(ElementRoll::Bonus(7)),
]) ])
@ -286,13 +290,23 @@ mod tests {
); );
assert_eq!( assert_eq!(
ElementExpressionRoll(vec![ ElementExpressionRoll(vec![
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))), SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3))),
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 2]))), SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 2], 2))),
SignedElementRoll::Negative(ElementRoll::Bonus(4)), SignedElementRoll::Negative(ElementRoll::Bonus(4)),
SignedElementRoll::Positive(ElementRoll::Bonus(7)), SignedElementRoll::Positive(ElementRoll::Bonus(7)),
]) ])
.to_string(), .to_string(),
"-2 (-8 (1 + 3 + 4) + 3 (1 + 2) - 4 + 7)" "-2 (-8 (1 + 3 + 4) + 3 (1 + 2) - 4 + 7)"
); );
assert_eq!(
ElementExpressionRoll(vec![
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![4, 3, 1], 3))),
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![12, 2], 1))),
SignedElementRoll::Negative(ElementRoll::Bonus(4)),
SignedElementRoll::Positive(ElementRoll::Bonus(7)),
])
.to_string(),
"7 (-8 (4 + 3 + 1) + 12 (12 + [2]) - 4 + 7)"
);
} }
} }