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
410 lines (357 loc) · 11.3 KB
/
script.js
File metadata and controls
410 lines (357 loc) · 11.3 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
// Initialise static variables for game status
var GAME_STATUS_SHUFFLE_DECK = "SHUFFLE DECK";
var GAME_STATUS_DISPLAY_CARDS = "DISPLAY CARDS";
var GAME_STATUS_HUMAN_DECIDES = "HUMAN DECIDES";
var GAME_STATUS_RESULTS = "RESULTS";
// Initialise static variables for hit or stand
var HIT = "h";
var STAND = "s";
// Initialise game status
var gameStatus = GAME_STATUS_SHUFFLE_DECK;
// Initialise card arrays
var humanCards = [];
var dealerCards = [];
// Function to create the card deck representation as an array of objects
var makeDeck = function () {
// Initialise an empty deck array
var cardDeck = [];
// Intialise an arrage of the 4 suits in our deck. We will loop over this array.
var suits = ["hearts", "diamonds", "clubs", "spades"];
var suitEmoji = ["♣️", "♠️", "♦️", "♥️"];
var nameEmoji = [
"A",
"2️⃣",
"3️⃣",
"4️⃣",
"5️⃣",
"6️⃣",
"7️⃣",
"9️⃣",
"8️⃣",
"🔟",
"🕺🏽",
"👸🏽",
"🤴🏽",
];
// Loop over the suits array
var suitIndex = 0;
while (suitIndex < suits.length) {
// Store the current suit in a variable
var currentSuit = suits[suitIndex];
var currentEmoji = suitEmoji[suitIndex];
// Loop from 1 to 13 to create all cards for a given suit
// Notice rankCounter starts at 1 and not 0, and ends at 13 and not 12.
// This is an example of a loop without an array.
var rankCounter = 1;
while (rankCounter <= 13) {
// By default, the card name is the same as rankCounter
var cardName = rankCounter;
var currentRank = rankCounter;
var currentNameEmoji = nameEmoji[rankCounter - 1];
// if rank is 1, 11, 12, or 13, set cardName to the ace or face card's name
if (cardName == 1) {
cardName = "ace";
} else if (cardName == 11) {
cardName = "jack";
currentRank = 10;
} else if (cardName == 12) {
cardName = "queen";
currentRank = 10;
} else if (cardName == 13) {
cardName = "king";
currentRank = 10;
}
// Create a new card with the current name, suit, and rank
var card = {
name: cardName,
suit: currentSuit,
rank: currentRank,
emoji: currentEmoji,
nameEmoji: currentNameEmoji,
};
// Add the new card to the deck
cardDeck.push(card);
// Increment rankCounter to iterate over the next rank
rankCounter += 1;
}
// Increment the suit index to iterate over the next suit
suitIndex += 1;
}
// Return the completed card deck
return cardDeck;
};
// Initialise the card deck
var deck = makeDeck();
// Get a random index ranging from 0 (inclusive) to max (exclusive).
var getRandomIndex = function (max) {
return Math.floor(Math.random() * max);
};
// Shuffle the elements in the deck array
var shuffleCards = function (deck) {
// Loop over the card deck array once
var currentIndex = 0;
while (currentIndex < deck.length) {
// Select a random index in the deck
var randomIndex = getRandomIndex(deck.length);
// Select the card that corresponds to randomIndex
var randomCard = deck[randomIndex];
// Select the card that corresponds to currentIndex
var currentCard = deck[currentIndex];
// Swap positions of randomCard and currentCard in the deck
deck[currentIndex] = randomCard;
deck[randomIndex] = currentCard;
// Increment currentIndex
currentIndex = currentIndex + 1;
}
// Return the shuffled deck
return deck;
};
// get sum of an array of card objects
var cardSum = function (playerCards) {
var sum = 0;
var numOfAce = 0;
var counter = 0;
// calculate sum assuming all Aces are value 1 and counts how many aces there are in the hand
while (counter < playerCards.length) {
// ace counter
if (playerCards[counter].rank == 1) {
numOfAce += 1;
}
// sum of card rank
sum = sum + playerCards[counter].rank;
counter += 1;
}
// adjust sum if there is at least one ace
if (numOfAce > 0) {
// higher sum assuming 1 ace has value 11
var highSum = sum + 10;
if (highSum < 22) {
sum = highSum;
}
}
return sum;
};
// check for BlackJack
var checkBlackJack = function (playerCards) {
var ifBlackJack = Boolean;
var sum = cardSum(playerCards);
if (sum == 21) {
ifBlackJack = true;
} else {
ifBlackJack = false;
}
return ifBlackJack;
};
// check for bust
var checkBust = function (playerCards) {
var ifBust = Boolean;
var sum = cardSum(playerCards);
if (sum > 21) {
ifBust = true;
} else {
ifBust = false;
}
return ifBust;
};
// get which player has higher card sum
var getWinner = function (humanCards, dealerCards) {
var winner = "";
var humanCardSum = cardSum(humanCards);
var dealerCardSum = cardSum(dealerCards);
if (humanCardSum > dealerCardSum) {
winner = "human";
} else if (dealerCardSum > humanCardSum) {
winner = "dealer";
} else {
winner = "tie";
}
return winner;
};
// create display cards message
var displayCards = function (humanCards, dealerCards) {
var msg = "";
// if game ended, create message to display all cards
if (gameStatus == GAME_STATUS_RESULTS) {
var humanCardsCounter = 0;
// create message for all human cards
while (humanCardsCounter < humanCards.length) {
msg =
msg +
`Player card ${humanCardsCounter + 1}: ${
humanCards[humanCardsCounter].nameEmoji
} of ${humanCards[humanCardsCounter].emoji}. <br>`;
humanCardsCounter += 1;
}
msg = msg + "<br>";
var dealerCardsCounter = 0;
// add to message for all dealer cards
while (dealerCardsCounter < dealerCards.length) {
msg =
msg +
`Dealer card ${dealerCardsCounter + 1}: ${
dealerCards[dealerCardsCounter].nameEmoji
} of ${dealerCards[dealerCardsCounter].emoji}. <br>`;
dealerCardsCounter += 1;
}
msg = msg + "<br>";
// if human only has two cards, check for Black Jack
if (humanCards.length == 2) {
if (checkBlackJack(humanCards)) {
msg =
msg +
`Player hits Black Jack. PLAYER WINS. <br> Player total is ${cardSum(
humanCards
)}. Dealer total is ${cardSum(
dealerCards
)}. <br><br> Click submit to play again.`;
gameStatus = GAME_STATUS_SHUFFLE_DECK;
return msg;
// else check if dealer bust
} else if (checkBust(dealerCards)) {
msg =
msg +
`Dealer busts. PLAYER WINS. <br> Player total is ${cardSum(
humanCards
)}. Dealer total is ${cardSum(
dealerCards
)}. <br><br>Click submit to play again.`;
gameStatus = GAME_STATUS_SHUFFLE_DECK;
return msg;
}
// else if human has more than two cards, check if human busts
} else if (checkBust(humanCards)) {
msg =
msg +
`Player busts. DEALER WINS. <br> Player total is ${cardSum(
humanCards
)}. Dealer total is ${cardSum(
dealerCards
)}. <br><br>Click submit to play again.`;
gameStatus = GAME_STATUS_SHUFFLE_DECK;
return msg;
// else check if dealer busts
} else if (checkBust(dealerCards)) {
msg =
msg +
`Dealer busts. PLAYER WINS. <br> Player total is ${cardSum(
humanCards
)}. Dealer total is ${cardSum(
dealerCards
)}. <br><br>Click submit to play again.`;
gameStatus = GAME_STATUS_SHUFFLE_DECK;
return msg;
}
// Initialise winner
var winner = getWinner(humanCards, dealerCards);
// add to output message based on winner
if (winner == "human") {
msg = msg + "PLAYER WINS! <br>";
}
if (winner == "dealer") {
msg = msg + "DEALER WINS! <br>";
}
if (winner == "tie") {
msg = msg + "IT'S A TIE. <br>";
}
// add player and dealer totals to message
msg =
msg +
`Player total is ${cardSum(humanCards)}. Dealer total is ${cardSum(
dealerCards
)} <br><br>Click submit to play again!`;
gameStatus = GAME_STATUS_SHUFFLE_DECK;
// else for all other game statuses, create message to display all human cards plus first card of dealer
} else {
var humanCardsCounter = 0;
// create message for all human cards
while (humanCardsCounter < humanCards.length) {
msg =
msg +
`Player card ${humanCardsCounter + 1}: ${
humanCards[humanCardsCounter].nameEmoji
} of ${humanCards[humanCardsCounter].emoji}. <br>`;
humanCardsCounter += 1;
}
// add dealer's first card to message
msg =
msg +
`<br> Dealer card 1: ${dealerCards[0].nameEmoji} of ${dealerCards[0].emoji}.`;
// give hint to human
msg = msg + `<br><br> Player current total is ${cardSum(humanCards)}.`;
if (cardSum(humanCards) < 13) {
msg = msg + " You should hit.";
} else if (cardSum(humanCards) > 12 && cardSum(humanCards) < 17) {
msg = msg + " Tricky! Think about about you want to hit or stand.";
} else if (cardSum(humanCards > 16) && cardSum(humanCards < 20)) {
msg = msg + " You should probably stand.";
} else {
msg = msg + " You should stand.";
}
msg = msg + "<br><br> Type h for hit or s for stand.";
}
return msg;
};
// check if sum of cards is below 17
var isBelow17 = function (playerCards) {
var belowOrNot = false;
var sum = cardSum(playerCards);
if (sum < 17) {
belowOrNot = true;
}
return belowOrNot;
};
// main function
var main = function (input) {
var myOutputValue = "";
var shuffledDeck = shuffleCards(deck);
if (gameStatus == GAME_STATUS_SHUFFLE_DECK) {
humanCards = [];
dealerCards = [];
var counter = 0;
while (counter < 2) {
humanCards[counter] = shuffledDeck.pop();
dealerCards[counter] = shuffledDeck.pop();
counter += 1;
}
if (checkBlackJack(humanCards)) {
gameStatus = GAME_STATUS_RESULTS;
myOutputValue = displayCards(humanCards, dealerCards);
gameStatus = GAME_STATUS_SHUFFLE_DECK;
} else {
gameStatus = GAME_STATUS_DISPLAY_CARDS;
myOutputValue = displayCards(humanCards, dealerCards);
gameStatus = GAME_STATUS_HUMAN_DECIDES;
}
} else if (gameStatus == GAME_STATUS_HUMAN_DECIDES) {
// check input to make sure it's "h" or "s"
if (input !== HIT && input !== STAND) {
myOutputValue = "Please input h for hit or s for stand";
return myOutputValue;
}
// if human decides to hit
if (input == HIT) {
// deal another card to human
humanCards.push(shuffledDeck.pop());
// check if human busts
if (checkBust(humanCards)) {
gameStatus = GAME_STATUS_RESULTS;
myOutputValue = displayCards(humanCards, dealerCards);
gameStatus = GAME_STATUS_SHUFFLE_DECK;
} else {
gameStatus = GAME_STATUS_DISPLAY_CARDS;
myOutputValue = displayCards(humanCards, dealerCards);
gameStatus = GAME_STATUS_HUMAN_DECIDES;
}
// if human decides to stand
} else if (input == STAND) {
// deal out cards for dealer while dealer cards sum is less than 17
while (isBelow17(dealerCards)) {
dealerCards.push(shuffledDeck.pop());
}
// set game status to GAME_STATUS_RESULTS
gameStatus = GAME_STATUS_RESULTS;
myOutputValue = displayCards(humanCards, dealerCards);
}
}
return myOutputValue;
};