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
288 lines (234 loc) · 10.4 KB
/
script.js
File metadata and controls
288 lines (234 loc) · 10.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
// Declare global variables to keep track of...
var gameStage = 'SET UP'; // whether SET UP, DRAW CARDS, PLAYER or DEALER, or RESULT
var shuffledDeck = [];
var playerCards = [], dealerCards = []; // cards dealt by player and dealer respectively
var playerScore = 0, dealerScore = 0; // score of player and dealer respectively
var disableAllButtons = false; // this is a flag to terminate the game when card deck runs low
var main = function (input) {
if (gameStage == 'SET UP') { // SET UP mode - to Make and Shuffle SIX decks of cards
deck = [];
for (var i=0; i<6; i+=1) {
arr = shuffleCards(makeDeck());
deck = deck.concat(arr);
}
shuffledDeck = shuffleCards(deck);
console.log(shuffledDeck.length);
gameStage = 'DRAW CARDS'; // toggle to DRAW CARDS mode
//return 'Game is setup, click Submit to play';
}
if (gameStage == 'DRAW CARDS') { // this game mode to draw the cards, for player and dealer to have two cards each
// if the stack of cards is below 30 cards left, then game must restart
if (shuffledDeck.length < 30) {
disableAllButtons = true;
return `Dealer needs to shuffle the cards now. <br>
Please refresh browser to restart the game.`;
}
for (var i=0; i<2; i+=1) {
// Draw 2 cards from the top of the deck - one for dealer and the other for player - and store in respective arrays
var playerCard = shuffledDeck.pop();
var dealerCard = shuffledDeck.pop();
playerCards.push(playerCard);
dealerCards.push(dealerCard);
console.log('Player card is:', playerCard); console.log('Dealer card is:', dealerCard);
}
playerScore = calculateScore(playerCards);
dealerScore = calculateScore(dealerCards);
if (playerScore == 21) { // if player already 21 points, jump straight to DEALER
gameStage = 'DEALER';
} else { // else toggle to PLAYER mode - let player decide hit or stand
gameStage = 'PLAYER';
var instruction = 'Click "hit" or "stand" buttons';
var myOutputValue = formatAllHands(playerCards, dealerCards, playerScore, dealerScore) + instruction;
return myOutputValue;
}
}
if (gameStage == 'PLAYER') { // for user to decide whether to hit or stand
if (input!= 'hit' && input != 'stand') { // input validation
return 'Please input only "hit" or "stand"';
}
if (input == 'hit') { // user wish to draw another card
var playerCard = shuffledDeck.pop(); console.log('Player card is:', playerCard);
playerCards.push(playerCard);
playerScore = calculateScore(playerCards); // recalculate player score
if (playerScore > 21) { // if player becomes busted, then player loses straight away
gameStage = 'RESULT';
} else if (playerScore == 21) { // if player scores 21, then it becomes DEALER's turn
gameStage = 'DEALER';
} else { // player can still choose to draw more cards
var instruction = 'Click "hit" or "stand" buttons';
var myOutputValue = formatAllHands(playerCards, dealerCards, playerScore, dealerScore) + instruction;
return myOutputValue;
}
} else { // user wish to stop drawing more cards
gameStage = 'DEALER'; // toggle to DEALER mode - let dealer decide hit or stand
}
}
if (gameStage == 'DEALER') { // let's say dealer deals at 16; stand at 17
while (dealerScore <= 16) {
var dealerCard = shuffledDeck.pop(); console.log('Dealer card is:', dealerCard);
dealerCards.push(dealerCard);
dealerScore = calculateScore(dealerCards); // recalculate dealer score
}
gameStage = 'RESULT'; // toggle to RESULT mode
}
if (gameStage == 'RESULT') { // this stage is to decide who is the winner
result = checkResult(playerScore, dealerScore, playerCards, dealerCards);
var instruction = `<b>${result}</b> <br> Continue playing? <br>`;
var myOutputValue = formatAllHands(playerCards, dealerCards, playerScore, dealerScore) + instruction;
var myImage = showGIF(result);
myOutputValue += myImage;
playerCards = [], dealerCards = []; // to reset for next round;
gameStage = 'DRAW CARDS'; // toggle to DRAW CARDS mode
return myOutputValue;
}
};
var formatAllHands = function(playerCards, dealerCards, playerScore, dealerScore) {
outputMsg =
`<u>PLAYER </u> <br> ${displayHand(playerCards, false)}
<b> Player's hand totals to ${playerScore}</b> <br><br>
<u>DEALER </u> <br> ${displayHand(dealerCards, true)}`;
if (gameStage == 'RESULT') { // only display the dealer's hand totals if RESULT mode
outputMsg += `<b> Dealer's hand totals to ${dealerScore}</b> <br><br>`;
}
outputMsg += `<hr> <br>`;
return outputMsg;
}
var displayHand = function(inputHand, dealer) {
var outputMsg = '';
if (gameStage != 'RESULT' && dealer == true) { // only display dealer's first card if Not RESULT mode
outputMsg += `${inputHand[0].name} of ${displaySuit(inputHand[0].suit)} <br>
[Hidden card] <br>`;
} else { // display all cards
for (var i=0; i<inputHand.length; i+=1) {
outputMsg += `${inputHand[i].name} of ${displaySuit(inputHand[i].suit)} <br> `;
}
}
return outputMsg;
}
var displaySuit = function(inputSuit) {
switch(inputSuit) {
case 'hearts': {return '<font color="red"> ♥️ </font>';}
case 'diamonds': {return '<font color="red"> ♦️ </font>';}
case 'spades': {return '♠️';}
case 'clubs': {return '♣️';}
}
}
var checkResult = function(playerScore, dealerScore, playerCards, dealerCards) {
if (isBlackjack(playerCards) && isBlackjack(dealerCards)) { return "It's a tie."}
if (isBlackjack(playerCards) && !isBlackjack(dealerCards)) { return "Player wins by black jack!"}
if (!isBlackjack(playerCards) && isBlackjack(dealerCards)) { return "Dealer wins by black jack!"}
// if (playerScore > 21 && dealerScore > 21) { return "It's a tie."}
if (playerScore <= 21 && dealerScore > 21) { return "Player wins!";}
if (playerScore > 21) { return "Dealer wins. Player is busted.";}
if (playerScore > dealerScore) {
return "Player wins!";
} else if (playerScore < dealerScore) {
return "Dealer wins.";
} else {
return "It's a tie."
}
}
var showGIF = function(inputResultText) {
var winningImage = '<img class="img" src="https://tenor.com/view/the-wolf-of-wall-street-clap-clapping-clapping-hands-leonardo-dicaprio-gif-22829304.gif"/>';
var losingImage = '<img class="img" src="https://tenor.com/view/wolf-of-wall-street-oh-my-god-leonardo-di-caprio-fist-bite-gif-4681785.gif"/>';
var tieImage = '<img class="img" src="https://tenor.com/view/mark-wahlberg-wahlberg-tie-oscar-%E5%B9%B3%E6%89%8B-gif-9081826.gif"/>';
if (inputResultText.includes('Player wins')) {return winningImage;} // return winning Image if player wins
if (inputResultText.includes('Dealer wins')) {return losingImage;} // return losing Image if dealer wins
return tieImage; // return tieImage if it's a tie
}
var isBlackjack = function(inputHand) {
var tenCards = [10, 'jack', 'queen', 'king']
if (tenCards.includes(inputHand[0].name) && inputHand[1].name == 'ace') {return true;}
if (tenCards.includes(inputHand[1].name) && inputHand[0].name == 'ace') {return true;}
return false;
}
var calculateScore = function(inputHand) {
var score = 0;
var aceCards = 0;
var pictureCards = ['jack', 'queen', 'king'];
for (var i=0; i<inputHand.length; i+=1) { // count total for non-ace cards first
if (pictureCards.includes(inputHand[i].name)) {
score += 10;
} else if (inputHand[i].name != 'ace') {
score += inputHand[i].rank;
}
}
for (var i=0; i<inputHand.length; i+=1) { // count the number of ace cards
if (inputHand[i].name == 'ace') {
aceCards += 1;
}
}
while (aceCards > 0) { // check what is the optimal way to account for each ace card
if (score + 11 <= 21) {
score += 11;
} else {
score += 1;
}
aceCards -= 1;
}
return score;
}
// 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 shuffleCards = function (cardDeck) {
// Loop over the card deck array once
var currentIndex = 0;
while (currentIndex < cardDeck.length) {
var randomIndex = getRandomIndex(cardDeck.length); // Select a random index in the deck
var randomCard = cardDeck[randomIndex]; // Select the card that corresponds to randomIndex
var currentCard = cardDeck[currentIndex]; // Select the card that corresponds to 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;
};
// Initialise the card deck representation as an array of objects
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];
// 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,
};
cardDeck.push(card); // Add the new card to the deck
rankCounter += 1; // Increment rankCounter to iterate over the next rank
}
// Increment the suit index to iterate over the next suit
suitIndex += 1;
}
// Return the completed card deck
return cardDeck;
};