Re: Parsing a Dice Roll String
- Posted by Matt Lewis <matthewwalkerlewis at yahoo.com> Aug 05, 2004
- 454 views
Christian Cuvier wrote: > > > Subject: Parsing a Dice Roll String > > > > > > posted by: cklester <cklester at yahoo.com> > > > > I'm writing a die roller and need to be > > able to parse a dice function string. > > > > Some examples: > > > > 3d6+5 (roll 6-sided die 3 times, add 5 to sum) > > 4d6-Lowest (roll 6-sided die 4 times, discard lowest die) > > 4d6-Highest (roll 6-sided die 4 times, discard highest die) > > 2d10*3-3d4 (roll 10-sided die twice, multiply by 3, then subtract > > the result of rolling a 4-sided die three times) > > 5d12/10 (roll 12-sided die five times, divide result by 10) > > > > Well, I'd expect Matt's matheval.e to be able to handle this with minor text > translation. > My understanding of your pb is: parse a string like ` > roll ::= > ({<number>d<sides>[<operation>(roll | <reference> | > <constant>])[<operation>])+ > > where <reference is any of "Lowest" or "Highest" (any more?). > What's to be stored in list_Rolls? The roll outcome? > <number> and <sides> are positive integers, <constant> is an atom.. > <operation> is any of +-*/ (any more?) > Did I get it? It would sort of work. If you put these types of things through matheval, here's what you'd get: 3d6+5: { POLYNOMIAL, "D6", {5,3}} 4d6-lowest: { ADD, { VAR, "D6", 4 }, { VAR, "LOWEST", {-1} }} 2d10*3-3d4: { ADD, { VAR, "D10",{6}},{ VAR,"D4",{-3}}} One problem is that matheval tries to simplify things if it can, so 2d10*3 looks like 6d10 to it, even though that's not correct. Also, it tries hard to make things into polynomials if possible, since that's a lot more compact, and also faster to evaluate. If you took out the call_func( SIMPLIFY, {expr[1]}) call at the end of Parse() in parseval.e, you might be able to avoid some of these things. Matt Lewis