-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimalBlackjack.js
217 lines (208 loc) · 6.89 KB
/
optimalBlackjack.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
let maxHands;
let resplitAces;
const cards = new Map([["A", 1], ["2", 2], ["3", 3], ["4", 4], ["5", 5], ["6", 6], ["7", 7], ["8", 8], ["9", 9], ["10", 10], ["J", 10], ["Q", 10], ["K", 10]]);
function OptimalBlackjack(maxHands = 2, resplitAces = false) {
this.maxHands = maxHands;
this.resplitAces = resplitAces;
}
// playerCards is an array of cards, with values 2-10 and J, Q, K, A
// dealerCard is a single value from 2-10, J, Q, K, A
OptimalBlackjack.prototype.getAction = function getAction(playerCards, dealerCard, handCount = 1) {
const dealerValue = cards.get(dealerCard);
const playerValue = totalHand(playerCards);
if (shouldPlayerSplit(playerCards, dealerValue, handCount, this.maxHands, this.resplitAces)) {
return "split";
}
else if (shouldPlayerDouble(playerCards, playerValue, dealerValue)) {
return "double";
}
else if (shouldPlayerStand(playerValue, dealerValue)) {
return "stand";
}
else if (shouldPlayerHit(playerValue, dealerValue)) {
return "hit";
}
else {
console.log(`Player Hand: ${JSON.stringify(playerCards)}, Dealer Card: ${dealerCard}`);
throw new Error("No action was found");
}
}
function totalHand(playerCards) {
let handValue = { total: 0, soft: false };
let hasAce = false;
for (let i = 0; i < playerCards.length; i++) {
handValue.total += cards.get(playerCards[i]);
if (playerCards[i] === "A") {
hasAce = true;
}
}
if (hasAce && handValue.total <= 11) {
handValue.total += 10;
handValue.soft = true;
}
return handValue;
}
function shouldPlayerSplit(playerCards, dealerValue, handCount, maxHands, resplitAces) {
// Check is split is allowed
if (playerCards.length != 2 || playerCards[0] != playerCards[1] || handCount >= maxHands) {
return false;
}
switch (cards.get(playerCards[0])) {
// Always split aces and 8s when allowed
case 1:
if (resplitAces || handCount === 1) {
return true;
}
return false;
case 8:
return true;
case 2:
case 3:
case 7:
// Split when dealer less than 8 and not ace
// Assumes can double after split for 2 and 3
if (dealerValue < 8 && dealerValue != 1) {
return true;
}
return false;
case 4:
// Split when dealer has 5-6
// Assumes can double after split
if (dealerValue === 5 || dealerValue === 6) {
return true;
}
return false;
case 5:
case 10:
// Never split 5s or 10s
return false;
case 6:
// Split when dealer less than 7 and not ace
// Assumes can double after split
if (dealerValue < 7 && dealerValue != 1) {
return true;
}
return false;
case 9:
// Split unless dealer shows 7, 10 or ace
if (dealerValue != 7 && dealerValue != 10 && dealerValue != 1) {
return true;
}
return false;
}
}
function shouldPlayerDouble(playerCards, playerValue, dealerValue) {
if (playerCards.length != 2) {
// Can double only on any first two
return false;
}
if (playerValue.soft) {
switch (playerValue.total) {
case 13:
if (dealerValue === 6) { return true; }
return false;
case 14:
case 15:
if (5 <= dealerValue && dealerValue <= 6) { return true; }
return false;
case 16:
if (4 <= dealerValue && dealerValue <= 6) { return true; }
return false;
case 17:
if (3 <= dealerValue && dealerValue <= 6) { return true; }
return false;
case 18:
if (2 <= dealerValue && dealerValue <= 6) { return true; }
return false;
default:
return false;
}
} else {
switch (playerValue.total) {
case 9:
if (3 <= dealerValue && dealerValue <= 6) { return true; }
return false;
case 10:
if (2 <= dealerValue && dealerValue <= 9) { return true; }
return false;
case 11:
if (2 <= dealerValue && dealerValue <= 10) { return true; }
return false;
default:
return false;
}
}
}
function shouldPlayerStand(playerValue, dealerValue) {
if (playerValue.soft) {
switch (playerValue.total) {
case 18:
// dealer 3-6 should double if allowed and will be caught by shouldPlayerDouble check first
if (2 <= dealerValue && dealerValue <= 8) {
return true;
}
return false;
case 19:
case 20:
case 21:
return true;
default:
return false;
}
} else {
if (playerValue.total >= 17) {
return true;
}
switch (playerValue.total) {
case 12:
if (4 <= dealerValue && dealerValue <= 6) { return true; }
return false;
case 13:
case 14:
case 15:
case 16:
if (2 <= dealerValue && dealerValue <= 6) { return true; }
return false;
default:
return false;
}
}
}
function shouldPlayerHit(playerValue, dealerValue) {
if (playerValue.soft) {
// various dealer cards would cause double to be optimal and will be caught by shouldPlayerDouble first
if (playerValue.total <= 17) {
return true;
}
else if (playerValue.total === 18) {
if (dealerValue === 9 || dealerValue === 10 || dealerValue === 1) {
return true;
}
return false;
} else {
return false;
}
} else {
// various dealer cards would cause double to be optimal and will be caught by shouldPlayerDouble first
if (playerValue.total <= 11) {
return true;
}
else if (playerValue.total === 12) {
if ((1 <= dealerValue && dealerValue <= 3) || (7 <= dealerValue && dealerValue <= 10)) {
return true;
} else {
return false;
}
}
else if (13 <= playerValue.total && playerValue.total <= 16) {
if ((7 <= dealerValue && dealerValue <= 10) || dealerValue === 1) {
return true;
} else {
return false;
}
} else if (playerValue.total >= 17) {
return false;
}
}
}
module.exports = OptimalBlackjack;