forked from rocketacademy/basics-blackjack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
671 lines (648 loc) · 26.9 KB
/
script.js
File metadata and controls
671 lines (648 loc) · 26.9 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
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
// DEMO for 6 Jun class 15-4
// Game modes stored in constants
const WAITING_FOR_NUM_PLAYERS = "waiting for num of players";
const WAITING_FOR_NAME = "waiting for username";
const INSTRUCTIONS = "tell player instructions";
const ASK_FOR_BET = "ask player for betting amount";
const TAKE_BET = "take user's bet";
const DEAL_CARDS = "deal cards";
const SELECT_PLAYER = "select player and check for blackjack";
const PLAYER_ACTION = "player hits or stands";
const CHANGE_ACE_MODE = "change ace mode";
const COMPUTER_CALCULATES = "computer calculates";
const FINAL_RESULT = "final result";
const GAME_SUMMARY = "output all results";
// Declare user inputs as constants
const INPUT_HIT = "hit";
const INPUT_STAND = "stand";
const INPUT_CHANGE_ACE = "ace";
const POSITION = "position";
const ACE_ONE = "ace 1";
const ACE_ELEVEN = "ace 11";
const INPUT_RESET = "reset";
// Global variables
var allPlayerOutcomes = [];
var allPlayerCards = [];
var playerCards = [];
var computerCards = [];
var cardDeck = [];
var shuffledDeck;
var createDeck;
var playerPoints = 0;
var computerPoints = 0;
var aceIndex = 0;
var aceValue = 0;
var aceCounter = 0;
var userName = [];
var bankRoll = [];
var userBet = [];
var numPlayers = 0;
var stringNames = "";
var stringBet = "";
var currentPlayer = 0;
// Current game mode
var gameMode = WAITING_FOR_NUM_PLAYERS;
// Create a deck of cards
var makeDeck = function () {
var suits = ["hearts", "diamonds", "clubs", "spades"];
var emojiSuits = ["♥️", "♦️", "♣️", "♠️"];
var suitIndex = 0;
while (suitIndex < suits.length) {
var currentSuit = suits[suitIndex];
var currentEmojiSuit = emojiSuits[suitIndex];
var rankCounter = 1;
while (rankCounter <= 13) {
// Create new object attribute for cards to represent their value
var cardValue = rankCounter;
if (rankCounter == 1) {
// Set ace to be 11 points, so that blackjack condition can be fulfilled. Player can change value later
cardValue = 11;
} else if (rankCounter == 11 || rankCounter == 12 || rankCounter == 13) {
cardValue = 10;
}
var cardName = rankCounter;
if (cardName == 1) {
cardName = "Ace";
} else if (cardName == 11) {
cardName = "Jack";
} else if (cardName == 12) {
cardName = "Queen";
} else if (cardName == 13) {
cardName = "King";
}
var card = {
name: cardName,
suit: currentSuit,
emojiSuit: currentEmojiSuit,
rank: rankCounter,
value: cardValue,
};
cardDeck.push(card);
rankCounter += 1;
}
suitIndex += 1;
}
return cardDeck;
};
var getRandomIndex = function (max) {
return Math.floor(Math.random() * max);
};
// Shuffle deck of cards
var shuffleDeck = function (cardDeck) {
var currentIndex = 0;
while (currentIndex < cardDeck.length) {
var randomIndex = getRandomIndex(cardDeck.length);
var randomCard = cardDeck[randomIndex];
var currentCard = cardDeck[currentIndex];
cardDeck[currentIndex] = randomCard;
cardDeck[randomIndex] = currentCard;
currentIndex = currentIndex + 1;
}
return cardDeck;
};
var dealCards = function () {
// Deal two cards to each player as determined by user
for (var playerCounter = 0; playerCounter < numPlayers; playerCounter += 1) {
playerCards.push(shuffledDeck.pop());
playerCards.push(shuffledDeck.pop());
allPlayerCards.push(playerCards);
playerCards = [];
}
// Deal two cards to computer
computerCards.push(shuffledDeck.pop());
computerCards.push(shuffledDeck.pop());
// Add computer scores for first two cards
computerPoints = computerCards[0].value + computerCards[1].value;
return `The cards have been shuffled! <br> Click submit to see your cards!`;
};
var selectPlayer = function () {
playerCardList = generatePlayerCardList();
// Add relevant player's scores for first two cards
// Use global value 'currentPlayer' to change index and hence select relevant player's cards that are being accessed in array
playerPoints =
allPlayerCards[currentPlayer][0].value +
allPlayerCards[currentPlayer][1].value;
if (
// If player gets blackjack, declare winner
playerPoints == 21 &&
computerPoints != 21
) {
playerPoints = 0;
currentPlayer += 1;
// Change to next player / summarize results if all players have played
if (currentPlayer == numPlayers) {
gameMode = GAME_SUMMARY;
} else {
gameMode = SELECT_PLAYER;
}
// Update points
bankRoll[currentPlayer - 1] =
bankRoll[currentPlayer - 1] + userBet[currentPlayer - 1] * 1.5;
allPlayerOutcomes.push("win");
return `${
userName[currentPlayer - 1]
}! <br>YOU HAVE GOTTEN BLACKJACK AND WIN! 🥳 <br><br> Your cards: <br>${
allPlayerCards[currentPlayer - 1][0].name
} of ${allPlayerCards[currentPlayer - 1][0].emojiSuit} <br>${
allPlayerCards[currentPlayer - 1][1].name
} of ${
allPlayerCards[currentPlayer - 1][1].emojiSuit
} <br><br> Your bank $${
bankRoll[currentPlayer - 1]
} <br><br> Time for the next player! `;
} else if (
// If both get blackjack, declare draw
playerPoints == 21 &&
computerPoints == 21
) {
playerPoints = 0;
currentPlayer += 1;
// Change to next player / summarize results if all players have played
if (currentPlayer == numPlayers) {
gameMode = GAME_SUMMARY;
} else {
gameMode = SELECT_PLAYER;
}
bankRoll[currentPlayer - 1] =
bankRoll[currentPlayer - 1] - userBet[currentPlayer - 1];
allPlayerOutcomes.push("draw");
return `${
userName[currentPlayer - 1]
}!<br> You both drew blackjacks! Its a draw! 😐 <br><br> Your cards: <br>${
allPlayerCards[currentPlayer - 1][0].name
} of ${allPlayerCards[currentPlayer - 1][0].emojiSuit} <br>${
allPlayerCards[currentPlayer - 1][1].name
} of ${
allPlayerCards[currentPlayer - 1][1].emojiSuit
} <br><br> Your bank $${
bankRoll[currentPlayer - 1]
} <br><br> Time for the next player!`;
} else if (playerPoints > 21) {
// If player gets two aces, inform them to change value
gameMode = PLAYER_ACTION;
return `${userName[currentPlayer]}, you will bust and lose unless you change your ace card's value! <br><br> Type 'ace' to change its value.<br><br> Your cards: <br>${allPlayerCards[currentPlayer][0].name} of ${allPlayerCards[currentPlayer][0].emojiSuit} <br>${allPlayerCards[currentPlayer][1].name} of ${allPlayerCards[currentPlayer][1].emojiSuit} <br> Your points: ${playerPoints}`;
} else {
gameMode = PLAYER_ACTION;
// Otherwise inform player of current cards and ask if they want to hit, stand or change ace value
return `${userName[currentPlayer]}, your cards are: <br>${allPlayerCards[currentPlayer][0].name} of ${allPlayerCards[currentPlayer][0].emojiSuit} <br>${allPlayerCards[currentPlayer][1].name} of ${allPlayerCards[currentPlayer][1].emojiSuit} <br> Your points: ${playerPoints} <br><br> Please enter 'hit' if you would like more cards <br><br> Please enter 'stand' if you do not want anymore cards <br><br> Please enter 'ace' if you would like to change the value of an ace card<br><br> Please enter 'reset' if you would like to restart the game`;
}
};
// If player wants to change ace value, ensure they are selecting ace card in array, otherwise inform them of this condition
var checkForAce = function (input) {
if (gameMode == CHANGE_ACE_MODE) {
aceIndex = Number(input) - 1;
if (allPlayerCards[currentPlayer][aceIndex].name != "Ace") {
gameMode = PLAYER_ACTION;
// Run while loop to output all player card names and suits in output statement
var aceInvalidOutput = `${userName[currentPlayer]}, you can only change the value of aces. <br><br> Your cards are: `;
aceCounter = 0;
while (aceCounter < allPlayerCards[currentPlayer].length) {
aceInvalidOutput =
aceInvalidOutput +
`<br> ${allPlayerCards[currentPlayer][aceCounter].name} of ${allPlayerCards[currentPlayer][aceCounter].emojiSuit}`;
aceCounter += 1;
}
return (
aceInvalidOutput +
`<br> Your points: ${playerPoints}<br><br> Please enter 'hit' if you would like more cards <br><br> Please enter 'stand' if you do not want anymore cards <br><br> Please enter 'ace' if you would like to change the value of an ace card`
);
// If player selects ace card in their array, switch mode and allow them to change ace value
} else if (allPlayerCards[currentPlayer][aceIndex].name == "Ace") {
gameMode = PLAYER_ACTION;
return `${userName[currentPlayer]}, you have chosen to change the value of the ace card in positon: ${input} <br><br> Please type either: <br><br> 'ace 1' to change the value of ace to 1 <br> 'ace 11' to change the value of ace to 11`;
}
}
};
// If ace card is correctly selected, allow player to enter string to change ace value
var changeAceValue = function (input) {
if (input == ACE_ELEVEN) {
aceValue = Number(11);
playerPoints = playerPoints + 10;
} else if (input == ACE_ONE) {
aceValue = Number(1);
playerPoints = playerPoints - 10;
}
allPlayerCards[currentPlayer][aceIndex].value = aceValue;
gameMode = PLAYER_ACTION;
// Run while loop to output all player card names and suits in output statement
var aceChangeOutput = `${userName[currentPlayer]}, you have changed the value of ${allPlayerCards[currentPlayer][aceIndex].name} of ${allPlayerCards[currentPlayer][aceIndex].emojiSuit} to ${aceValue}. <br><br> Your cards are: `;
aceCounter = 0;
while (aceCounter < allPlayerCards[currentPlayer].length) {
aceChangeOutput =
aceChangeOutput +
`<br> ${allPlayerCards[currentPlayer][aceCounter].name} of ${allPlayerCards[currentPlayer][aceCounter].emojiSuit}`;
aceCounter += 1;
}
return (
aceChangeOutput +
`<br> Your points: ${playerPoints}<br><br> Please enter 'hit' if you would like more cards <br><br> Please enter 'stand' if you do not want anymore cards <br><br> Please enter 'ace' if you would like to change the value of an ace card<br><br> Please enter 'reset' if you would like to restart the game`
);
};
// Allow player to draw extra card
var playerDrawsExtraCard = function () {
playerCardList = generatePlayerCardList();
allPlayerCards[currentPlayer].push(shuffledDeck.pop());
var playerCardMessage = `Your cards are: `;
var endMessage = "";
// Update player points
playerCounter = allPlayerCards[currentPlayer].length - 1;
while (playerCounter < allPlayerCards[currentPlayer].length) {
playerPoints =
playerPoints + allPlayerCards[currentPlayer][playerCounter].value;
endMessage = `<br> Your points: ${playerPoints}<br><br> Please enter 'hit' if you would like more cards <br><br> Please enter 'stand' if you do not want anymore cards <br><br> Please enter 'ace' if you would like to change the value of an ace card<br><br> Please enter 'reset' if you would like to restart the game`;
playerCounter += 1;
}
aceCounter = 0;
while (aceCounter < allPlayerCards[currentPlayer].length) {
playerCardMessage =
playerCardMessage +
`<br> ${allPlayerCards[currentPlayer][aceCounter].name} of ${allPlayerCards[currentPlayer][aceCounter].emojiSuit}`;
aceCounter += 1;
}
// If player busts, output results
if (playerPoints > 21) {
var bustOutput = "";
// However, if player draws ace, allow them to change value
counter = allPlayerCards[currentPlayer].length - 1;
while (counter < allPlayerCards[currentPlayer].length) {
if (allPlayerCards[currentPlayer][counter].name == "Ace") {
gameMode = PLAYER_ACTION;
return `${userName[currentPlayer]}, you will bust and lose unless you change your ace card's value! <br><br> Type 'ace' to change its value.<br><br> ${playerCardMessage}`;
}
counter += 1;
}
// Also, if player busts and has a previous ace card with value 11, allow them to change value
secondCounter = 0;
while (secondCounter < allPlayerCards[currentPlayer].length) {
if (
allPlayerCards[currentPlayer][secondCounter].name == "Ace" &&
allPlayerCards[currentPlayer][secondCounter].value == 11
) {
gameMode = PLAYER_ACTION;
return `${userName[currentPlayer]}, you will bust and lose unless you change your ace card's value! <br><br> Type 'ace' to change its value.<br><br> ${playerCardMessage}`;
}
secondCounter += 1;
}
currentPlayer += 1;
allPlayerOutcomes.push("lose");
bankRoll[currentPlayer - 1] =
bankRoll[currentPlayer - 1] - userBet[currentPlayer - 1];
bustOutput = `${
userName[currentPlayer - 1]
}, you've bust and lost! 😭 <br><br> ${playerCardMessage} <br> Your points: ${playerPoints}<br><br> Click submit for the next player's turn!<br><br> Your bank $${
bankRoll[currentPlayer - 1]
} `;
// Change to next player / summarize results if all players have played
if (currentPlayer == numPlayers) {
playerPoints = 0;
gameMode = GAME_SUMMARY;
} else {
playerPoints = 0;
gameMode = SELECT_PLAYER;
}
return bustOutput;
}
// If player gets blackjack, output results
if (playerPoints == 21) {
currentPlayer += 1;
bankRoll[currentPlayer - 1] =
bankRoll[currentPlayer - 1] + userBet[currentPlayer - 1];
allPlayerOutcomes.push("win");
// Change to next player / summarize results if all players have played
if (currentPlayer == numPlayers) {
playerPoints = 0;
gameMode = GAME_SUMMARY;
} else {
playerPoints = 0;
gameMode = SELECT_PLAYER;
}
return `${
userName[currentPlayer - 1]
}, you've gotten blackjack and won! 🥳 <br><br>${playerCardMessage}<br><br> Your bank $${
bankRoll[currentPlayer - 1]
} <br><br> Click submit for the next player's turn!`;
}
return `${userName[currentPlayer]}, ${playerCardMessage} ${endMessage};`;
};
// Calculate computer's score and automatically draw more if necessary
var calculateComputerScore = function () {
playerCardList = generatePlayerCardList();
// If two card score >= 17 and < 21, change game mode to compare with player's score
if (computerPoints >= 17 && computerPoints < 21) {
gameMode = FINAL_RESULT;
}
// If computer gets blackjack, output results
else if (computerPoints == 21) {
allPlayerOutcomes.push("lose");
currentPlayer += 1;
bankRoll[currentPlayer - 1] =
bankRoll[currentPlayer - 1] - userBet[currentPlayer - 1];
// Change to next player / summarize results if all players have played
if (currentPlayer == numPlayers) {
playerPoints = 0;
gameMode = GAME_SUMMARY;
} else {
playerPoints = 0;
gameMode = SELECT_PLAYER;
}
return ` ${
userName[currentPlayer - 1]
}! <br> ${playerCardList} <br><br> Find out if you've won at the end of the game! <br><br> Click submit for the next player's turn
`;
} else {
// Otherwise computer draws card until it reaches at least 17, gets blackjack or busts
while (computerPoints < 17) {
computerCards.push(shuffledDeck.pop());
computerPoints =
computerPoints + computerCards[computerCards.length - 1].value;
// Run while loop to generate all computer card names and suits
var computerCardMessage = `Computer's cards were: `;
computerCounter = 0;
while (computerCounter < computerCards.length) {
computerCardMessage =
computerCardMessage +
`<br> ${computerCards[computerCounter].name} of ${computerCards[computerCounter].emojiSuit}`;
computerCounter += 1;
}
// If computer gets at least 17, change game mode and compare with player
if (computerPoints >= 17 && computerPoints < 21) {
gameMode = FINAL_RESULT;
}
// If computer gets blackjack after drawing cards, return winning statement and reset game
if (computerPoints == 21) {
allPlayerOutcomes.push("lose");
playerPoints = 0;
currentPlayer += 1;
bankRoll[currentPlayer - 1] =
bankRoll[currentPlayer - 1] - userBet[currentPlayer - 1];
// Change to next player / summarize results if all players have played
if (currentPlayer == numPlayers) {
gameMode = GAME_SUMMARY;
} else {
gameMode = SELECT_PLAYER;
}
return ` ${
userName[currentPlayer - 1]
}! <br> ${playerCardList} <br><br> Find out if you've won at the end of the game! <br><br> Click submit for the next player's turn`;
}
// If computer busts, output results
if (computerPoints > 21) {
allPlayerOutcomes.push("win");
playerPoints = 0;
currentPlayer += 1;
bankRoll[currentPlayer - 1] =
bankRoll[currentPlayer - 1] + userBet[currentPlayer - 1];
// Change to next player / summarize results if all players have played
if (currentPlayer == numPlayers) {
gameMode = GAME_SUMMARY;
} else {
gameMode = SELECT_PLAYER;
}
return ` ${
userName[currentPlayer - 1]
}! <br> ${playerCardList} <br><br> Find out if you've won at the end of the game! <br><br> Click submit for the next player's turn`;
}
}
}
};
// If above conditions are all not met, compare player results to computer results
var determineFinalResult = function () {
// Run while loop to generate all player card names and suits
playerCardList = generatePlayerCardList();
currentPlayer += 1;
// Determine player's outcome
if (playerPoints > computerPoints) {
allPlayerOutcomes.push("win");
bankRoll[currentPlayer - 1] =
bankRoll[currentPlayer - 1] + userBet[currentPlayer - 1];
} else if (playerPoints < computerPoints) {
allPlayerOutcomes.push("lose");
bankRoll[currentPlayer - 1] =
bankRoll[currentPlayer - 1] - userBet[currentPlayer - 1];
} else if (playerPoints == computerPoints) {
allPlayerOutcomes.push("draw");
bankRoll[currentPlayer - 1] =
bankRoll[currentPlayer - 1] - userBet[currentPlayer - 1];
}
// Change to next player / summarize results if all players have played
if (currentPlayer == numPlayers) {
playerPoints = 0;
gameMode = GAME_SUMMARY;
} else {
playerPoints = 0;
gameMode = SELECT_PLAYER;
}
return ` ${
userName[currentPlayer - 1]
}! <br> ${playerCardList} <br><br> Find out if you've won at the end of the game! <br><br> Click submit for the next player's turn`;
};
// After all player's turns, display final results before starting new round and asking for new bets
var displayGameSummary = function () {
gameMode = ASK_FOR_BET;
var outerSummaryCounter = 0;
var summaryOutput = "";
// Outer loop to access index in main array
while (outerSummaryCounter < allPlayerCards.length) {
var summaryCounter = 0;
summaryOutput =
summaryOutput +
`<br><br>Player ${outerSummaryCounter + 1}: ${
userName[outerSummaryCounter]
}, you: ${allPlayerOutcomes[outerSummaryCounter]}! <br> Your bank: ${
bankRoll[outerSummaryCounter]
} <br> Your cards were: `;
// Inner loop to access index in sub arrays
while (summaryCounter < allPlayerCards[outerSummaryCounter].length) {
summaryOutput =
summaryOutput +
`<br> ${allPlayerCards[outerSummaryCounter][summaryCounter].name} of ${allPlayerCards[outerSummaryCounter][summaryCounter].emojiSuit} `;
summaryCounter += 1;
}
outerSummaryCounter += 1;
}
var computerCardMessage = `Computer's cards are: `;
computerCounter = 0;
while (computerCounter < computerCards.length) {
computerCardMessage =
computerCardMessage +
`<br> ${computerCards[computerCounter].name} of ${computerCards[computerCounter].emojiSuit}`;
computerCounter += 1;
}
return (
summaryOutput +
`<br><br>` +
computerCardMessage +
`<br><br> Please click submit to start another game. <br> Please enter 'reset' if you would like to reset the game and choose a different number of players`
);
};
// // Generate list of player cards to be used for various output statements for output formatting
var generatePlayerCardList = function () {
aceCounter = 0;
var playerCardMessage = `Your cards are: `;
while (aceCounter < allPlayerCards[currentPlayer].length) {
playerCardMessage =
playerCardMessage +
`<br> ${allPlayerCards[currentPlayer][aceCounter].name} of ${allPlayerCards[currentPlayer][aceCounter].emojiSuit}`;
aceCounter += 1;
}
return playerCardMessage;
};
var playerCardList = "";
// Generate list of usernames to be used in main for output formatting
var generateUserNameList = function () {
var nameOutput = ``;
for (var nameCounter = 0; nameCounter < userName.length; nameCounter += 1) {
nameOutput =
nameOutput + `<br> Player ${nameCounter + 1}: ${userName[nameCounter]}`;
}
return nameOutput;
};
var main = function (input) {
var myOutputValue = "";
// Ask for number of players
if (gameMode == WAITING_FOR_NUM_PLAYERS) {
gameMode = WAITING_FOR_NAME;
myOutputValue = `Hello player(s)! <br><br> Please enter the number of people playing`;
} else if (gameMode == WAITING_FOR_NAME) {
// Empty global variables if players want to change no of players and reset game
playerPoints = 0;
computerPoints = 0;
allPlayerOutcomes = [];
allPlayerCards = [];
computerCards = [];
userName = [];
userBet = [];
bankRoll = [];
currentPlayer = 0;
// Take in number of players and convert to integer
numPlayers = Number(input);
// Use loop to store bank roll for each player in an array
for (var bankCounter = 0; bankCounter < numPlayers; bankCounter += 1) {
bankRoll.push(100);
}
myOutputValue = `You have chosen to player with ${numPlayers} players <br><br> Please enter all your names with a spacing and without a comma!!`;
gameMode = INSTRUCTIONS;
} else if (gameMode == INSTRUCTIONS) {
// Take in all player's names and convert into individual strings in an array
stringNames = input;
userName = stringNames.split(" ");
var userNameList = generateUserNameList();
gameMode = ASK_FOR_BET;
myOutputValue = `Hello Players! <br> ${userNameList} <br><br> Welcome to Blackjack!!! 2️⃣1️⃣♠️❤️♣️♦️ <br><br> These are the rules:<br> You will be dealt two cards after clicking submit <br> The aim is to get 21 points without exceeding it & to score more points than the computer <br> You need to hit a minimum of 17 points <br> Aces are worth either 1 or 11 and can be changed throughout the game <br><br> Press submit to continue`;
} else if (gameMode == ASK_FOR_BET) {
// Empty global variables for subsequent rounds
currentPlayer = 0;
playerPoints = 0;
computerPoints = 0;
allPlayerOutcomes = [];
allPlayerCards = [];
computerCards = [];
userBet = [];
gameMode = TAKE_BET;
// Ask player for individual bet amount
myOutputValue = `${userName}, before we begin enter an amount that each of you would like to bet! <br> Please enter the amount with a space and without a comma`;
} else if (gameMode == TAKE_BET) {
gameMode = DEAL_CARDS;
// Take in user bets and convert into individual integer in an array
stringBet = input;
userBet = stringBet.split(" ").map(Number);
myOutputValue = `${userName}, you have chosen to bet $${userBet} this round respectively. <br><br> Your current bank totals are $${bankRoll}.
<br><br> When you're ready click submit to play! `;
} else if (gameMode == DEAL_CARDS) {
gameMode = SELECT_PLAYER;
// Create a deck of cards
createDeck = makeDeck();
// Shuffle deck of cards
shuffledDeck = shuffleDeck(cardDeck);
// Deal two cards to each player and computer
myOutputValue = dealCards();
}
if (gameMode == SELECT_PLAYER) {
gameMode = PLAYER_ACTION;
// Selec appropriate patient's cards and check for blackjack, otherwise change allow player to choose their next action
myOutputValue = selectPlayer();
}
if (gameMode == PLAYER_ACTION) {
if (input == INPUT_CHANGE_ACE) {
// Ask player to type in position that the ace card is in so that it can be checked if it is really an ace
gameMode = CHANGE_ACE_MODE;
return `${
userName[currentPlayer]
}, please type in the position of the ace that you want to change <br><br> ${generatePlayerCardList()}`;
}
// Once, correct (ace) card is selected, change player's ace card based on input and update player's points
if (input == ACE_ELEVEN || input == ACE_ONE) {
myOutputValue = changeAceValue(input);
}
// Allow player to draw extra card and update player's points
if (input == INPUT_HIT) {
myOutputValue = playerDrawsExtraCard();
}
// Allow player to stand and switch to next mode
if (input == INPUT_STAND) {
// Ensure that player meets minimum game score required, else change game mode back
if (playerPoints < 17) {
gameMode = PLAYER_ACTION;
return `${
userName[currentPlayer]
}, you need at least 17 points. <br><br>Please type 'hit' to draw another card. <br><br> ${generatePlayerCardList()}`;
}
gameMode = COMPUTER_CALCULATES;
}
}
if (gameMode == CHANGE_ACE_MODE) {
// Ensure player has selected an ace card
myOutputValue = checkForAce(input);
}
if (gameMode == COMPUTER_CALCULATES) {
// Check if computer has enough cards and add if necessary
myOutputValue = calculateComputerScore();
}
if (gameMode == FINAL_RESULT) {
// Compare player and computer cards and determine winner
myOutputValue = determineFinalResult();
if (currentPlayer == numPlayers) {
gameMode = GAME_SUMMARY;
} else {
gameMode = SELECT_PLAYER;
}
}
// Allow player to reset entire game and reselect number of players and usernames
if (input == INPUT_RESET) {
gameMode = WAITING_FOR_NAME;
myOutputValue =
myOutputValue = `Hello player(s)! <br><br> Please enter the number of people playing`;
}
// Output final results of all players
if (gameMode == GAME_SUMMARY) {
myOutputValue = displayGameSummary();
gameMode = ASK_FOR_BET;
}
return myOutputValue;
};
var inputDisplay = function () {
// Change input message based on mode
if (gameMode == WAITING_FOR_NUM_PLAYERS) {
return "WELCOME TO SHANNON'S CASINO! <br> <br>Click submit to start playing!:";
} else if (gameMode == WAITING_FOR_NAME) {
return "How many of you are playing today?:";
} else if (gameMode == INSTRUCTIONS) {
return `Wow! ${numPlayers} players! <br><br> What are your names?:`;
} else if (gameMode == ASK_FOR_BET) {
return "The game is about to begin!: ";
} else if (gameMode == TAKE_BET) {
return "Place your bets!:";
} else if (gameMode == DEAL_CARDS) {
return "Feeling rich huh?:";
} else if (gameMode == SELECT_PLAYER) {
return "What would you like to do?:";
} else if (gameMode == PLAYER_ACTION) {
return "What would you like to do?:";
} else if (gameMode == CHANGE_ACE_MODE) {
return "Hmmm... Let me check if you have an ace:";
} else if (gameMode == FINAL_RESULT) {
return "Tabulating results!:";
} else if (gameMode == GAME_SUMMARY) {
return "The final results are!:";
}
};