m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/dice.js
blob: c8a25729b966f33fa17ff85464770c2b2bd7bab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const constant = n => () => [n]

const roll = (die) => {
  return die().reduce((a, b) => (a + b))
}

const d = (number, sides) => {
  return () => {
    let pool = []

    const currentNumber = roll(number)
    const currentSides = roll(sides)

    for (let i = 0; i < currentNumber; i++) {
      pool.push(1 + Math.floor(Math.random() * currentSides))
    }

    return pool
  }
}

const add = (die1, die2) => {
  return () => {
    return die1().concat(die2())
  }
}

const negative = (die) => {
  return () => {
    return die().map(value => -value)
  }
}

const subtract = (die1, die2) => {
  return () => {
    return die1().concat(negative(die2)())
  }
}

exports.constant = constant
exports.d = d
exports.add = add
exports.subtract = subtract
exports.roll = roll