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
393 lines (367 loc) · 14.4 KB
/
script.js
File metadata and controls
393 lines (367 loc) · 14.4 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
// Blackjack - base
// Declare game modes
var place_bet = "place your bets";
var game_start = "game start";
var cards_drawn = "cards drawn";
var hit_or_stand = "hit or stand";
var game_results = "show game results";
var currentGameMode = place_bet;
// track player points and player bet
var playerPoints = Number(100);
var playerBet;
// Declare variables to store player and dealer hands (use arrays to store multiple card objects)
var playerHandArray = [];
var dealerHandArray = [];
// Declare variable to hold deck of cards (empty at the start)
var cardDeck = [];
// if gameEnd = true, show output msg to refresh / reset the game
var gameEnd = false;
var makeDeck = function () {
// Initialise an empty deck array
var cardDeck = [];
// Initialise an array of the 4 suits in our deck. We will loop over this array.
var suits = ["hearts", "diamonds", "clubs", "spades"];
// Loop over the suits array
var suitIndex = 0;
while (suitIndex < suits.length) {
// Store the current suit in a variable
var currentSuit = suits[suitIndex];
console.log(`current suit: ${currentSuit}`);
// 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;
// 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";
} else if (cardName == 12) {
cardName = "queen";
} else if (cardName == 13) {
cardName = "king";
}
// Create a new card with the current name, suit, and rank
var card = {
name: cardName,
suit: currentSuit,
rank: rankCounter,
};
console.log(`rank: ${rankCounter}`);
// 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;
};
var cardDeck = 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 cardDeck array
var shuffleDeck = function (cardDeck) {
// Loop over the card deck array once
var currentIndex = 0;
while (currentIndex < cardDeck.length) {
// Select a random index in the deck
var randomIndex = getRandomIndex(cardDeck.length);
// Select the card that corresponds to randomIndex
var randomCard = cardDeck[randomIndex];
// Select the card that corresponds to currentIndex
var currentCard = cardDeck[currentIndex];
// Swap positions of randomCard and currentCard in the deck
cardDeck[currentIndex] = randomCard;
cardDeck[randomIndex] = currentCard;
// Increment currentIndex
currentIndex = currentIndex + 1;
}
// Return the shuffled deck
return cardDeck;
};
// Shuffle the deck and save it in a new variable shuffledDeck
// to communicate that we have shuffled the deck.
var shuffledDeck = shuffleDeck(cardDeck);
// Function that creates and shuffles a deck
var createNewDeck = function () {
var newDeck = makeDeck();
var shuffledDeck = shuffleDeck(newDeck);
return shuffledDeck;
};
// Function that checks a hand for black jack
var checkForBlackjack = function (handArray) {
// Loop through player hand
// if there is a blackjack return true
// else return false
var playerCardOne = handArray[0];
var playerCardTwo = handArray[1];
var isBlackJack = false;
// Possible black jack scenerios
// First card is Ace + Second card is 10 or suits
// Second card is Ace + First card is 10 or suits
if (
(playerCardOne.name == "ace" && playerCardTwo.rank >= 10) ||
(playerCardTwo.name == "ace" && playerCardOne.rank >= 10)
) {
isBlackJack = true;
}
return isBlackJack;
};
// Function that calculates a hand
var calculateTotalHandValue = function (handArray) {
var totalHandValue = 0;
// Counter to keep track of the number of aces found within the given hand
var aceCounter = 0;
// Loop through player or dealers hand and add up the ranks
var index = 0;
while (index < handArray.length) {
var currCard = handArray[index];
// In blackjack, the value of king, queen, and jack are counted as 10 by default
if (
currCard.name == "king" ||
currCard.name == "queen" ||
currCard.name == "jack"
) {
totalHandValue = totalHandValue + 10;
}
// We count the value of ace as 11 by default
else if (currCard.name == "ace") {
totalHandValue = totalHandValue + 11;
aceCounter = aceCounter + 1;
// Else, all other numbered cards are valued by their ranks
} else {
totalHandValue = totalHandValue + currCard.rank;
}
index = index + 1;
}
// Reset index for ace counter
index = 0;
// Loop for the number of aces found and only deduct 10 from total hand value
// when totalHandValue is more than 21.
while (index < aceCounter) {
if (totalHandValue > 21) {
totalHandValue = totalHandValue - 10;
}
index = index + 1;
}
return totalHandValue;
};
// function to display player and dealer hands
var cardsDrawn = function (handArray) {
cardsIndex = 0;
cardsHand = "";
while (cardsIndex < handArray.length) {
cardsHand =
cardsHand +
`~${handArray[cardsIndex].name} of ${handArray[cardsIndex].suit}~<br>`;
cardsIndex += 1;
}
return cardsHand;
};
var main = function (input) {
var myOutputValue = "";
if (gameEnd == true) {
return `The game is over. Click refresh to play again.`;
}
// start game mode with placing bets
if (currentGameMode == place_bet) {
if (input == "") {
var myImage =
'<img src="https://c.tenor.com/bV604YJsMTIAAAAC/spongebob-squarepants-patrick-star.gif"/>';
return `You will start with 100 points. Please input the number of points you want to bet. <br><br> ${myImage}`;
}
// else user inputs number to bet, then switch to game start mode
playerBet = Number(input);
currentGameMode = game_start;
// input validation: if input is not a number
if (Number.isNaN(playerBet)) {
return `Invalid input. Please input the number of points you want to bet.`;
}
}
if (currentGameMode == game_start) {
// create the game deck
cardDeck = createNewDeck();
console.log(cardDeck);
// deal 2 cards to player and dealer
var playerCardOne = cardDeck.pop();
console.log("player card 1:", playerCardOne);
var playerCardTwo = cardDeck.pop();
console.log("player card 2:", playerCardTwo);
var dealerCardOne = cardDeck.pop();
console.log("dealer card 1:", dealerCardOne);
var dealerCardTwo = cardDeck.pop();
console.log("dealer card 2:", dealerCardTwo);
playerHandArray.push(playerCardOne);
playerHandArray.push(playerCardTwo);
dealerHandArray.push(dealerCardOne);
dealerHandArray.push(dealerCardTwo);
// progress gameMode
currentGameMode = cards_drawn;
myOutputValue =
'Everyone has been dealt with 2 cards. Click the "submit" button to calculate your cards.';
}
if (currentGameMode == cards_drawn) {
// for testing purposes
// playerHandArray = [
// { name: "queen", suit: "clubs", rank: 12 },
// { name: "7", suit: "diamonds", rank: 7 },
// ];
// dealerHandArray = [
// { name: "king", suit: "clubs", rank: 13 },
// { name: "7", suit: "spades", rank: 7 },
// ];
// check for blackjack
var playerHasBlackjack = checkForBlackjack(playerHandArray);
var dealerHasBlackjack = checkForBlackjack(dealerHandArray);
console.log("check player for blackjack", playerHasBlackjack);
console.log("check dealer for blackjack", dealerHasBlackjack);
// calculate total hand value
var playerHandTotalValue = calculateTotalHandValue(playerHandArray);
var dealerHandTotalValue = calculateTotalHandValue(dealerHandArray);
console.log("player total hand value:", playerHandTotalValue);
console.log("dealer total hand value:", dealerHandTotalValue);
if (playerHasBlackjack == true || dealerHasBlackjack == true) {
// both player and dealer has blackjack -> tie
if (playerHasBlackjack == true && dealerHasBlackjack == true) {
gameEnd = true;
playerPoints += playerBet;
myOutputValue = `Hi player, your cards are: <br><br> ${cardsDrawn(
playerHandArray
)} <br> Total value: ${playerHandTotalValue} <br><br> Dealer cards are: <br><br> ${cardsDrawn(
dealerHandArray
)} <br> Total value: ${dealerHandTotalValue}<br><br> It's a blackjack tie! <br>Total points: ${playerPoints}<br> Click refresh to play again.`;
}
// only player has blackjack -> player wins
else if (playerHasBlackjack == true && dealerHasBlackjack == false) {
gameEnd = true;
playerPoints += playerBet;
myOutputValue = `Hi player, your cards are: <br><br> ${cardsDrawn(
playerHandArray
)} <br> Total value: ${playerHandTotalValue}<br><br> Dealer cards are: <br><br> ${cardsDrawn(
dealerHandArray
)} <br> Total value: ${dealerHandTotalValue}<br><br> Player wins by blackjack!<br>Total points: ${playerPoints}<br> Click refresh to play again.`;
}
// only dealer has blackjack -> dealer wins
else if (playerHasBlackjack == false && dealerHasBlackjack == true) {
gameEnd = true;
playerPoints -= playerBet;
myOutputValue = `Hi player, your cards are: <br><br> ${cardsDrawn(
playerHandArray
)} <br> Total value: ${playerHandTotalValue}<br><br> Dealer cards are: <br><br> ${cardsDrawn(
dealerHandArray
)} <br> Total value: ${dealerHandTotalValue}<br><br> Dealer wins by blackjack! <br>Total points: ${playerPoints}<br> Click refresh to play again.`;
}
} else {
myOutputValue = `Hi player, your cards are: <br><br> ${cardsDrawn(
playerHandArray
)} <br> Total value: ${playerHandTotalValue}<br><br> Dealer cards are: <br><br> ${cardsDrawn(
dealerHandArray
)} <br> Total value: ${dealerHandTotalValue}<br><br> There is no blackjack. <br> Please input "hit" or "stand".`;
// no blackjack -> game mode change to 'hit or stand'
currentGameMode = hit_or_stand;
}
return myOutputValue;
}
if (currentGameMode == hit_or_stand) {
console.log("current game mode:", currentGameMode);
if (input !== "hit" && input !== "stand") {
myOutputValue = `Please enter only hit or stand.`;
}
if (input == "hit") {
playerHandArray.push(cardDeck.pop());
console.log("player hand:", playerHandArray);
// calculate player hand value
var playerHandTotalValue = calculateTotalHandValue(playerHandArray);
console.log("player total hand value:", playerHandTotalValue);
myOutputValue = `You have chosen to hit and drew another card. Your cards are: <br><br> ${cardsDrawn(
playerHandArray
)} <br> Total value: ${playerHandTotalValue} Please input "hit" or "stand".`;
} else if (input == "stand") {
currentGameMode = game_results;
var myImage =
'<img src="https://c.tenor.com/hcjiQIlb7UAAAAAi/flanaato-bt21baby.gif"/>';
myOutputValue = `You have chosen to stand. Click submit to show the game results.<br><br> ${myImage}`;
}
return myOutputValue;
}
if ((currentGameMode = game_results)) {
console.log("current game mode:", currentGameMode);
// Dealer to hit if total hand value is below 17
while (calculateTotalHandValue(dealerHandArray) < 17) {
dealerHandArray.push(cardDeck.pop());
console.log("dealer hand:", dealerHandArray);
}
// calculate total hand value
var playerHandTotalValue = calculateTotalHandValue(playerHandArray);
var dealerHandTotalValue = calculateTotalHandValue(dealerHandArray);
console.log("player total hand value:", playerHandTotalValue);
console.log("dealer total hand value:", dealerHandTotalValue);
myOutputValue = `Player cards are: <br><br> ${cardsDrawn(
playerHandArray
)} <br> Total value: ${playerHandTotalValue}
<br><br> Dealer cards are: <br><br> ${cardsDrawn(
dealerHandArray
)} <br> Total value: ${dealerHandTotalValue}<br>`;
// if both have same hand and <= 21, it's a tie
if (
playerHandTotalValue == dealerHandTotalValue &&
playerHandTotalValue &&
dealerHandTotalValue <= 21
) {
playerPoints;
myOutputValue =
myOutputValue +
`<br> It's a tie!<br>Total points: ${playerPoints}<br>Click refresh to play again.`;
}
// if both > 21, busted
if (dealerHandTotalValue > 21 && playerHandTotalValue > 21) {
playerPoints -= playerBet;
myOutputValue =
myOutputValue +
`<br> Both you and the dealer have busted.<br>Total points: ${playerPoints}<br> Click refresh to play again.`;
}
// if dealer > 21 and player <= 21, dealer busted & player wins
if (dealerHandTotalValue > 21 && playerHandTotalValue <= 21) {
playerPoints += playerBet;
myOutputValue =
myOutputValue +
`<br> The dealer has busted. You win!<br> ${myImagePlayerWin}<br> Total points: ${playerPoints}<br>Click refresh to play again.`;
}
// if player > 21 and dealer <= 21, player busted & dealer wins
if (playerHandTotalValue > 21 && dealerHandTotalValue <= 21) {
playerPoints -= playerBet;
myOutputValue =
myOutputValue +
`<br> You have busted. Dealer wins!<br>Total points: ${playerPoints}<br>Click refresh to play again.`;
}
// if player > dealer and both <= 21, player wins
if (
playerHandTotalValue > dealerHandTotalValue &&
playerHandTotalValue <= 21
) {
playerPoints += playerBet;
myOutputValue =
myOutputValue +
`<br> Player wins!<br>Total points: ${playerPoints}<br>Click refresh to play again.`;
}
// if player < dealer and both <=21, dealer wins
if (
playerHandTotalValue < dealerHandTotalValue &&
dealerHandTotalValue <= 21
) {
playerPoints -= playerBet;
myOutputValue =
myOutputValue +
`<br> Dealer wins!<br>Total points: ${playerPoints}<br>Click refresh to play again.`;
}
return myOutputValue;
}
};