From 5bb027bde84e0a8fdb01bfa680b511ad842498ef Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Wed, 23 Aug 2017 21:26:01 -0400 Subject: Implement threshold --- __tests__/dice.test.js | 25 ++++++++++++++++++++++++- src/dice.js | 17 +++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js index a18584c..12d9130 100644 --- a/__tests__/dice.test.js +++ b/__tests__/dice.test.js @@ -10,7 +10,8 @@ const { explode, keepHigh, keepLow, - again + again, + threshold } = require('../src/dice.js') const defaultNumberRolls = 500 @@ -560,3 +561,25 @@ describe('again', () => { }) }) }) + +describe('threshold', () => { + describe('8T3d10', () => { + const die = threshold(constant(8), d(constant(3), constant(10))) + + testDie(die, { + diceCount: 3, + average: { + average: 0.9 + }, + variance: { + variance: 0.63 + }, + bounds: { + low: 0, + high: 3, + expectLow: true, + expectHigh: true + } + }) + }) +}) diff --git a/src/dice.js b/src/dice.js index ef67edf..9bc53d7 100644 --- a/src/dice.js +++ b/src/dice.js @@ -147,6 +147,22 @@ const again = (die1, die2) => { } } +const threshold = (die1, die2) => { + return () => { + const cutoff = roll(die1) + + return die2().map(die => { + return () => { + if (die() >= cutoff) { + return 1 + } else { + return 0 + } + } + }) + } +} + exports.pool = pool exports.roll = roll @@ -161,3 +177,4 @@ exports.explode = explode exports.keepHigh = keepHigh exports.keepLow = keepLow exports.again = again +exports.threshold = threshold -- cgit v1.2.3