m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-11 12:18:10 -0400
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-11 12:19:07 -0400
commit69b48d1a9c4e97d5f71230c75b3cfe6d8453558f (patch)
tree5397c84f16bc2343bed4badf39b8ac47189856f4
parent11219fcba6108f7f8db138ae8a3cbb0ec8f4df3b (diff)
Add the constant die
-rw-r--r--__tests__/dice.test.js52
-rw-r--r--src/dice.js10
2 files changed, 56 insertions, 6 deletions
diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js
index e113c3d..04b1c84 100644
--- a/__tests__/dice.test.js
+++ b/__tests__/dice.test.js
@@ -1,4 +1,4 @@
-const { d, add, subtract } = require('../src/dice.js')
+const { constant, d, add, subtract } = require('../src/dice.js')
const defaultNumberRolls = 500
const defaultError = 0.2
@@ -182,7 +182,7 @@ const combinedDiceTestSpecs = (dieSpecs) => {
const describeBasicDie = (number, sides, numberRolls = defaultNumberRolls) => {
describe(`${number}d${sides}`, () => {
- const die = d(number, sides)
+ const die = d(constant(number), constant(sides))
testDie(die, basicDieTestSpecs(number, sides), numberRolls)
})
}
@@ -200,13 +200,57 @@ const describeCompoundDice = (diceSpecs, numberRolls = defaultNumberRolls) => {
const dieString = getDieString(diceSpecs)
const die = diceSpecs.slice(1).reduce((die, spec) => {
const combinator = spec.negative ? subtract : add
- return combinator(die, d(spec.number, spec.sides))
- }, d(diceSpecs[0].number, diceSpecs[0].sides))
+ return combinator(die, d(constant(spec.number), constant(spec.sides)))
+ }, d(constant(diceSpecs[0].number), constant(diceSpecs[0].sides)))
describe(dieString, () => testDie(die, combinedDiceTestSpecs(diceSpecs),
numberRolls))
}
+describe('constant', () => {
+ describe('1', () => {
+ const die = constant(1)
+ testDie(die, {
+ diceCount: 1,
+ average: { average: 1 },
+ variance: { variance: 0 },
+ bounds: {
+ low: 1,
+ high: 1,
+ expectLow: true,
+ }
+ }, 10)
+ })
+
+ describe('10', () => {
+ const die = constant(10)
+ testDie(die, {
+ diceCount: 1,
+ average: { average: 10 },
+ variance: { variance: 0 },
+ bounds: {
+ low: 10,
+ high: 10,
+ expectLow: true,
+ }
+ }, 10)
+ })
+
+ describe('-5', () => {
+ const die = constant(-5)
+ testDie(die, {
+ diceCount: 1,
+ average: { average: -5 },
+ variance: { variance: 0 },
+ bounds: {
+ low: -5,
+ high: -5,
+ expectLow: true,
+ }
+ }, 10)
+ })
+})
+
describe('basic dice', () => {
describeBasicDie(1, 6)
describeBasicDie(2, 8, 500)
diff --git a/src/dice.js b/src/dice.js
index c45bafc..ead3d16 100644
--- a/src/dice.js
+++ b/src/dice.js
@@ -1,9 +1,14 @@
+const constant = n => () => [n]
+
const d = (number, sides) => {
return () => {
let pool = []
- for (let i = 0; i < number; i++) {
- pool.push(1 + Math.floor(Math.random() * sides))
+ const currentNumber = number()
+ const currentSides = sides()
+
+ for (let i = 0; i < currentNumber; i++) {
+ pool.push(1 + Math.floor(Math.random() * currentSides))
}
return pool
@@ -32,6 +37,7 @@ const roll = (die) => {
return die().reduce((a, b) => (a + b))
}
+exports.constant = constant
exports.d = d
exports.add = add
exports.subtract = subtract