Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implement postfix ops for dice (best/worst) #9

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ lazy_static! {
PrecClimber::new(vec![
Operator::new(plus, Left) | Operator::new(minus, Left),
Operator::new(times, Left) | Operator::new(slash, Left),
Operator::new(roll, Right),
])
};
}
Expand All @@ -38,6 +37,42 @@ pub fn compute(expr: Pairs<Rule>) -> i64 {
|pair: Pair<Rule>| match pair.as_rule() {
Rule::number => pair.as_str().parse::<i64>().unwrap(),
Rule::expr => compute(pair.into_inner()),
Rule::dice => {
let mut inner = pair.into_inner();
// LHS
let num = inner.next().unwrap();
let lhs = num.as_str().parse::<i64>().expect("Did not find a number on LHS!");
assert!(lhs != 0, "Number of dice must not be zero!");
// Operator
let d = inner.next().unwrap().as_str();
assert!(d == "d" || d == "D");
// RHS
let num = inner.next().unwrap();
let rhs = num.as_str().parse::<i64>().expect("Did not find a number on RHS!");
assert!(rhs > 0, "Number of sides must be greater than zero!");
// Postfix
let op = inner.next();
match op.map(|r| r.as_rule()) {
Some(Rule::best) => {
let num = inner.next().unwrap();
let take = num.as_str().parse::<i64>().expect("Did not find a number on postfix!");
lhs.signum() * roll_and_take_dice_raw(lhs.abs(), rhs as u64, take)
}
Some(Rule::worst) => {
let num = inner.next().unwrap();
let take = num.as_str().parse::<i64>().expect("Did not find a number on postfix!");
lhs.signum() * roll_and_take_dice_raw(lhs.abs(), rhs as u64, -take)
}
Some(Rule::adv) => {
lhs.signum() * roll_and_take_dice_raw(lhs.abs() * 2, rhs as u64, lhs.abs())
}
Some(Rule::dis) => {
lhs.signum() * roll_and_take_dice_raw(lhs.abs() * 2, rhs as u64, -lhs.abs())
}
None => lhs.signum() * roll_dice_raw(lhs.abs(), rhs as u64),
Some(_) => unreachable!(),
}
}
Rule::custom_dice => {
let mut inner = pair.into_inner();
// LHS
Expand All @@ -59,18 +94,6 @@ pub fn compute(expr: Pairs<Rule>) -> i64 {
_ => unreachable!(),
},
|lhs: i64, op: Pair<Rule>, rhs: i64| match op.as_rule() {
Rule::roll => {
if rhs < 1 {
panic!("Sides must be greater than zero")
} else {
match lhs.signum() {
0 => panic!("Number of sides must not be zero"),
-1 => -roll_dice_raw(lhs.abs(), rhs as u64),
1 => roll_dice_raw(lhs.abs(), rhs as u64),
_ => unreachable!(),
}
}
},
Rule::plus => lhs + rhs,
Rule::minus => lhs - rhs,
Rule::times => lhs * rhs,
Expand Down
15 changes: 15 additions & 0 deletions src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@ pub fn roll_custom_dice_raw(num: i64, sides: &[u64]) -> i64 {

(0..num.abs()).map(|_| rng.choose(sides).unwrap()).fold(0, |acc, x| acc + *x as i64)
}

pub fn roll_and_take_dice_raw(num: i64, sides: u64, take: i64) -> i64 {
let mut rng = thread_rng();
let mut rolls: Vec<_> = (0..num.abs()).map(|_| rng.gen_range(1, sides as i64 + 1)).collect();
rolls.sort_unstable();

if take.is_positive() {
rolls.iter().rev().take(take.abs() as usize).sum()
} else if take.is_negative() {
rolls.iter().take(take.abs() as usize).sum()
} else {
// Zero is technically correct even if it seems odd.
0
}
}
8 changes: 7 additions & 1 deletion src/rouler.pest
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ op = _{ plus | minus | times | slash }
times = { "*" }
slash = { "/" }

dice = _{ number ~ (roll ~ number) }
post = _{ best | worst | adv | dis }
best = { "b" }
worst = { "w" }
adv = { "a" }
dis = { "d" }

dice = { number ~ (roll ~ number) ~ (post ~ number?)? }
roll = { "d" | "D" }
custom_dice = { number ~ roll ~ sides }
sides = _{ "[" ~ number ~ ("," ~ number )* ~ "]" }
Expand Down