forked from kedoska/engine-blackjack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
340 lines (311 loc) · 10.2 KB
/
engine.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
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
// @flow
/*!
engine-blackjack
Copyright (C) 2016 Marco Casula
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import luckyLucky from './paytables/luchyLuchy'
import * as TYPES from './constants'
import type { SideBets, Card, Hand, HandInfo, HandValue } from './types'
export const isNull = (obj: ?any): boolean => obj === null
export const isUndefined = (obj: ?any): boolean => obj === undefined
export const isNullOrUndef = (obj: ?any): boolean => isUndefined(obj) || isNull(obj)
export const calculate = (array: Array<Card>): HandValue => {
if (array.length === 1) {
if (isNullOrUndef(array[0])) {
return null
}
const value = array[0].value
return {
hi: value === 1 ? 11 : value,
lo: value === 1 ? 1 : value
}
}
const aces = []
const value = array.reduce((memo, x) => {
if (x.value === 1) {
aces.push(1)
return memo
}
memo += x.value
return memo
}, 0)
return aces.reduce((memo) => {
if ((memo.hi + 11) <= 21) {
memo.hi += 11
memo.lo += 1
} else {
memo.hi += 1
memo.lo += 1
}
if (memo.hi > 21 && memo.lo <= 21) {
memo.hi = memo.lo
}
return memo
}, {
hi: value,
lo: value
})
}
export const getHigherValidValue = (handValue: HandValue):number => handValue.hi <= 21 ? handValue.hi : handValue.lo
export const checkForBusted = (handValue: HandValue): boolean => (handValue.hi > 21) && (handValue.lo === handValue.hi)
export const isBlackjack = (array: Array<Card>): boolean => array.length === 2 && calculate(array).hi === 21
export const isSoftHand = (array: Array<Card>): boolean => {
return array.some(x => x.value === 1) &&
array
.reduce((memo, x) => {
memo += (x.value === 1 && memo < 11) ? 11 : x.value
return memo
}, 0) === 17
}
export const isSuited = (array: Array<Card> = []): boolean => {
if (array.length === 0) {
return false
}
const suite = array[0].suite
return array.every(x => x.suite === suite)
}
export const countCards = (array: Array<Card>) => {
const systems = {
'Hi-Lo': [ -1, 1, 1, 1, 1, 1, 0, 0, 0, -1, -1, -1, -1 ]
}
return array.reduce((memo, x) => {
memo += systems['Hi-Lo'][x.value - 1]
return memo
}, 0)
}
export const getHandInfo = (playerCards: Array<Card>, dealerCards: Array<Card>, hasSplit:boolean = false): Hand => {
const handValue = calculate(playerCards)
if (!handValue) {
return null
}
const hasBlackjack = isBlackjack(playerCards) && hasSplit === false
const hasBusted = checkForBusted(handValue)
const isClosed = hasBusted || hasBlackjack || handValue.hi === 21
const canDoubleDown = !isClosed && true
const canSplit = playerCards.length > 1 && playerCards[ 0 ].value === playerCards[ 1 ].value && !isClosed
const canInsure = dealerCards[ 0 ].value === 1 && !isClosed
return {
cards: playerCards,
playerValue: handValue,
playerHasBlackjack: hasBlackjack,
playerHasBusted: hasBusted,
playerHasSurrendered: false,
close: isClosed,
availableActions: {
double: canDoubleDown,
split: canSplit,
insurance: canInsure,
hit: !isClosed,
stand: !isClosed,
surrender: !isClosed
}
}
}
export const getHandInfoAfterDeal = (playerCards: Array<Card>, dealerCards: Array<Card>, initialBet: number): Hand => {
const hand = getHandInfo(playerCards, dealerCards)
hand.bet = initialBet
// After deal, even if we got a blackjack the hand cannot be considered closed.
const availableActions = hand.availableActions
hand.availableActions = {
...availableActions,
stand: true,
hit: true,
surrender: true
}
return {
...hand,
close: hand.playerHasBlackjack
}
}
export const getHandInfoAfterSplit = (playerCards: Array<Card>, dealerCards: Array<Card>, initialBet: number): Hand => {
const hand = getHandInfo(playerCards, dealerCards, true)
const availableActions = hand.availableActions
hand.availableActions = {
...availableActions,
split: false,
double: !hand.close && (playerCards.length === 2),
insurance: false,
surrender: false
}
hand.bet = initialBet
return hand
}
export const getHandInfoAfterHit = (playerCards: Array<Card>, dealerCards: Array<Card>, initialBet: number, hasSplit: boolean): Hand => {
const hand = getHandInfo(playerCards, dealerCards, hasSplit)
const availableActions = hand.availableActions
hand.availableActions = {
...availableActions,
double: (playerCards.length === 2),
split: false,
insurance: false,
surrender: false
}
hand.bet = initialBet
return hand
}
export const getHandInfoAfterDouble = (playerCards: Array<Card>, dealerCards: Array<Card>, initialBet: number, hasSplit: boolean): Hand => {
const hand = getHandInfoAfterHit(playerCards, dealerCards, initialBet, hasSplit)
const availableActions = hand.availableActions
hand.availableActions = {
...availableActions,
hit: false,
stand: false
}
hand.bet = initialBet * 2
return {
...hand,
close: true
}
}
export const getHandInfoAfterStand = (handInfo: Hand): Hand => {
return {
...handInfo,
close: true,
availableActions: {
double: false,
split: false,
insurance: false,
hit: false,
stand: false,
surrender: false
}
}
}
export const getHandInfoAfterSurrender = (handInfo: Hand): Hand => {
const hand = getHandInfoAfterStand(handInfo)
return {
...hand,
playerHasSurrendered: true,
close: true
}
}
export const getHandInfoAfterInsurance = (playerCards: Array<Card>, dealerCards: Array<Card>): Hand => {
const hand = getHandInfo(playerCards, dealerCards)
const availableActions = hand.availableActions
hand.availableActions = {
...availableActions,
stand: true,
hit: true,
surrender: true,
insurance: false
}
return {
...hand,
close: hand.playerHasBlackjack
}
}
export const isLuckyLucky = (playerCards: Array<Card>, dealerCards: Array<Card>): boolean => {
// Player hand and dealer's up card sum to 19, 20, or 21 ("Lucky Lucky")
const v1 = calculate(playerCards).hi + calculate(dealerCards).hi
const v2 = calculate(playerCards).lo + calculate(dealerCards).lo
const v3 = calculate(playerCards).hi + calculate(dealerCards).lo
const v4 = calculate(playerCards).lo + calculate(dealerCards).hi
return (v1 >= 19 && v1 <= 21) || (v2 >= 19 && v2 <= 21) || (v3 >= 19 && v3 <= 21) || (v4 >= 19 && v4 <= 21)
}
export const getLuckyLuckyMultiplier = (playerCards: Array<Card>, dealerCards: Array<Card>) => {
const cards = [].concat(playerCards, dealerCards)
const isSameSuite = isSuited(cards)
const flatCards = cards.map(x => x.value).join('')
const value = calculate(cards)
return luckyLucky(flatCards, isSameSuite, value)
}
export const isPerfectPairs = (playerCards: Array<Card>): boolean => playerCards[0].value === playerCards[1].value
export const getSideBetsInfo = (availableBets: SideBets, sideBets: SideBets, playerCards: Array<Card>, dealerCards: Array<Card>): any => {
const sideBetsInfo = {
luckyLucky: 0,
perfectPairs: 0
}
if (availableBets.luckyLucky && sideBets.luckyLucky && isLuckyLucky(playerCards, dealerCards)) {
const multiplier = getLuckyLuckyMultiplier(playerCards, dealerCards)
sideBetsInfo.luckyLucky = sideBets.luckyLucky * multiplier
}
if (availableBets.perfectPairs && sideBets.perfectPairs && isPerfectPairs(playerCards)) {
// TODO: impl colored pairs
// TODO: impl mixed pairs
sideBetsInfo.perfectPairs = sideBets.perfectPairs * 5
}
return sideBetsInfo
}
export const isActionAllowed = (actionName: string, stage: string): boolean => {
if (actionName === TYPES.RESTORE) {
return true
}
switch (stage) {
case TYPES.STAGE_READY: {
return [TYPES.RESTORE, TYPES.DEAL].indexOf(actionName) > -1
}
case TYPES.STAGE_PLAYER_TURN_RIGHT: {
return [TYPES.STAND, TYPES.INSURANCE, TYPES.SURRENDER, TYPES.SPLIT, TYPES.HIT, TYPES.DOUBLE].indexOf(actionName) > -1
}
case TYPES.STAGE_PLAYER_TURN_LEFT: {
return [TYPES.STAND, TYPES.HIT, TYPES.DOUBLE].indexOf(actionName) > -1
}
case TYPES.SHOWDOWN: {
return [TYPES.SHOWDOWN, TYPES.STAND].indexOf(actionName) > -1
}
case TYPES.STAGE_DEALER_TURN: {
return [TYPES.DEALER_HIT].indexOf(actionName) > -1
}
default: {
return false
}
}
}
export const getPrize = (playerHand: Hand, dealerCards: Array<Card>): number => {
const {
close = false,
playerHasSurrendered = true,
playerHasBlackjack = false,
playerHasBusted = true,
playerValue = {},
bet = 0
} = playerHand
const higherValidDealerValue = getHigherValidValue(calculate(dealerCards))
const dealerHasBlackjack = isBlackjack(dealerCards)
if (!close) {
return 0
}
if (playerHasBusted) {
return 0
}
if (playerHasSurrendered) {
return bet / 2
}
if (playerHasBlackjack && !dealerHasBlackjack) {
return bet + (bet * 1.5)
}
const dealerHasBusted = higherValidDealerValue > 21
if (dealerHasBusted) {
return (bet + bet)
}
const higherValidPlayerValue = getHigherValidValue(playerValue)
if (higherValidPlayerValue > higherValidDealerValue) {
return (bet + bet)
} else if (higherValidPlayerValue === higherValidDealerValue) {
return bet
}
return 0
}
export const getPrizes = ({ history, handInfo: { left, right }, dealerCards }: { history: Array<any>, handInfo: HandInfo, dealerCards: Array<Card>}) => {
const finalBet = history.reduce((memo, x) => {
memo += x.value
return memo
}, 0)
const wonOnRight = getPrize(right, dealerCards)
const wonOnLeft = getPrize(left, dealerCards)
return {
finalBet: finalBet,
wonOnRight: wonOnRight,
wonOnLeft: wonOnLeft
}
}