-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
92 lines (74 loc) · 2.04 KB
/
index.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict'
const settle = require('./settle.js')
const betting = require('./betting.js')
function rollD6 () {
return 1 + Math.floor(Math.random() * 6)
}
function shoot (before, dice) {
const sortedDice = dice.sort()
const after = {
die1: sortedDice[0],
die2: sortedDice[1],
diceSum: dice.reduce((m, r) => { return m + r }, 0)
}
// game logic based on: https://github.com/tphummel/dice-collector/blob/master/PyTom/Dice/logic.py
if (before.isComeOut) {
if ([2, 3, 12].indexOf(after.diceSum) !== -1) {
after.result = 'comeout loss'
after.isComeOut = true
} else if ([7, 11].indexOf(after.diceSum) !== -1) {
after.result = 'comeout win'
after.isComeOut = true
} else {
after.result = 'point set'
after.isComeOut = false
after.point = after.diceSum
}
} else {
if (before.point === after.diceSum) {
after.result = 'point win'
after.isComeOut = true
} else if (after.diceSum === 7) {
after.result = 'seven out'
after.isComeOut = true
} else {
after.result = 'neutral'
after.point = before.point
after.isComeOut = false
}
}
return after
}
function playHand ({ rules, bettingStrategy, roll = rollD6 }) {
const history = []
let balance = 0
let hand = {
isComeOut: true
}
let bets
while (hand.result !== 'seven out') {
bets = bettingStrategy({ rules, bets, hand })
balance -= bets.new
if (process.env.DEBUG && bets.new) console.log(`[bet] new bet $${bets.new} ($${balance})`)
delete bets.new
hand = shoot(
hand,
[roll(), roll()]
)
if (process.env.DEBUG) console.log(`[roll] ${hand.result} (${hand.diceSum})`)
bets = settle.all({ rules, bets, hand })
if (bets?.payouts?.total) {
balance += bets.payouts.total
if (process.env.DEBUG) console.log(`[payout] new payout $${bets.payouts.total} ($${balance})`)
delete bets.payouts
}
history.push(hand)
}
return { history, balance }
}
module.exports = {
rollD6,
shoot,
playHand,
betting
}