-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blackjack.js
578 lines (465 loc) · 11 KB
/
Blackjack.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
export default class Blackjack {
/**
* @property {array} dealerHand - The dealer's hand.
*/
dealerHand;
/**
* @property {number} dealerScore - The dealer's score.
*/
dealerScore;
/**
* @property {boolean} dealerStays - True if dealer doesn't want to hit.
*/
dealerStays;
/**
* @property {array} gameOver - True if game is over.
*/
gameOver;
/**
* @property {number} playerBank - The player's bank.
*/
playerBank;
/**
* @property {number} playerBet - The player's bet.
*/
playerBet;
/**
* @property {number} playerHand - The player's hand.
*/
playerHand;
/**
* @property {number} playerStays - True if player doesn't want to hit.
*/
playerStays;
/**
* @property {array} rounds - All game rounds.
*/
rounds = [];
/**
* @property {array} roundOver - True if round is over.
*/
roundStarted;
/**
* @property {array} shoe - The shoe of cards.
*/
shoe;
/**
* @property {string} winner - The winner
*/
winner;
constructor() {
// Set game over.
this.gameOver = false;
// Set player bank.
this.playerBank = 100;
// Set player bet.
this.playerBet = 10;
// Set stays.
this.dealerStays = false;
this.playerStays = false;
// Set hands.
this.dealerHand = [];
this.playerHand = [];
// Set scores.
this.dealerScore = 0;
this.playerScore = 0;
// Set round started.
this.roundStarted = false;
// Set winner.
this.winner = false;
// Fill shoe.
this.fillShoe();
}
/**
* Check for winner.
*/
checkForWinner = async () => {
// If not round started, return.
if (!this.roundStarted) return;
// If dealer score is > 21.
if (this.dealerScore > 21) {
// Set winner.
this.winner = "Player";
// End round.
await this.endRound();
return;
}
// If player score is > 21.
if (this.playerScore > 21) {
// Set winner.
this.winner = "Dealer";
// End round.
await this.endRound();
return;
}
// If dealer score is 21.
if (this.dealerScore === 21) {
// Set winner.
this.winner = "Dealer";
// Call dealer stay.
this.dealerStay();
// End round.
await this.endRound();
return;
}
// If player score is 21.
if (this.playerScore === 21) {
// Set winner.
this.winner = "Player";
// Call player stay.
this.playerStay();
// End round.
await this.endRound();
return;
}
// If not dealer stays.
if (!this.dealerStays) {
return;
}
// If dealer score is greater than player score.
if (this.dealerScore > this.playerScore) {
// Set winner.
this.winner = "Dealer";
// End round.
await this.endRound();
return;
}
// If player score is greater than player score.
if (this.playerScore > this.dealerScore) {
// Set winner.
this.winner = "Player";
// End round.
await this.endRound();
return;
}
// If dealer score is equal to player score.
if (this.dealerScore === this.playerScore) {
// Set winner.
this.winner = "Dealer";
// End round.
await this.endRound();
return;
}
};
/**
* Removes a card from the deck and adds it to the dealer's hand.
*/
dealerHit = async () => {
// If dealer stays, return.
if (this.dealerStays) return;
// If dealer score greater than 16.
if (this.dealerScore > 16) {
// Call dealer stay.
await this.dealerStay();
return;
}
// Get the card.
const card = await this.shoe.pop();
// Deal the card.
this.dealerHand.push(card);
// Score hands.
await this.scoreHands();
// Check for winner.
await this.checkForWinner();
};
/**
* Sets dealer stay.
*/
dealerStay = async () => {
// Set dealer stay.
this.dealerStays = true;
};
/**
* End the game.
*/
endGame = () => {
// Set game over.
this.gameOver = true;
};
/**
* Ends a round.
*/
endRound = async () => {
// If not round started, return.
if (!this.roundStarted) return;
// Push current hands and winner to rounds.
this.rounds.push({
dealerHand: this.dealerHand,
dealerScore: this.dealerScore,
dealerStays: this.dealerStays,
playerHand: this.playerHand,
playerScore: this.playerScore,
playerStays: this.playerStays,
winner: this.winner,
});
// Set round started.
this.roundStarted = false;
// If dealer is winner.
if (this.winner === "Dealer") {
// Mod player bank.
await this.playerBankMod(-1 * this.playerBet);
}
// If player is winner.
if (this.winner === "Player") {
// Mod player bank.
await this.playerBankMod(this.playerBet);
}
};
/**
* Fills the shoe.
*/
fillShoe = async () => {
/**
* Creates a deck of cards.
*
* @returns {array} deck
*/
const deckOfCards = () => {
// Define deck.
const deck = [];
// Define card suits.
const cardSuits = ["CLUBS", "DIAMONDS", "HEARTS", "SPADES"];
// Define card names.
const cardNames = [
"Ace",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"Jack",
"Queen",
"King",
];
// Loop through suits and names.
for (const cardSuit of cardSuits) {
for (const cardName of cardNames) {
deck.push({
name: cardName,
suit: cardSuit,
});
}
}
const shuffleArray = (arr) => {
let currentIndex = arr.length,
randomIndex;
// While there remain elements to shuffle...
while (currentIndex != 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[arr[currentIndex], arr[randomIndex]] = [
arr[randomIndex],
arr[currentIndex],
];
}
return arr;
};
return shuffleArray(deck);
};
// Define shoe.
this.shoe = [
...deckOfCards(),
...deckOfCards(),
...deckOfCards(),
...deckOfCards(),
...deckOfCards(),
...deckOfCards(),
];
};
/**
* Gets the dealer's hand.
*/
getDealerHand = () => {
// Define hand.
const hand = this.dealerHand.slice();
// If not dealer stays.
if (!this.dealerStays) {
// Splice array.
hand.splice(0, 1, { name: false, suit: false });
}
return hand;
};
/**
* Gets the player bet.
*
* @returns playerBet
*/
getPlayerHand = () => {
return this.playerBet;
};
/**
* Gets the player's hand.
*
* @returns playerHand
*/
getPlayerHand = () => {
return this.playerHand;
};
/**
* Gets the player score.
*
* @returns playerScore
*/
getPlayerScore = () => {
return this.playerScore;
};
/**
* Adds amount to the player bank.
*
* @param {number} amount
*/
playerBankMod = async (amount) => {
// Add amount to player bank.
this.playerBank += amount;
};
/**
* Removes a card from the deck and adds it to the player's hand.
*/
playerHit = async () => {
// If player stays, return.
if (this.playerStays) return;
// Get the card.
const card = await this.shoe.pop();
// Deal the card.
this.playerHand.push(card);
// Score hands.
await this.scoreHands();
// Check for winner.
await this.checkForWinner();
};
/**
* Signals a player is staying and it is now the dealer's turn.
*/
playerStay = async () => {
// Set player stays.
this.playerStays = true;
// While not winner.
while (!this.winner) {
// If not dealer stays.
if (!this.dealerStays) {
// Dealer hit.
await this.dealerHit();
}
// Check for winner.
await this.checkForWinner();
}
};
/** Score hands. */
scoreHands = async () => {
// If dealer hand or player hand length is zero, return.
if (this.dealerHand.length === 0 || this.playerHand.length == 0) return;
// Define scores.
const scores = [this.dealerHand, this.playerHand].map((hand) => {
// Pull aces out of the hand.
const aces = hand.filter((card) => card.name === "Ace");
// Score all non-aces.
const nonAcesScore = hand
.filter((card) => card.name !== "Ace")
.map((card) => (isNaN(card.name) ? 10 : parseInt(card.name)))
.reduce((a, b) => a + b, 0);
// Calculate lowest possible score.
let score = (aces && aces.length * 1) + nonAcesScore;
// Loop over aces to determine highest possible score.
aces.forEach(() => {
// If ace can be valued at 11.
if (score + 10 <= 21) {
// Increment score.
score += 10;
}
});
return score;
});
// Set dealer score.
this.dealerScore = scores[0];
// Set player score.
this.playerScore = scores[1];
};
/**
* Sets the player's bet amount.
*
* @param {number} amount
*/
setPlayerBet = (amount) => {
// If round started, return.
if (this.roundStarted) return;
// Set player bet.
this.playerBet = amount;
};
/**
* Starts a new round.
*/
startRound = async () => {
// If player bank is less than player bet.
if (this.playerBank < this.playerBet) {
// End game.
this.endGame();
return;
}
// If round started, return.
if (this.roundStarted) return;
// If less than 20 cards in shoe.
if (this.shoe.length < 20) {
// Fill shoe.
await this.fillShoe();
}
// Set hands.
this.dealerHand = [];
this.playerHand = [];
// Set scores.
this.dealerScore = 0;
this.playerScore = 0;
// Set stays.
this.dealerStays = false;
this.playerStays = false;
// Set round started.
this.roundStarted = true;
// Set winner.
this.winner = false;
// Inital deal.
await this.playerHit();
await this.dealerHit();
await this.playerHit();
await this.dealerHit();
// Score hands.
await this.scoreHands();
// Check for winner.
await this.checkForWinner();
};
test = async () => {
// Define i.
let i = 0;
// While not game over.
while (!this.gameOver) {
// Start a round.
await this.startRound();
// While not winner.
while (!this.winner) {
// If player score greater than 17.
if (this.playerScore > 17) {
// Call player stay.
await this.playerStay();
} else {
//Call player hit.
await this.playerHit();
}
}
if (this.playerScore > 21 && this.dealerScore > 21) {
console.log(this.rounds.slice(-1));
}
// Increment i.
i++;
// If i equals 5000.
if (i >= 5000) {
break;
}
}
// Log object.
console.log(this);
};
}