m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--__tests__/dice.test.js23
-rw-r--r--__tests__/lexer.test.js10
-rw-r--r--__tests__/parser.test.js10
-rw-r--r--index.js2
-rw-r--r--src/dice.js16
-rw-r--r--src/lexer.js1
-rw-r--r--src/parser.js1
7 files changed, 62 insertions, 1 deletions
diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js
index 272f436..fb8a155 100644
--- a/__tests__/dice.test.js
+++ b/__tests__/dice.test.js
@@ -13,7 +13,8 @@ const {
keepLow,
again,
againUnder,
- threshold
+ threshold,
+ thresholdLow
} = require('../src/dice.js')
const defaultNumberRolls = 500
@@ -625,4 +626,24 @@ describe('threshold', () => {
}
})
})
+
+ describe('4t3d10', () => {
+ const die = thresholdLow(constant(4), d(constant(3), constant(10)))
+
+ testDie(die, {
+ diceCount: 3,
+ average: {
+ average: 1.2
+ },
+ variance: {
+ variance: 0.72
+ },
+ bounds: {
+ low: 0,
+ high: 3,
+ expectLow: true,
+ expectHigh: true
+ }
+ })
+ })
})
diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js
index 25ef816..b51c0f9 100644
--- a/__tests__/lexer.test.js
+++ b/__tests__/lexer.test.js
@@ -225,5 +225,15 @@ describe('lex', () => {
{ type: 'constant', value: 10 }
])
})
+
+ test('8t3d10', () => {
+ expect(lex('8t3d10')).toEqual([
+ { type: 'constant', value: 8 },
+ { type: 't' },
+ { type: 'constant', value: 3 },
+ { type: 'd' },
+ { type: 'constant', value: 10 }
+ ])
+ })
})
})
diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js
index ad67fc4..6e575b6 100644
--- a/__tests__/parser.test.js
+++ b/__tests__/parser.test.js
@@ -241,6 +241,16 @@ describe('parse', () => {
right: { type: 'constant', value: 8 }
}
})
+
+ expect(parse('7t4d8')).toEqual({
+ type: 't',
+ left: { type: 'constant', value: 7 },
+ right: {
+ type: 'd',
+ left: { type: 'constant', value: 4 },
+ right: { type: 'constant', value: 8 }
+ }
+ })
})
describe('parsing parentheses', () => {
diff --git a/index.js b/index.js
index 740b96e..6dd9a5f 100644
--- a/index.js
+++ b/index.js
@@ -21,6 +21,8 @@ const interpret = tree => {
return D.againUnder(interpret(tree.left), interpret(tree.right))
case 'T':
return D.threshold(interpret(tree.left), interpret(tree.right))
+ case 't':
+ return D.thresholdLow(interpret(tree.left), interpret(tree.right))
case 'add':
return D.add(interpret(tree.left), interpret(tree.right))
case 'subtract':
diff --git a/src/dice.js b/src/dice.js
index e580c9d..53d337d 100644
--- a/src/dice.js
+++ b/src/dice.js
@@ -214,6 +214,21 @@ const threshold = (die1, die2) => {
}
}
+const thresholdLow = (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
@@ -231,3 +246,4 @@ exports.keepLow = keepLow
exports.again = again
exports.againUnder = againUnder
exports.threshold = threshold
+exports.thresholdLow = thresholdLow
diff --git a/src/lexer.js b/src/lexer.js
index f33dc87..ba3d3a5 100644
--- a/src/lexer.js
+++ b/src/lexer.js
@@ -29,6 +29,7 @@ newLexemeType('k', 'k')
newLexemeType('A', 'A')
newLexemeType('a', 'a')
newLexemeType('T', 'T')
+newLexemeType('t', 't')
const lex = (expressionString) => {
let lexemes = []
diff --git a/src/parser.js b/src/parser.js
index f8e2a5b..adb4b9b 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -67,6 +67,7 @@ newDieOperation('k')
newDieOperation('A')
newDieOperation('a')
newDieOperation('T')
+newDieOperation('t')
newInfix('bigPlus', 20, { type: 'add' })
newInfix('bigMinus', 20, { type: 'subtract' })