Skip to content

Commit

Permalink
allow to throw multiple dice (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Hanke authored and nicolodavis committed Feb 23, 2018
1 parent 8c88b70 commit ebe7758
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/core/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ const SpotValue = {
const predefined = {};
for (const key in SpotValue) {
const spotvalue = SpotValue[key];
predefined[key] = () => {
return Math.floor(random() * spotvalue) + 1;
predefined[key] = diceCount => {
if (diceCount === undefined) {
return Math.floor(random() * spotvalue) + 1;
} else {
return [...Array(diceCount).keys()].map(
() => Math.floor(random() * spotvalue) + 1
);
}
};
}

Expand All @@ -91,27 +97,39 @@ export function GenSeed() {
export const Random = {
/**
* Similar to Die below, but with fixed spot values.
* Supports passing a diceCount
* if not defined, defaults to 1 and returns the value directly.
* if defined, returns an array containing the random dice values.
*
* D4: () => value
* D6: () => value
* D8: () => value
* D10: () => value
* D12: () => value
* D20: () => value
* D4: (diceCount) => value
* D6: (diceCount) => value
* D8: (diceCount) => value
* D10: (diceCount) => value
* D12: (diceCount) => value
* D20: (diceCount) => value
*/
...predefined,

/**
* Roll a die of specified spot value.
*
* @param {number} spotvalue - The die dimension (default: 6).
* @param {number} diceCount - number of dice to throw.
* if not defined, defaults to 1 and returns the value directly.
* if defined, returns an array containing the random dice values.
*/
Die: spotvalue => {
Die: (spotvalue, diceCount) => {
if (spotvalue === undefined) {
spotvalue = 6;
}

return Math.floor(random() * spotvalue) + 1;
if (diceCount === undefined) {
return Math.floor(random() * spotvalue) + 1;
} else {
return [...Array(diceCount).keys()].map(
() => Math.floor(random() * spotvalue) + 1
);
}
},

/**
Expand Down
19 changes: 19 additions & 0 deletions src/core/random.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ test('predefined dice values', () => {
expect(result).toBeGreaterThanOrEqual(1);
expect(result).toBeLessThanOrEqual(pair.highest);
expect(PRNGState.get().prngstate).toBeDefined();

const multiple = pair.fn(5);
expect(multiple).toBeDefined();
expect(multiple.length).toBe(5);
multiple.forEach(m => {
expect(m).toBeGreaterThanOrEqual(1);
expect(m).toBeLessThanOrEqual(pair.highest);
});
});
});

Expand All @@ -51,6 +59,17 @@ test('Random.Die', () => {
expect(result).toBeLessThanOrEqual(6);
expect(PRNGState.get().prngstate).toBeDefined();
}

{
const multiple = Random.Die(6, 3);
expect(multiple).toBeDefined();
expect(multiple.length).toBe(3);
multiple.forEach(m => {
expect(m).toBeGreaterThanOrEqual(1);
expect(m).toBeLessThanOrEqual(6);
});
expect(PRNGState.get().prngstate).toBeDefined();
}
});

test('Random.Number', () => {
Expand Down

0 comments on commit ebe7758

Please sign in to comment.