Keep/Drop Function #92
|
@ -41,19 +41,21 @@ fn parse_dice(input: &str) -> IResult<&str, Dice> {
|
|||
// if ok, keep expression is present
|
||||
Ok(r) => {
|
||||
input = r.0;
|
||||
// don't allow keep greater than number of dice, and don't allow keep zero
|
||||
if r.1.1 <= count && r.1.1 != "0" {
|
||||
keep = r.1.1;
|
||||
} else {
|
||||
keep = count;
|
||||
}
|
||||
keep = r.1.1;
|
||||
}
|
||||
// otherwise absent and keep all dice
|
||||
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((
|
||||
input,
|
||||
Dice::new(count.parse().unwrap(), sides.parse().unwrap(), keep.parse().unwrap()),
|
||||
Dice::new(count, sides.parse().unwrap(), keep),
|
||||
))
|
||||
}
|
||||
|
||||
|
@ -130,6 +132,10 @@ mod tests {
|
|||
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("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]
|
||||
|
|
|
@ -33,7 +33,7 @@ impl DiceRoll {
|
|||
|
||||
// only count kept dice in total
|
||||
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::*;
|
||||
#[test]
|
||||
fn dice_roll_display_test() {
|
||||
assert_eq!(DiceRoll(vec![1, 3, 4]).to_string(), "8 (1 + 3 + 4)");
|
||||
assert_eq!(DiceRoll(vec![]).to_string(), "0");
|
||||
assert_eq!(DiceRoll(vec![1, 3, 4], 3).to_string(), "8 (1 + 3 + 4)");
|
||||
assert_eq!(DiceRoll(vec![], 0).to_string(), "0");
|
||||
assert_eq!(
|
||||
DiceRoll(vec![4, 7, 2, 10]).to_string(),
|
||||
DiceRoll(vec![4, 7, 2, 10], 4).to_string(),
|
||||
"23 (4 + 7 + 2 + 10)"
|
||||
);
|
||||
assert_eq!(
|
||||
DiceRoll(vec![20, 13, 11, 10], 3).to_string(),
|
||||
"44 (20 + 13 + 11 + [10])"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn element_roll_display_test() {
|
||||
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)"
|
||||
);
|
||||
assert_eq!(ElementRoll::Bonus(7).to_string(), "7");
|
||||
|
@ -231,11 +235,11 @@ mod tests {
|
|||
#[test]
|
||||
fn signed_element_roll_display_test() {
|
||||
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)"
|
||||
);
|
||||
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)"
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -252,14 +256,14 @@ mod tests {
|
|||
fn element_expression_roll_display_test() {
|
||||
assert_eq!(
|
||||
ElementExpressionRoll(vec![SignedElementRoll::Positive(ElementRoll::Dice(
|
||||
DiceRoll(vec![1, 3, 4])
|
||||
DiceRoll(vec![1, 3, 4], 3)
|
||||
)),])
|
||||
.to_string(),
|
||||
"8 (1 + 3 + 4)"
|
||||
);
|
||||
assert_eq!(
|
||||
ElementExpressionRoll(vec![SignedElementRoll::Negative(ElementRoll::Dice(
|
||||
DiceRoll(vec![1, 3, 4])
|
||||
DiceRoll(vec![1, 3, 4], 3)
|
||||
)),])
|
||||
.to_string(),
|
||||
"-8 (1 + 3 + 4)"
|
||||
|
@ -276,8 +280,8 @@ mod tests {
|
|||
);
|
||||
assert_eq!(
|
||||
ElementExpressionRoll(vec![
|
||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))),
|
||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 2]))),
|
||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3))),
|
||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 2], 2))),
|
||||
SignedElementRoll::Positive(ElementRoll::Bonus(4)),
|
||||
SignedElementRoll::Negative(ElementRoll::Bonus(7)),
|
||||
])
|
||||
|
@ -286,13 +290,23 @@ mod tests {
|
|||
);
|
||||
assert_eq!(
|
||||
ElementExpressionRoll(vec![
|
||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4]))),
|
||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 2]))),
|
||||
SignedElementRoll::Negative(ElementRoll::Dice(DiceRoll(vec![1, 3, 4], 3))),
|
||||
SignedElementRoll::Positive(ElementRoll::Dice(DiceRoll(vec![1, 2], 2))),
|
||||
SignedElementRoll::Negative(ElementRoll::Bonus(4)),
|
||||
SignedElementRoll::Positive(ElementRoll::Bonus(7)),
|
||||
])
|
||||
.to_string(),
|
||||
"-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)"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue