[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Dice Roller (#61

James Gray

1/6/2006 6:57:00 PM

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

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').


108 Answers

Gregory Seidman

1/6/2006 7:01:00 PM

0

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



Jacob Fugal

1/6/2006 7:14:00 PM

0

On 1/6/06, Ruby Quiz <james@grayproductions.net> 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


J. Ryan Sobol

1/6/2006 7:29:00 PM

0

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


Austin Ziegler

1/6/2006 7:36:00 PM

0

On 06/01/06, J. Ryan Sobol <ryansobol@gmail.com> 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 * halostatue@gmail.com
* Alternate: austin@halostatue.ca


James Gray

1/6/2006 7:37:00 PM

0

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



matthew.moss.coder

1/6/2006 7:38:00 PM

0

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 <lukfugl@gmail.com> wrote:
>
> On 1/6/06, Ruby Quiz <james@grayproductions.net> 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
>
>

matthew.moss.coder

1/6/2006 7:40:00 PM

0

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

=)


On 1/6/06, J. Ryan Sobol <ryansobol@gmail.com> 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
>
>
>
>

matthew.moss.coder

1/6/2006 7:43:00 PM

0

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.

Will Shattuck

1/6/2006 7:47:00 PM

0

On 1/6/06, Matthew Moss <matthew.moss.coder@gmail.com> 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 = 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.
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/dnd...


Will

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

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


Austin Ziegler

1/6/2006 7:49:00 PM

0

On 06/01/06, Matthew Moss <matthew.moss.coder@gmail.com> 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

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

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca