dbkang / dice-expression-evaluator

Parses and evaluates dice expressions using randomized dice rolls
MIT License
6 stars 4 forks source link

dice-expression-evaluator NPM version Build Status Dependency Status Coverage percentage

dice-expession-evaluator is used to parse dice expressions and to evaluate them using simulated dice rolls.

Installation

$ npm install --save dice-expression-evaluator

Dice expressions

Dice expressions are used to specify calculations that are based on a series of dice rolls. For example, this is a dice expression that represents a single, standard 6-sided die:

1d6

This can be shortened to:

d6

What if we rolled five of those dice at once and added up?

5d6

To roll two other 10-sided dice and subtract the sum of the two from above?

5d6 - 2d10

What if we also wanted to add 15 to ensure that the final result does not go below zero?

5d6 - 2d10 + 15

If the number of sides is 100, it can be replaced with a % sign (percentage, out of 100, got it?)

5d100 == 5d%

Dice expressions can also be specified as:

API

var DiceExpression = require('dice-expression-evaluator');

var d = new DiceExpression('2d5 + 4d2 + 10');

Examples

d() // 20
d() // 18
d() // 23
d.min() // 16
d.max() // 28
d.roll() // { roll: 23, diceSums: [7, 6, 10], diceRaw: [[3, 4], [1, 2, 2, 1], [10]] }

Future plans

We may add support for

License

MIT © [Dan Kang]()