From 831e19699a8b604c81df999b743ac0ce4853f502 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Tue, 22 Aug 2017 18:48:45 -0400 Subject: Refactor --- __tests__/dice.test.js | 218 +++++++++++++++++++++++------------------------ __tests__/parser.test.js | 96 ++++++++++----------- src/dice.js | 38 ++++----- src/parser.js | 4 +- 4 files changed, 178 insertions(+), 178 deletions(-) diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js index 6ff86e7..15941cb 100644 --- a/__tests__/dice.test.js +++ b/__tests__/dice.test.js @@ -4,12 +4,12 @@ const { d, add, subtract, + bonusAdd, + bonusSubtract, negative, explode, keepHigh, - keepLow, - bonusAdd, - bonusSubtract + keepLow } = require('../src/dice.js') const defaultNumberRolls = 500 @@ -293,12 +293,6 @@ describe('add', () => { ]) }) -describe('negative', () => { - describeBasicDie(1, 6, defaultNumberRolls, true) - describeBasicDie(0, 6, defaultNumberRolls, true) - describeBasicDie(2, 8, defaultNumberRolls, true) -}) - describe('subtract', () => { describeCompoundDice([ { number: 1, sides: 6 }, @@ -317,6 +311,112 @@ describe('subtract', () => { }) }) +describe('bonusAdd', () => { + describe('1d20+3', () => { + const die = bonusAdd(d(constant(1), constant(20)), constant(3)) + testDie(die, { + diceCount: 1, + average: { + average: 13.5 + }, + variance: { + variance: 33.25 + }, + bounds: { + low: 4, + high: 23, + expectLow: true, + expectHigh: true + } + }) + }) + + describe('3d4+1', () => { + const die = bonusAdd(d(constant(3), constant(4)), constant(1)) + testDie(die, { + diceCount: 3, + average: { + average: 10.5 + }, + variance: { + variance: 3.75 + }, + bounds: { + low: 6, + high: 15, + expectLow: true, + expectHigh: true + } + }) + }) + + describe('2d6+1d6', () => { + const die = bonusAdd(d(constant(2), constant(6)), + d(constant(1), constant(6))) + testDie(die, { + diceCount: 2, + average: { + average: 14 + }, + variance: { + variance: 11.67 + }, + bounds: { + low: 4, + high: 24, + expectLow: false, + expectHigh: false + } + }) + }) +}) + +describe('bonusSubtract', () => { + describe('1d20-3', () => { + const die = bonusSubtract(d(constant(1), constant(20)), constant(3)) + testDie(die, { + diceCount: 1, + average: { + average: 7.5 + }, + variance: { + variance: 33.25 + }, + bounds: { + low: -2, + high: 17, + expectLow: true, + expectHigh: true + } + }) + }) + + describe('3d4-1', () => { + const die = bonusSubtract(d(constant(3), constant(4)), constant(1)) + testDie(die, { + diceCount: 3, + average: { + average: 4.5 + }, + variance: { + variance: 3.75 + }, + bounds: { + low: 0, + high: 9, + expectLow: true, + expectHigh: true + } + }) + }) +}) + +describe('negative', () => { + describeBasicDie(1, 6, defaultNumberRolls, true) + describeBasicDie(0, 6, defaultNumberRolls, true) + describeBasicDie(2, 8, defaultNumberRolls, true) +}) + describe('compound dice', () => { describe('(1d4)d(1d6)', () => { const die = d(d(constant(1), constant(4)), d(constant(1), constant(6))) @@ -434,103 +534,3 @@ describe('keep', () => { testDie(die, basicDieTestSpecs(1, 20)) }) }) - -describe('bonusAdd', () => { - describe('1d20+3', () => { - const die = bonusAdd(d(constant(1), constant(20)), constant(3)) - testDie(die, { - diceCount: 1, - average: { - average: 13.5 - }, - variance: { - variance: 33.25 - }, - bounds: { - low: 4, - high: 23, - expectLow: true, - expectHigh: true - } - }) - }) - - describe('3d4+1', () => { - const die = bonusAdd(d(constant(3), constant(4)), constant(1)) - testDie(die, { - diceCount: 3, - average: { - average: 10.5 - }, - variance: { - variance: 3.75 - }, - bounds: { - low: 6, - high: 15, - expectLow: true, - expectHigh: true - } - }) - }) - - describe('2d6+1d6', () => { - const die = bonusAdd(d(constant(2), constant(6)), - d(constant(1), constant(6))) - testDie(die, { - diceCount: 2, - average: { - average: 14 - }, - variance: { - variance: 11.67 - }, - bounds: { - low: 4, - high: 24, - expectLow: false, - expectHigh: false - } - }) - }) -}) - -describe('bonusSubtract', () => { - describe('1d20-3', () => { - const die = bonusSubtract(d(constant(1), constant(20)), constant(3)) - testDie(die, { - diceCount: 1, - average: { - average: 7.5 - }, - variance: { - variance: 33.25 - }, - bounds: { - low: -2, - high: 17, - expectLow: true, - expectHigh: true - } - }) - }) - - describe('3d4-1', () => { - const die = bonusSubtract(d(constant(3), constant(4)), constant(1)) - testDie(die, { - diceCount: 3, - average: { - average: 4.5 - }, - variance: { - variance: 3.75 - }, - bounds: { - low: 0, - high: 9, - expectLow: true, - expectHigh: true - } - }) - }) -}) diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js index fc47576..25edbfc 100644 --- a/__tests__/parser.test.js +++ b/__tests__/parser.test.js @@ -97,6 +97,54 @@ describe('parse', () => { }) }) + it('parses additive bonuses', () => { + expect(parse('3d4+1')).toEqual({ + type: 'bonusAdd', + left: { + type: 'd', + left: { type: 'constant', value: 3 }, + right: { type: 'constant', value: 4 } + }, + right: { type: 'constant', value: 1 } + }) + }) + + it('parses negative bonuses', () => { + expect(parse('3d4-1')).toEqual({ + type: 'bonusSubtract', + left: { + type: 'd', + left: { type: 'constant', value: 3 }, + right: { type: 'constant', value: 4 } + }, + right: { type: 'constant', value: 1 } + }) + }) + + test('bonus binds stronger than addition', () => { + expect(parse('2d6 + 2d6+2d6')).toEqual({ + type: 'add', + left: { + type: 'd', + left: { type: 'constant', value: 2 }, + right: { type: 'constant', value: 6 } + }, + right: { + type: 'bonusAdd', + left: { + type: 'd', + left: { type: 'constant', value: 2 }, + right: { type: 'constant', value: 6 } + }, + right: { + type: 'd', + left: { type: 'constant', value: 2 }, + right: { type: 'constant', value: 6 } + } + } + }) + }) + it('parses negatives', () => { expect(parse('-1 + (-(2d6))')).toEqual({ type: 'add', @@ -151,54 +199,6 @@ describe('parse', () => { }) }) - it('parses additive bonuses', () => { - expect(parse('3d4+1')).toEqual({ - type: 'bonusAdd', - left: { - type: 'd', - left: { type: 'constant', value: 3 }, - right: { type: 'constant', value: 4 } - }, - right: { type: 'constant', value: 1 } - }) - }) - - it('parses negative bonuses', () => { - expect(parse('3d4-1')).toEqual({ - type: 'bonusSubtract', - left: { - type: 'd', - left: { type: 'constant', value: 3 }, - right: { type: 'constant', value: 4 } - }, - right: { type: 'constant', value: 1 } - }) - }) - - test('bonus binds stronger than addition', () => { - expect(parse('2d6 + 2d6+2d6')).toEqual({ - type: 'add', - left: { - type: 'd', - left: { type: 'constant', value: 2 }, - right: { type: 'constant', value: 6 } - }, - right: { - type: 'bonusAdd', - left: { - type: 'd', - left: { type: 'constant', value: 2 }, - right: { type: 'constant', value: 6 } - }, - right: { - type: 'd', - left: { type: 'constant', value: 2 }, - right: { type: 'constant', value: 6 } - } - } - }) - }) - describe('parsing parentheses', () => { test('(1d6)d6', () => { expect(parse('(1d6)d6')).toEqual({ diff --git a/src/dice.js b/src/dice.js index 58467c4..6f3b7da 100644 --- a/src/dice.js +++ b/src/dice.js @@ -41,6 +41,23 @@ const subtract = (die1, die2) => { } } +const bonusAdd = (die1, die2) => { + return () => { + return die1().map(die => { + return () => die() + roll(die2) + }) + } +} + +const bonusSubtract = (die1, die2) => { + const negative2 = negative(die2) + return () => { + return die1().map(die => { + return () => die() + roll(negative2) + }) + } +} + const explode = (die1, die2) => { return () => { const explodeOn = roll(die1) @@ -94,32 +111,15 @@ const keepLow = (die1, die2) => { } } -const bonusAdd = (die1, die2) => { - return () => { - return die1().map(die => { - return () => die() + roll(die2) - }) - } -} - -const bonusSubtract = (die1, die2) => { - const negative2 = negative(die2) - return () => { - return die1().map(die => { - return () => die() + roll(negative2) - }) - } -} - exports.pool = pool exports.roll = roll exports.constant = constant exports.d = d exports.add = add exports.subtract = subtract +exports.bonusAdd = bonusAdd +exports.bonusSubtract = bonusSubtract exports.negative = negative exports.explode = explode exports.keepHigh = keepHigh exports.keepLow = keepLow -exports.bonusAdd = bonusAdd -exports.bonusSubtract = bonusSubtract diff --git a/src/parser.js b/src/parser.js index 344fb7d..a4665ac 100644 --- a/src/parser.js +++ b/src/parser.js @@ -57,7 +57,7 @@ newSymbol('d', (parser) => { return { type: 'd', left: { type: 'constant', value: 1 }, - right: parser.expression(29) + right: parser.expression(dieBindingPower - 1) } }) newDieOperation('E') @@ -65,9 +65,9 @@ newDieOperation('K') newDieOperation('k') newInfix('bigPlus', 20, { type: 'add' }) +newInfix('bigMinus', 20, { type: 'subtract' }) newInfix('plus', 25, { type: 'bonusAdd' }) newInfix('minus', 25, { type: 'bonusSubtract' }) -newInfix('bigMinus', 20, { type: 'subtract' }) newSymbol('minus', (parser) => { return { type: 'negative', -- cgit v1.2.3