NOTE: This project is abandoned. Ed Ropple maintains a fork that can be found on GitHub:

Dice Notation .NET

A simple parser / evaluator for a variant of "dice notation" similar to the version used in Dungeons and Dragons 3.5 that allows developers to support the use of dice notation in their own programs. It is written in C# and depends only on System and System.Core.

An overview of dice notation can be found on Wikipedia. At the moment, the project supports only the variant of die notation that I am using in a game of my own, so more advanced features are not yet implemented. This may change in the future.

Examples of Supported Dice Notation

Expression Meaning
3d6 Roll three six-sided dice
4d6k3 Roll four six-sided dice, keep the three highest
2*2d8 Roll two eight-sided dice and multiply the result by two
5+d2 Roll a two-sided die (flip a coin) and add 5 to the result

Simple Examples of Use

Dice.Parse("3d6").Roll().Value // Get the sum. You can also inspect the individual terms / dice by looking at the Results property.
Dice.Parse("3d6").Roll(new MinRoller()) // Roll using a custom "roll provider" (Standard roller just uses System.Random)

IDiceParser parser = new DiceParser();
DiceExpression dice = parser.Parse("3d6");

Fluent Construction Syntax

var expression = new DiceExpression().Constant(5).Die(8).Dice(4, 6, choose: 3); // 5+d8+4d6k3

Not Currently Supported

  • Results of dice rolls cannot be used as parameters to dice rolls (scalar, multiplicity, or choose).
  • The distributive property is not supported. ("3*(2 + d6)" is not possible)
  • There is no support for variable substitution (1d{Weapon}) yet.
  • Malformed dice expressions are not detected / handled particularly gracefully.

Last edited Mar 15, 2015 at 10:20 PM by cawagner, version 14