Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
Hoodlums problem - D&D Damage Dice

Here's a simple problem which I found quite fun to implement.

Suggested hoodlums problem (beginner level):

Dungeons and Dragons Damage Dice


In the game of dungeons and dragons, an example damage expression might be:

"Half of (2d12 + 3d6 + 19 + 2d6) + 1d6 + StrengthModifier" hit points of damage.


  • 2d12, for example, notates the sum of two 12-sided dice
  • StrengthModifier is just a simple scalar variable (e.g. -2), for a given character.

It is also possible for the player to make a "critical hit" which does maximum damage, requiring the max possible value of the damage expression be calculated, rather than the expression being "rolled" randomly.


Create a function to calculate a sample damage total based on the expression, as well as optionally producing some statistics about the expression.

For the first pass, we can assume the damage expression is coded as legal haskell.

Example: we might encode

"half of (3d6 + 2d4) + half of (StrengthMod + 3)"


DAdd (DHalf (DAdd (DRoll 3 D6) (DRoll 2 D4))) (DHalf (DAdd (DMod StrMod) (DLit 3)))

Later we might develop a little language for simpler text entry or pretty-printing.

Besides this, it might be fun to show this expression in various stages of evaluation:

  • simplified (reduced)
  • Fully evaluated, with a small number of sample dice rolls
  • As above, but showing the evaluation of each component of the roll (further work)
  • Fully evaluated, with thousands dice rolls, showing the actual distribution of results
  • Theoretically evaluated, showing:
    • the likely distribution
    • the predicted median value
  • Fully evaluated in maximised form (e.g. for a critical hit)

Perhaps we would use R or gnuplot to do the actual graphing.

It's not specific to the problem or to D&D, but the following web site has examples of more complicated dice expressions which illustrate the domain:

e.g. the possibility one should re-roll the result of any die less than n the possibility of "explosion" that any die rolling its max value causes additional die to be added to the roll (possibly recursively), representing unlikely but possible explosive success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment