diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-08-22 16:58:42 -0400 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-08-22 16:58:42 -0400 |
commit | 1ae29437d03ea010da1b87dc12fd155d414e9c1f (patch) | |
tree | 9f31aea7741c7e4cd7bae1512886a9b927137933 | |
parent | 9fcc2b892602f7a964d9c260680b4faab6ee81a4 (diff) |
Implment keepLow
-rw-r--r-- | __tests__/dice.test.js | 26 | ||||
-rw-r--r-- | src/dice.js | 20 |
2 files changed, 46 insertions, 0 deletions
diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js index 6fb48a2..3073fee 100644 --- a/__tests__/dice.test.js +++ b/__tests__/dice.test.js @@ -7,6 +7,7 @@ const { negative, explode, keepHigh, + keepLow, bonusAdd } = require('../src/dice.js') @@ -406,6 +407,31 @@ describe('keep', () => { const die = keepHigh(constant(2), d(constant(1), constant(20))) testDie(die, basicDieTestSpecs(1, 20)) }) + + describe('1k2d20', () => { + const die = keepLow(constant(1), d(constant(2), constant(20))) + + testDie(die, { + diceCount: 1, + average: { + average: 7.175 + }, + variance: { + variance: 22.194 + }, + bounds: { + low: 1, + high: 20, + expectLow: true, + expectHigh: true + } + }, 800) + }) + + describe('2k1d20', () => { + const die = keepLow(constant(2), d(constant(1), constant(20))) + testDie(die, basicDieTestSpecs(1, 20)) + }) }) describe('bonusAdd', () => { diff --git a/src/dice.js b/src/dice.js index 14addd4..becf7dc 100644 --- a/src/dice.js +++ b/src/dice.js @@ -75,6 +75,25 @@ const keepHigh = (die1, die2) => { } } +const keepLow = (die1, die2) => { + return () => { + const numberToKeep = roll(die1) + let rolls = die2().map(die => [die, die()]) + rolls.sort((a, b) => (a[1] - b[1])) + return rolls.slice(0, numberToKeep).map(pair => { + let returnedOriginal = false + return () => { + if (!returnedOriginal) { + returnedOriginal = true + return pair[1] + } else { + return pair[0]() + } + } + }) + } +} + const bonusAdd = (die1, die2) => { return () => { return die1().map(die => { @@ -92,4 +111,5 @@ exports.subtract = subtract exports.negative = negative exports.explode = explode exports.keepHigh = keepHigh +exports.keepLow = keepLow exports.bonusAdd = bonusAdd |