Package Exports
- dicebag
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dicebag) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
dicebag
A dice expression parser and roller.
Installation
# in a local node_modules/
npm install --save dicebag
# globally, to use the CLI
npm install -g dicebagCommand-line usage
dicebag [-p] [<dice expression>]If a dice expression is provided, prints the result of rolling those dice and
exits. Otherwise, reads expressions from stdin in a loop.
-pprint dice pools (default behavior is to print the dice's sum)
Examples
$ dicebag 1d6
1
$ dicebag "2d8 + 1d4"
7
$ dicebag -p "2d8 + 1d4"
[ 5, 3, 4 ]Library usage
const { parse, pool, roll } = require('dicebag')The API consists of three functions:
parse(diceExpression)parses an expression into an object understood by the other two functions.pool(dice)rolls the dice and returns an array of their results.roll(dice)rolls the dice and returns their sum.
Examples
const d6 = parse('1d6')
roll(d6) // 4
roll(d6) // 5
pool(d6) // [ 2 ]
const dice = parse('2d6 + 1d8')
roll(dice) // 10
pool(dice) // [ 1, 4, 7 ]Dice expressions
Basics
Simple expressions involving standard dice notation as used in most roleplaying games are supported. You can do things like:
XdY: rollsXY-sided dice (1d6is a single 6-sided die,2d4is two 4-sided dice).dXis the same as1dX(so you can shorten1d6tod6).dice +/- constant: rolls the dice, adds/subtracts the constant.dice +/- moreDice: sums/takes the difference of the results of rollingdiceandmoreDice.
Full syntax and semantics
Note: this is still an early version. Syntax and semantics will be expanded in future versions. Backwards incompatible changes are possible.
The parser recognizes the following grammar (all whitespace between symbols is ignored):
Die ::= <an integer>
| '(' Die ')'
| Die 'd' Die
| 'd' Die
| Die '+' Die
| Die '-' Die
| '-' DieSemantics are defined in terms of the pool function.
N, whereNis an integer, is a die that always rolls a single value equal toN.poolreturns an array containing justN.DdE, whereDandEare dice expressions, is a die that rolls a number of dice equal to the result of rollingD, where each die has a number of sides equal to the result of rollingE.poolreturns an array ofroll(D)numbers, each between 1 androll(E). Note: ifDorEevaluates to a negative number, the behavior is undefined.dDis equivalent to1dD.D+Eappends the dice pool generated byEto the dice pool generated byD.-Dreturns the opposites of values generated byD.D-Eis equivalent toD+(-E).
Additionally:
- The binary arithmetic operations (
+,-) are left associative. - The die operation
dis right associative (1d2d3is equivalent to1d(2d3), use explicit parentheses if you need(1d2)d3) dbinds stronger than the binary arithmetic operations (1d6+1d4is equivalent to(1d6) + (1d4)).