Dice Notation

Completed Jan. 7, 2010

In role-playing games, there is often a need to resolve the outcome of actions using random numbers. In tabletop games, this is usually accomplished by using a variety of dice. The number and type of dice are often specified by using a type of "dice notation".

The basic syntax for this notation is "XdY" where X is the number of dice and Y is the faces on each die. These rolls can be futher modified using basic arithmetic operators such as "+" and "*" for addition an multiplication, respecively. Here are some examples:

  • "1d20" means roll one 20-sided die
  • "3d6+10" means roll 3 6-sided dice (summing the totals) and add 10 to the result
  • "4*d100" means roll a 100-sided die and multiply the result by 4

Your task to create a program that can example a dice notation expression as an input, and generate a usable result.

It should at least accept the following syntaxes:

  • "3d6" roll 3 6-sided die (summing the total)
  • "2d4+10" roll 2 4-sided die and add 10
  • "d20" roll 1 20-sided die (the 1 is implied if not provided)
  • "3*4d6" roll 4 six-sided die and multiply the result by 3
  • "d10 * d10" roll 2 10-sided dice and multiply the result

Extra credit:

  1. Also accept subtraction "-" and division "/" operators

  2. Support "keep" syntax as well. This will allow you to specify whether to keep or discard certain dice rolls. This takes the form XdYkZ, where X is number of dice to roll, Y is the number of faces, and Z is how many dice to keep. If this notation is used, you should keep the "best" dice, which means you get rid of the lowest rolls first. For example: "4d6k3" means roll 4 six-sided dice, keeping the best 3. If the rolls turned up as 1,3,4,5 you would discard the 1, and the total would be 12.

  3. Also accept parenthes "()" for operator precedence. Example: "(1d20+3)*5"

  4. Allow the result of die rolls to be used as parameters in subsequent rolls For instance: "(d20)d6" means to roll a d20, and use the result to specify how many six-sided die to roll.

  5. Write an analyzer to determine the probability of each result from a given dice notation experession :)

This challenge is now closed.


None yet!

Come Chat!

Join channel ##proggit on freenode.


Having issues with the the site? Let us know.