[QUIZ] Dice Roller (#61)

R

Ruby Quiz

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Matthew D Moss

Time to release your inner nerd.

The task for this Ruby Quiz is to write a dice roller. You should write a
program that takes two arguments: a dice expression followed by the number of
times to roll it (being optional, with a default of 1). So to calculate those
stats for your AD&D character, you would do this:
roll.rb "3d6" 6
72 64 113 33 78 82

Or, for something more complicated:
roll.rb "(5d5-4)d(16/d4)+3"
31

[NOTE: You'll usually want quotes around the dice expression to hide parenthesis
from the shell, but the quotes are not part of the expression.]

The main code of roll.rb should look something like this:

d = Dice.new(ARGV[0])
(ARGV[1] || 1).to_i.times { print "#{d.roll} " }

The meat of this quiz is going to be parsing the dice expression (i.e.,
implementing Dice.new). Let's first go over the grammar, which I present in a
simplified BNF notation with some notes:

<expr> := <expr> + <expr>
| <expr> - <expr>
| <expr> * <expr>
| <expr> / <expr>
| ( <expr> )
| [<expr>] d <expr>
| integer

* Integers are positive; never zero, never negative.
* The "d" (dice) expression XdY rolls a Y-sided die (numbered
from 1 to Y) X times, accumulating the results. X is optional
and defaults to 1.
* All binary operators are left-associative.
* Operator precedence:
( ) highest
d
* /
+ - lowest

[NOTE: The BNF above is simplified here for clarity and space. If requested, I
will make available the full BNF description I've used in my own solution, which
incorporates the association and precedence rules.]

A few more things... Feel free to either craft this by hand or an available
lexing/parsing library. Handling whitespace between integers and operators is
nice. Some game systems use d100 quite often, and may abbreviate it as "d%"
(but note that '%' is only allowed immediately after a 'd').
 
G

Gregory Seidman

]
} [NOTE: The BNF above is simplified here for clarity and space. If
} requested, I will make available the full BNF description I've used in my
} own solution, which incorporates the association and precedence rules.]

I would appreciate the full BNF, please.

--Greg
 
J

J. Ryan Sobol

--Apple-Mail-15--645237476
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

Please don't take this the wrong way, but I've never played D&D.
Would someone mind explaining the math that went into the command
below to generate it's result?

~ ryan ~


Time to release your inner nerd.

The task for this Ruby Quiz is to write a dice roller...

72 64 113 33 78 82



--Apple-Mail-15--645237476--
 
A

Austin Ziegler

Please don't take this the wrong way, but I've never played D&D.
Would someone mind explaining the math that went into the command
below to generate it's result?

I suspect user error.

The correct answer will always be between 3 and 18 for 3d6.

-austin
 
J

James Edward Gray II

Please don't take this the wrong way, but I've never played D&D.
Would someone mind explaining the math that went into the command
below to generate it's result?

~ ryan ~

Hmm, that example looks wrong now that you mention it. It should be
6 numbers between 3 and 18 (the roll of 3 six-sided dice).

James Edward Gray II
 
M

Matthew Moss

------=_Part_10183_9649781.1136576280805
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Sticking with typical integer division (ie, round-down) is fine.

If you wanted to extend the syntax to support round-up division (using '\'
perhaps) or other options, feel free. Extra credit.

A lot of extra credit if you add syntax to support some RPGs/home rules
where you might want 3d6, but you'll actually roll 4d6 and toss the lowest.


I assume integer arithmetic? So if, for example, a 3 comes up on your
d4, 16/d4 would be 5?

Jacob Fugal

------=_Part_10183_9649781.1136576280805--
 
M

Matthew Moss

------=_Part_10191_15694013.1136576379698
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Ha ha... Must have copied the wrong line when writing up the quiz
description.

That should look like this:
roll.rb "3d6" 6
18 18 18 18 18 18

=3D)


Please don't take this the wrong way, but I've never played D&D.
Would someone mind explaining the math that went into the command
below to generate it's result?

~ ryan ~

------=_Part_10191_15694013.1136576379698--
 
M

Matthew D Moss

I'm not sure whether this made it to the list first time I sent it, or
is just delayed. Here it is again in case folks missed it...
I would appreciate the full BNF, please.

Okay, this is what I've done in my current version that takes care of
basic precedence and associativity.

INTEGER = /[1-9][0-9]*/

expr: fact
| expr '+' fact
| expr '-' fact

fact: term
| fact '*' term
| fact '/' term

term: unit
| [term] 'd' dice

dice: '%'
| unit

unit: '(' expr ')'
| INTEGER


Actually, this is slightly different than my current version, which
after reexamining to extract this BNF, I found a minor error (in
handling of the term rules and handling of the optional arg). My own
code has a morphed version of this BNF in order to code up a recursive
descent parser, but this BNF shows one way to handle the
precedence/association rules.
 
W

Will Shattuck

Ha ha... Must have copied the wrong line when writing up the quiz
description.

That should look like this:

18 18 18 18 18 18

Actually 3d6 means roll a 6 sided die 3 times so you would have a result of=
3-18

so this:
roll.rb "3d6" 6

Would actully be: (RND =3D Random)

RND(3-18) RND(3-18) RND(3-18) RND(3-18) RND(3-18) RND(3-18)

Below is 3d6 from the DnD Dice Roller on Wizards.com. The +0 would be
a modifier from depending if it was an attack roll or a defense roll.=20
For our purposes you would remove the +0

Roll(3d6)+0:
1,6,6,+0
Total:13

DnD Dice Roller:
http://www.wizards.com/default.asp?x=3Ddnd/dnd/20040517a


Will
 
A

Austin Ziegler

Ha ha... Must have copied the wrong line when writing up the quiz
description.

That should look like this:

18 18 18 18 18 18

Just don't tell me that the first one is 18/00.

-austin
 
M

Matthew Moss

Actually 3d6 means roll a 6 sided die 3 times so you would have a result =
of 3-18

Actually, you're right, but actually my post was a half-joke. The
munchkin players seem to roll 18's every time. ;)
 
J

J. Ryan Sobol

--Apple-Mail-17--642119500
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

I guess that must be a D&D inside half-joke because I'm totally
confused.

Don't worry about explaining it as I just needed to know what that
command, roll.rb "3d6" 6, did.

~ ryan ~


18 18 18 18 18 18

Just don't tell me that the first one is 18/00.

<dies laughing> They all were, of course.

Actually, you're right, but actually my post was a half-joke. The
munchkin players seem to roll 18's every time. ;)






--Apple-Mail-17--642119500--
 
J

James Edward Gray II

I guess that must be a D&D inside half-joke because I'm totally
confused.

18 was the best stat a starting character could have. If you got
one, they let you roll d% and put it after the slash (the higher the
better). 00 == 100. So characters with 18/00 had some damn lucky
die rolls. :)

James Edward Gray II
 
A

Austin Ziegler

18 was the best stat a starting character could have. If you got
one, they let you roll d% and put it after the slash (the higher the
better). 00 =3D=3D 100. So characters with 18/00 had some damn lucky
die rolls. :)

In most versions of D&D/AD&D, this was also limited to Strength attributes =
only.

This may have changed recently. ;)

-austin
 
J

James Edward Gray II

In most versions of D&D/AD&D, this was also limited to Strength
attributes only.

This may have changed recently. ;)

Ah, yeah, you're right. It's been too long.

Actually, I believe 3rd Edition and up did away with the extra
percentile roll altogether.

James Edward Gray II
 
M

Matthew Moss

------=_Part_10155_29622363.1136575984791
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
I would appreciate the full BNF, please.

Okay, this is what I've done in my current version that takes care of basic
precedence and associativity.

INTEGER =3D /[1-9][0-9]*/

expr: fact
| expr '+' fact
| expr '-' fact

fact: term
| fact '*' term
| fact '/' term

term: unit
| [term] 'd' dice

dice: '%'
| unit

unit: '(' expr ')'
| INTEGER


Actually, this is slightly different than my current version, which after
reexamining to extract this BNF, I found a minor error (in handling of the
term rules and handling of the optional arg). My own code has a morphed
version of this BNF in order to code up a recursive descent parser, but thi=
s
BNF shows one way to handle the precedence/association rules.

------=_Part_10155_29622363.1136575984791--
 
W

Will Shattuck

forgive my ignorance... BNF?

w


I would appreciate the full BNF, please.

Okay, this is what I've done in my current version that takes care of bas= ic
precedence and associativity.

INTEGER =3D /[1-9][0-9]*/

expr: fact
| expr '+' fact
| expr '-' fact

fact: term
| fact '*' term
| fact '/' term

term: unit
| [term] 'd' dice

dice: '%'
| unit

unit: '(' expr ')'
| INTEGER


Actually, this is slightly different than my current version, which after
reexamining to extract this BNF, I found a minor error (in handling of th= e
term rules and handling of the optional arg). My own code has a morphed
version of this BNF in order to code up a recursive descent parser, but t= his
BNF shows one way to handle the precedence/association rules.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top