-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhands.js
More file actions
404 lines (367 loc) Β· 13.2 KB
/
hands.js
File metadata and controls
404 lines (367 loc) Β· 13.2 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env node
'use strict'
const { playHand } = require('./index.js')
const bettingStrategies = require('./betting.js')
function simulateHands ({ numHands, bettingStrategy, showDetail }) {
const summaryTemplate = {
balance: 0,
rollCount: 0,
pointsSet: 0,
pointsWon: 0,
comeOutWins: 0,
comeOutLosses: 0,
netComeOutWins: 0,
neutrals: 0,
comeLineWins: 0,
comeLineLosses: 0,
comeOddsWins: 0,
comeOddsLosses: 0,
placeSixWins: 0,
placeSixLosses: 0,
placeEightWins: 0,
placeEightLosses: 0,
dist: {
2: { ct: 0, prob: 1 / 36 },
3: { ct: 0, prob: 2 / 36 },
4: { ct: 0, prob: 3 / 36 },
5: { ct: 0, prob: 4 / 36 },
6: { ct: 0, prob: 5 / 36 },
7: { ct: 0, prob: 6 / 36 },
8: { ct: 0, prob: 5 / 36 },
9: { ct: 0, prob: 4 / 36 },
10: { ct: 0, prob: 3 / 36 },
11: { ct: 0, prob: 2 / 36 },
12: { ct: 0, prob: 1 / 36 }
}
}
const sessionSummary = Object.assign({}, summaryTemplate)
const hands = []
const rules = {
minBet: 15,
maxOddsMultiple: {
4: 3,
5: 4,
6: 5,
8: 5,
9: 4,
10: 3
}
}
for (let i = 0; i < numHands; i++) {
if (process.env.DEBUG) {
console.log(`\n================ HAND ${i + 1} ================`)
}
const hand = playHand({ rules, bettingStrategy: bettingStrategies[bettingStrategy] })
hand.summary = Object.assign({}, summaryTemplate)
sessionSummary.balance += hand.balance
hand.summary.balance = hand.balance
hand.history.reduce((memo, roll) => {
memo.rollCount++
hand.summary.rollCount++
memo.dist[roll.diceSum].ct++
switch (roll.result) {
case 'neutral':
memo.neutrals++
hand.summary.neutrals++
break
case 'point set':
memo.pointsSet++
hand.summary.pointsSet++
break
case 'point win':
memo.pointsWon++
hand.summary.pointsWon++
break
case 'comeout win':
memo.comeOutWins++
hand.summary.comeOutWins++
memo.netComeOutWins++
hand.summary.netComeOutWins++
break
case 'comeout loss':
memo.comeOutLosses++
hand.summary.comeOutLosses++
memo.netComeOutWins--
hand.summary.netComeOutWins--
break
}
if (Array.isArray(roll.payouts)) {
roll.payouts.forEach(p => {
if (p.type === 'place 6 win') {
memo.placeSixWins++
hand.summary.placeSixWins++
} else if (p.type === 'place 8 win') {
memo.placeEightWins++
hand.summary.placeEightWins++
} else if (p.type === 'come line win') {
memo.comeLineWins++
hand.summary.comeLineWins++
} else if (p.type === 'come odds win') {
memo.comeOddsWins++
hand.summary.comeOddsWins++
}
})
}
if (roll.result === 'seven out') {
if (roll.betsBefore?.place?.six) {
memo.placeSixLosses++
hand.summary.placeSixLosses++
}
if (roll.betsBefore?.place?.eight) {
memo.placeEightLosses++
hand.summary.placeEightLosses++
}
if (roll.betsBefore?.come?.points) {
Object.keys(roll.betsBefore.come.points).forEach(point => {
roll.betsBefore.come.points[point].forEach(bet => {
memo.comeLineLosses++
hand.summary.comeLineLosses++
if (bet.odds) {
memo.comeOddsLosses++
hand.summary.comeOddsLosses++
}
})
})
}
}
return memo
}, sessionSummary)
hands.push(hand)
}
sessionSummary.handCount = hands.length
for (const k of Object.keys(sessionSummary.dist)) {
sessionSummary.dist[k].ref = Number((sessionSummary.dist[k].prob * sessionSummary.rollCount).toFixed(1))
sessionSummary.dist[k].diff = Number((sessionSummary.dist[k].ct - sessionSummary.dist[k].ref).toFixed(1))
sessionSummary.dist[k].diff_pct = Number((((sessionSummary.dist[k].ct - sessionSummary.dist[k].ref) / sessionSummary.dist[k].ref) * 100).toFixed(1))
if (showDetail) {
sessionSummary.dist[k].ref_work = `${(sessionSummary.dist[k].prob * sessionSummary.rollCount).toFixed(1)} (${sessionSummary.rollCount} * ${sessionSummary.dist[k].prob.toFixed(2)})`
}
delete sessionSummary.dist[k].prob
}
return { sessionSummary, hands, rules }
}
function printResults ({ sessionSummary, hands, showDetail, rules }) {
console.log(`[table rules] minimum bet: $${rules.minBet}`)
console.log('\nDice Roll Distribution')
console.table(sessionSummary.dist)
delete sessionSummary.dist
console.log('\nSession Summary')
console.table(sessionSummary)
console.log('\nHands Summary')
console.table(hands.map(hand => {
delete hand.summary.dist
return hand.summary
}))
if (showDetail) {
console.log('\nHands')
hands.forEach((hand, handNum) => {
console.log(`\nHand: ${handNum + 1}, Balance: $${hand.balance}`)
const formattedHistory = hand.history.map((roll, rollIndex) => {
const formatted = {}
// Get game state BEFORE this roll (from previous roll or initial state)
let beforeIsComeOut, beforePoint
if (rollIndex === 0) {
// First roll - use initial hand state
beforeIsComeOut = true
beforePoint = undefined
} else {
// Use previous roll's after state
const prevRoll = hand.history[rollIndex - 1]
beforeIsComeOut = prevRoll.isComeOut
beforePoint = prevRoll.point
}
// Calculate total money in play
let moneyInPlay = 0
if (roll.betsBefore) {
// Pass line and odds
if (roll.betsBefore.pass?.line) {
moneyInPlay += roll.betsBefore.pass.line.amount
}
if (roll.betsBefore.pass?.odds) {
moneyInPlay += roll.betsBefore.pass.odds.amount
}
// Place bets
if (roll.betsBefore.place?.six) {
moneyInPlay += roll.betsBefore.place.six.amount
}
if (roll.betsBefore.place?.eight) {
moneyInPlay += roll.betsBefore.place.eight.amount
}
// Come bets
if (roll.betsBefore.come?.points) {
Object.values(roll.betsBefore.come.points).forEach(pointBets => {
pointBets.forEach(bet => {
if (bet.line) moneyInPlay += bet.line.amount
if (bet.odds) moneyInPlay += bet.odds.amount
})
})
}
if (roll.betsBefore.come?.pending?.length) {
moneyInPlay += roll.betsBefore.come.pending[0].amount
}
}
// 1. Format pass line and odds details
formatted.passLine = ''
if (roll.betsBefore?.pass?.line) {
const line = `$${roll.betsBefore.pass.line.amount}`
const odds = roll.betsBefore.pass.odds ? `+$${roll.betsBefore.pass.odds.amount}` : ''
formatted.passLine = line + odds
}
// 2. Format place 6,8 details
formatted.placeBets = ''
const placeBets = []
if (roll.betsBefore?.place?.six) {
placeBets.push(`6:$${roll.betsBefore.place.six.amount}`)
}
if (roll.betsBefore?.place?.eight) {
placeBets.push(`8:$${roll.betsBefore.place.eight.amount}`)
}
if (placeBets.length > 0) {
formatted.placeBets = placeBets.join(' ')
}
// 3. Format come bets (both established and pending)
formatted.comeBets = ''
const comeBetParts = []
if (roll.betsBefore?.come?.points) {
const comePoints = Object.keys(roll.betsBefore.come.points).map(point => {
const bets = roll.betsBefore.come.points[point]
return bets.map(bet => {
const line = `${point}:$${bet.line.amount}`
const odds = bet.odds ? `+$${bet.odds.amount}` : ''
return line + odds
}).join(',')
}).join(' ')
if (comePoints) comeBetParts.push(comePoints)
}
if (roll.betsBefore?.come?.pending?.length) {
comeBetParts.push(`P:$${roll.betsBefore.come.pending[0].amount}`)
}
if (comeBetParts.length > 0) {
formatted.comeBets = comeBetParts.join(' ')
}
// 5. Add money in play total
formatted.moneyInPlay = `$${moneyInPlay}`
// 6. Add game state BEFORE the roll
formatted.beforeIsComeOut = beforeIsComeOut
formatted.beforePoint = beforePoint ?? ''
// 7. Add roll details (combined dice display)
formatted.roll = `${roll.diceSum} (${roll.die1},${roll.die2})`
// 8. Add result
formatted.result = roll.result
// 9. Add game state AFTER the roll
formatted.afterIsComeOut = roll.isComeOut
formatted.afterPoint = roll.point ?? ''
// 8. Calculate losses based on betsBefore and result
formatted.losses = ''
const losses = []
let totalLosses = 0
if (roll.result === 'seven out') {
// Pass line + odds lost
if (roll.betsBefore?.pass?.line) {
const loss = roll.betsBefore.pass.line.amount
losses.push(`pass:$${loss}`)
totalLosses += loss
}
if (roll.betsBefore?.pass?.odds) {
const loss = roll.betsBefore.pass.odds.amount
losses.push(`pass-odds:$${loss}`)
totalLosses += loss
}
// All come bets lost
if (roll.betsBefore?.come?.points) {
Object.keys(roll.betsBefore.come.points).forEach(point => {
roll.betsBefore.come.points[point].forEach(bet => {
const lineLoss = bet.line.amount
losses.push(`come-${point}:$${lineLoss}`)
totalLosses += lineLoss
if (bet.odds) {
const oddsLoss = bet.odds.amount
losses.push(`come-${point}-odds:$${oddsLoss}`)
totalLosses += oddsLoss
}
})
})
}
// Place bets lost
if (roll.betsBefore?.place?.six) {
const loss = roll.betsBefore.place.six.amount
losses.push(`place-6:$${loss}`)
totalLosses += loss
}
if (roll.betsBefore?.place?.eight) {
const loss = roll.betsBefore.place.eight.amount
losses.push(`place-8:$${loss}`)
totalLosses += loss
}
} else if (roll.result === 'comeout loss') {
// Only pass line lost
if (roll.betsBefore?.pass?.line) {
const loss = roll.betsBefore.pass.line.amount
losses.push(`pass:$${loss}`)
totalLosses += loss
}
} else if (roll.betsBefore?.come?.pending?.length) {
// Check for immediate come bet losses (2, 3, 12)
if ([2, 3, 12].includes(roll.diceSum)) {
const loss = roll.betsBefore.come.pending[0].amount
losses.push(`come:$${loss}`)
totalLosses += loss
}
}
if (losses.length > 0) {
formatted.losses = losses.join(', ')
}
// 9. Track come bet movements (pending -> established on point)
formatted.movements = ''
if (roll.betsBefore?.come?.pending?.length) {
const pendingAmount = roll.betsBefore.come.pending[0].amount
// If dice sum is not an immediate win/loss, it establishes on that point
if (![7, 11, 2, 3, 12].includes(roll.diceSum)) {
formatted.movements = `comeβ${roll.diceSum}:$${pendingAmount}`
}
}
// 10. Format payouts to show wins
formatted.wins = ''
let totalWins = 0
if (Array.isArray(roll.payouts)) {
formatted.wins = roll.payouts.map(p => {
const winAmount = p.principal + p.profit
totalWins += winAmount
return `${p.type}:$${winAmount}`
}).join(', ')
}
// 11. Calculate net change
formatted.netChange = `$${totalWins - totalLosses}`
// Return with columns in desired order
return {
passLine: formatted.passLine,
comeBets: formatted.comeBets,
placeBets: formatted.placeBets,
moneyInPlay: formatted.moneyInPlay,
beforeIsComeOut: formatted.beforeIsComeOut,
beforePoint: formatted.beforePoint,
roll: formatted.roll,
result: formatted.result,
afterIsComeOut: formatted.afterIsComeOut,
afterPoint: formatted.afterPoint,
movements: formatted.movements,
wins: formatted.wins,
losses: formatted.losses,
netChange: formatted.netChange
}
})
console.table(formattedHistory)
})
}
}
if (require.main === module) {
const numHands = parseInt(process.argv[2], 10)
const bettingStrategy = process.argv[3]
const showDetail = process.argv[4]
console.log(`Simulating ${numHands} Craps Hand(s)`)
console.log(`Using betting strategy: ${bettingStrategy}`)
const result = simulateHands({ numHands, showDetail, bettingStrategy })
printResults({ ...result, showDetail })
} else {
module.exports = simulateHands
}