Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [QUIZ] Dice Roller (#61)

Reply
Thread Tools

[QUIZ] Dice Roller (#61)

 
 
Ruby Quiz
Guest
Posts: n/a
 
      01-06-2006
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').


 
Reply With Quote
 
 
 
 
Gregory Seidman
Guest
Posts: n/a
 
      01-06-2006
On Sat, Jan 07, 2006 at 03:56:47AM +0900, Ruby Quiz wrote:
[...]
} [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



 
Reply With Quote
 
 
 
 
Jacob Fugal
Guest
Posts: n/a
 
      01-06-2006
On 1/6/06, Ruby Quiz <(E-Mail Removed)> wrote:
> Or, for something more complicated:
>
> > roll.rb "(5d5-4)d(16/d4)+3"

> 31


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

Jacob Fugal


 
Reply With Quote
 
J. Ryan Sobol
Guest
Posts: n/a
 
      01-06-2006
--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 ~


On Jan 6, 2006, at 1:56 PM, Ruby Quiz wrote:

> Time to release your inner nerd.
>
> The task for this Ruby Quiz is to write a dice roller...
>
> > roll.rb "3d6" 6

> 72 64 113 33 78 82




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


 
Reply With Quote
 
Austin Ziegler
Guest
Posts: n/a
 
      01-06-2006
On 06/01/06, J. Ryan Sobol <(E-Mail Removed)> wrote:
> 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
--
Austin Ziegler * http://www.velocityreviews.com/forums/(E-Mail Removed)
* Alternate: (E-Mail Removed)


 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      01-06-2006
On Jan 6, 2006, at 1:29 PM, J. Ryan Sobol wrote:

> 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 ~
>
>
> On Jan 6, 2006, at 1:56 PM, Ruby Quiz wrote:
>
>> Time to release your inner nerd.
>>
>> The task for this Ruby Quiz is to write a dice roller...
>>
>> > roll.rb "3d6" 6

>> 72 64 113 33 78 82


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



 
Reply With Quote
 
Matthew Moss
Guest
Posts: n/a
 
      01-06-2006
------=_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.


On 1/6/06, Jacob Fugal <(E-Mail Removed)> wrote:
>
> On 1/6/06, Ruby Quiz <(E-Mail Removed)> wrote:
> > Or, for something more complicated:
> >
> > > roll.rb "(5d5-4)d(16/d4)+3"

> > 31

>
> 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--


 
Reply With Quote
 
Matthew Moss
Guest
Posts: n/a
 
      01-06-2006
------=_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)


On 1/6/06, J. Ryan Sobol <(E-Mail Removed)> wrote:
>
> 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 ~
>
>
> On Jan 6, 2006, at 1:56 PM, Ruby Quiz wrote:
>
> > Time to release your inner nerd.
> >
> > The task for this Ruby Quiz is to write a dice roller...
> >
> > > roll.rb "3d6" 6

> > 72 64 113 33 78 82

>
>
>
>


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


 
Reply With Quote
 
Matthew D Moss
Guest
Posts: n/a
 
      01-06-2006
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.

 
Reply With Quote
 
Will Shattuck
Guest
Posts: n/a
 
      01-06-2006
On 1/6/06, Matthew Moss <(E-Mail Removed)> wrote:
> 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
>


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-1 RND(3-1 RND(3-1 RND(3-1 RND(3-1 RND(3-1

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.../dnd/20040517a


Will

--
Will Shattuck ( willshattuck.at.gmail.com )
Home Page: http://www.thewholeclan.com/will

When you get to your wit's end, you'll find God lives there.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[QUIZ.SUMMARY] Dice Roller (#61) Matthew Moss Ruby 11 01-13-2006 08:29 PM
[QUIZ.SOLUTION] Dice Roller (#61) We don't need no steenking leexer/parsers Paul Novak Ruby 2 01-10-2006 02:28 AM
[SOLUTION] Dice Roller Stefan Walk Ruby 0 01-08-2006 10:56 PM
[SOLUTION] Dice Roller (#61) John Earles Ruby 0 01-08-2006 09:59 PM



Advertisments