crapshoot

The alpha and omega of rolling dice.

Why

Sometimes you want to roll four six-sided dice, drop the lowest, add three four-sided dice, and subtract seventeen.

Crapshoot.roll '4d6v + 3d4 - 17'

You might otherwise want to roll three twenty-sided dice and drop the highest, and then add six:

Crapshoot.roll '3d20^+6'

Or you might just want nine divided by three:

Crapshoot.roll '9 / 3'

Crapshoot supports addition, subtraction, multiplication, and division with correct order of operations:

Crapshoot.roll '1 + 2 * 3' # returns seven

Rolling Dice

Crapshoot.roll is the method that rolls dice and returns the result. It takes one argument, an expression in the crapshoot language.

67

a Constant

4d4

a Series, rolling four four-sided dice and adding them together

4d6v

a Series, rolling four four-sided dice, dropping the lowest one, adding them together

17+17

adds two Constants together

4d6v - 17

subtracts a Constant from a Series

4d20 + 2d100 + 3d6 - 4d4 + 19

adds a whole bunch of dice, subtracts some dice, and adds nineteen

Generally, whitespace is allowed around arithmetic operations, but not inside a series.

Describing a Roll

The return value from Crapshoot.roll is a Crapshoot::Result object, which is an Integer with a description field and a detailed_description field:

r = Crapshoot.roll '4d4^ + 6d6 - 10' # => 22
r.description # => "(2+2+2+3-3)+(1+3+5+5+6+6)-10"
r.detailed_description # => [["4d4^", "2+2+2+3-3"], ["+", "+"], ["6d6", "1+3+5+5+6+6"], ["-", "-"], ["10", "10"]]
r + 5 # => 27

Developing with Ragel

I used the “Ragel” state machine compiler to generate the parser for this library. If you’re changing the parser, you will need to install Ragel.

I’ve had good luck with the macports and homebrew packages for Ragel; I don’t anticipate any problems with any packaging of it.

If you are modifying the parser, you may want to generate a Graphviz .dot or PDF document of the state machine produced by the scanner. The “rake scanner:pdf” task will generate this PDF, provided your installation of Graphviz allows it. If you’re developing on Mac OS X, you can get a PDF-capable copy of Graphviz from www.graphviz.org/Download_macos.php .

Compatibility

Crapshoot has been developed and tested with ruby 1.9.2; it works on 1.9.3-rc1, which will be a supported platform once it’s released.

Our tests on 1.9.2 and 1.9.3 can be viewed at travis-ci.org/bkerley/crapshoot .

Contributing to crapshoot

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Write tests for your feature

  • Commit and push until you are happy with your contribution

  • Send me a pull request

Copyright © 201-2011 Bryce Kerley. See LICENSE.txt for further details.