m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-20 13:13:30 -0400
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-20 13:13:30 -0400
commit427149c9a4c4e0b389520d67199af01ad3c3f0c2 (patch)
treee1356f5633c51a92a0fa286a16f74d7d57f85e5e
parentddc5f52e7591834d291f9728da6480fd4af3a864 (diff)
Implement keepHigh
-rw-r--r--__tests__/dice.test.js30
-rw-r--r--src/dice.js20
2 files changed, 49 insertions, 1 deletions
diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js
index 1274b3d..1be0744 100644
--- a/__tests__/dice.test.js
+++ b/__tests__/dice.test.js
@@ -5,7 +5,8 @@ const {
add,
subtract,
negative,
- explode
+ explode,
+ keepHigh
} = require('../src/dice.js')
const defaultNumberRolls = 500
@@ -378,3 +379,30 @@ describe('exploding dice', () => {
})
})
})
+
+describe('keep', () => {
+ describe('1K2d20', () => {
+ const die = keepHigh(constant(1), d(constant(2), constant(20)))
+
+ testDie(die, {
+ diceCount: 1,
+ average: {
+ average: 13.825
+ },
+ variance: {
+ variance: 22.104
+ },
+ bounds: {
+ low: 1,
+ high: 20,
+ expectLow: true,
+ expectHigh: true
+ }
+ }, 800)
+ })
+
+ describe('2K1d20', () => {
+ const die = keepHigh(constant(2), d(constant(1), constant(20)))
+ testDie(die, basicDieTestSpecs(1, 20))
+ })
+})
diff --git a/src/dice.js b/src/dice.js
index b60af68..5e3b975 100644
--- a/src/dice.js
+++ b/src/dice.js
@@ -56,6 +56,25 @@ const explode = (die1, die2) => {
}
}
+const keepHigh = (die1, die2) => {
+ return () => {
+ const numberToKeep = roll(die1)
+ let rolls = die2().map(die => [die, die()])
+ rolls.sort((a, b) => (a[1] - b[1])).reverse()
+ return rolls.slice(0, numberToKeep).map(pair => {
+ let returnedOriginal = false
+ return () => {
+ if (!returnedOriginal) {
+ returnedOriginal = true
+ return pair[1]
+ } else {
+ return pair[0]()
+ }
+ }
+ })
+ }
+}
+
exports.pool = pool
exports.roll = roll
exports.constant = constant
@@ -64,3 +83,4 @@ exports.add = add
exports.subtract = subtract
exports.negative = negative
exports.explode = explode
+exports.keepHigh = keepHigh