Die

Die

The base Die class.

Each Die instance represents a distinct term in a roll equation which transacts rolls of an die with some number of faces. The Die instance provides controls for rerolling, exploding, counting, or modifying the set of results from the Die.

Constructor

new Die(faces)

Source:
Example
// Define a 6-sided die
let die = new Die(6);

// Roll the die 4 times
die.roll(4);

// Roll another 2 times, adding the new results to the existing set
die.roll(2);

// For all 6 of the initial rolls, reroll if any result was a 1
die.reroll([1]);

// For set of remaining results, roll a bonus die if any result was a 6
die.explode([6]);

// Count the total number of rolls which was greater than 3
die.countSuccess(3, ">");

// Display the total number of successes
console.log(die.total);
Parameters:
Name Type Description
faces Number

The number of faces for this Die

Members

(static) rgx :Object

Source:

Define regular expression option matches for the Die class

Type:
  • Object

faces :Number

Source:

The number of faces for this Die

Type:
Example
let die = new Die(6);    // A 6-sided die has six faces
console.log(die.faces)   // 6

options

Source:

Any additional options which may be required by the Die

results :Array

Source:

Track the set of kept results out of all rolls

Type:
Example
let die = new Die(6);
die.roll(6);               // Roll 6d6
console.log(die.results);  // [6,4,1,2,3,4]
die.keepHighest(2);        // Keep the 2 best results
console.log(die.results);  // [6,4]

rolls :Array

Source:

Track all dice which have ever been rolled

Type:
Example
let die = new Die(4);
die.roll(4);             // Roll 4d4
console.log(die.rolls);  // [{...}, {...}, {...}, {...}]

sides :Array

Source:

An Array representing the faces of the die

Type:
Example
let die = new Die(6);    // One side for each of the possible faces
console.log(die.sides)   // [1,2,3,4,5,6]

total :Number

Source:

The sum of all kept results

Type:
Example
let die = new Die(20);
die.roll(2);               // Roll 2d20
console.log(die.results)   // [6,17]
console.log(die.total)     // 23

Methods

(static) fromFormula(formula) → {Die|null}

Source:

Given a string formula, create and return a rolled Die object

Parameters:
Name Type Description
formula string

The string formula to parse

Returns:

The rolled Die object if the formula was valid, null otherwise

Type
Die | null

applyModifiers(query)

Source:

Apply suffix options and modifiers to the result of this Die roll

Parameters:
Name Type Description
query string

countSuccess(target, operator)

Source:

Map results to 0 or 1 depending on whether they match a success condition

Example
let die = new Die(3);
die.roll(6);               // Roll 6d3
console.log(die.results);  // [1,3,1,2,2,3]
die.countSuccess(3);       // Count the results where a 3 was rolled
console.log(die.results);  // [0,1,0,0,0,1]
console.log(die.total);    // 2
Parameters:
Name Type Description
target Number

The target result to test against

operator String

The comparison operator against which to test. Default is '>='

explode(range) → {Die}

Source:

Explode the rolls in this set by rolling additional dice for each roll which achieved a certain result Dice which have been re-rolled or have already exploded cannot explode

Example
let die = new Die(8);
die.roll(6);               // Roll 6d8
console.log(die.results);  // [8,3,6,4,2,7]
die.explode([7,8]);        // Explode on 7s and 8s, rolling additional dice
console.log(die.results);  // [8,3,6,4,2,7,7,2,3]
Parameters:
Name Type Description
range Array

The range of target results which would trigger an explode

Returns:

The updated die containing new rolls

Type
Die

keepHighest(n) → {Die}

Source:

Filter the result set, keeping the highest n results in order

Example
let die = new Die(6);
die.roll(4);               // Roll 4d6
console.log(die.results);  // [6,2,1,5]
die.keepHighest(2);        // Keep the best 2 results
console.log(die.results);  // [6,5]
Parameters:
Name Type Description
n Number

The number of results to keep

Returns:

The updated die containing new rolls

Type
Die

keepLowest(n) → {Array}

Source:

Filter the result set, keeping the lowest n results in order

Example
let die = new Die(6);
die.roll(4);               // Roll 4d6
console.log(die.results);  // [6,2,1,5]
die.keepLowest(3);         // Kepe the lowest 3 results
console.log(die.results);  // [2,1,5]
Parameters:
Name Type Description
n Number

The number of results to keep

Returns:

The filtered results

Type
Array

reroll(targets) → {Die}

Source:

Re-roll any results with results in the provided target set Dice which have already been re-rolled will not be re-rolled again

Example
let die = new Die(4);
die.roll(3);               // Roll 3d4
console.log(die.results);  // [1,3,4]
die.reroll([1,2]);         // Re-roll 1s or 2s
console.log(die.results);  // [3,4,2]
Parameters:
Name Type Description
targets Array

Target results which would trigger a reroll

Returns:

The updated die containing new rolls

Type
Die

roll(nd) → {Die}

Source:

Roll the initial set of results for the Die

Example
let die = new Die(6);
die.roll(6);               // Roll 6d6
console.log(die.results);  // [5,2,4,4,1,6]
console.log(die.total);    // 22
Parameters:
Name Type Description
nd Number

The number of times to roll the die

Returns:

The updated die containing new rolls

Type
Die