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
327 lines (293 loc) · 9.09 KB
/
script.js
File metadata and controls
327 lines (293 loc) · 9.09 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
//Create a deck of card
var makeDeck = function () {
var cardDeck = [];
var suits = ["Diamonds ♦️", "Hearts ♥️", "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];
var rankCounter = 1;
// Loop from 1 to 13 to create all cards for each suit
while (rankCounter <= 13) {
// By default, the card name is the same as rankCounter
var cardName = rankCounter;
var cardValue = rankCounter;
// If rank is 1, 11, 12, or 13, set cardName to the ace or face card's name
if (cardName == 1) {
cardName = "Ace";
cardValue = 11;
} else if (cardName == 11) {
cardName = "Jack";
cardValue = 10;
} else if (cardName == 12) {
cardName = "Queen";
cardValue = 10;
} else if (cardName == 13) {
cardName = "King";
cardValue = 10;
}
// Create a new card with the current name, suit, rank and value
var card = {
name: cardName,
suit: currentSuit,
rank: rankCounter,
value: cardValue,
};
// 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;
};
//Helper function to generate random index, to be used in the helper function shuffleDeck
var getRandomIndex = function (max) {
return Math.floor(Math.random() * max);
};
//Shuffle the deck of cards
var shuffleCards = function (cardDeck) {
// Loop over the card deck array once
var currentIndex = 0;
while (currentIndex < 52) {
// Select a random index in the deck
var randomIndex = getRandomIndex(52);
// Select the card that corresponds to randomIndex
var randomCard = cardDeck[randomIndex];
//var randomCard = cardDeck[1];
// 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;
};
// Global variables
// Store player's hand and dealer's hand in a separate array
// Initialize playerScore and dealerScore to 0
var playerHand = [];
var dealerHand = [];
var playerScore = 0;
var dealerScore = 0;
var newDeck = [];
var shuffledDeck = [];
var isThereAce = false;
//Helper function to show player's hand and dealer's hand
var showHands = function () {
var index = 0;
var showPlayerHand = "Player's Hand: <br>";
var showDealerHand = "Dealer's Hand: <br>";
//Loop through playerHand and show each card
while (index < playerHand.length) {
showPlayerHand =
showPlayerHand +
playerHand[index].name +
" of " +
playerHand[index].suit +
"<br>";
index += 1;
}
//re-initialize index
index = 0;
//Loop through dealerHand and show each card
while (index < dealerHand.length) {
showDealerHand =
showDealerHand +
dealerHand[index].name +
" of " +
dealerHand[index].suit +
"<br>";
index += 1;
}
//re-set index back to 0
index = 0;
return showPlayerHand + "<br>" + showDealerHand;
};
//Helper function to show player's score and dealer's score
var showScores = function () {
var index = 0;
playerScore = 0;
dealerScore = 0;
while (index < playerHand.length) {
playerScore = playerScore + playerHand[index].value;
index += 1;
}
index = 0;
while (index < dealerHand.length) {
dealerScore = dealerScore + dealerHand[index].value;
index += 1;
}
index = 0;
var showScoreMsg =
"Player score is " +
playerScore +
"<br>" +
"Dealer score is " +
dealerScore;
return showScoreMsg;
};
// Helper function to check if there's an Ace in player's Hand
var checkForAce = function () {
var index = 0;
while (index < playerHand.length) {
if (playerHand[index].name == "ace") {
isThereAce = true;
}
index += 1;
}
return isThereAce;
};
// Helper function to count the number of ace in a hand
var countNoAce = function () {
var index = 0;
var numOfAce = 0;
while (index < playerHand.length) {
if (playerHand[index].name == "ace") {
numOfAce += 1;
}
index += 1;
}
return numOfAce;
};
var main = function (input) {
if (input == "start") {
// Turn on Hit and Stand button
document.getElementById("hit-button").disabled = false;
document.getElementById("stand-button").disabled = false;
// Turn off Start button
document.getElementById("start-button").disabled = true;
newDeck = makeDeck();
shuffledDeck = shuffleCards(newDeck);
//Deal 2 cards each to player and dealer
var playerCard1 = shuffledDeck.pop();
playerHand.push(playerCard1);
var dealerCard1 = shuffledDeck.pop();
dealerHand.push(dealerCard1);
var playerCard2 = shuffledDeck.pop();
playerHand.push(playerCard2);
var dealerCard2 = shuffledDeck.pop();
dealerHand.push(dealerCard2);
return `${showHands()} <br> ${showScores()}`;
}
//When player choose to Hit
else if (input == "hit") {
var newCard = shuffledDeck.pop();
playerHand.push(newCard);
playerScore = playerScore + newCard.value;
// Check if player went over 21
// if player has an Ace and went bust, Ace counts as 1
var playerHasAce = checkForAce();
console.log(playerHasAce);
var numOfAce = countNoAce();
console.log(numOfAce);
if (playerScore > 21 && playerHasAce) {
playerScore = playerScore - 10 * (numOfAce - 1);
console.log(playerScore);
//if after changing ace to 1 player is still bust
if (playerScore > 21) {
//Turn off Stand button and Hit button
document.getElementById("stand-button").disabled = true;
document.getElementById("hit-button").disabled = true;
// Turn on Reset button
document.getElementById("reset-button").disabled = false;
return `Player went bust! Sorry You Lost! <br><br>${showHands()} <br>Player score is ${playerScore}<br>Dealer score is ${dealerScore}`;
}
return `${showHands()}<br>Player score is ${playerScore}<br> Dealer score is ${dealerScore}`;
} else if (playerScore > 21) {
//Turn off Stand button and Hit button
document.getElementById("stand-button").disabled = true;
document.getElementById("hit-button").disabled = true;
// Turn on Reset button
document.getElementById("reset-button").disabled = false;
var bustMsg =
"You went bust! Sorry You Lost! <br><br>" +
showHands() +
"<br>" +
"Player score is " +
playerScore +
"<br>" +
"Dealer score is " +
dealerScore;
return bustMsg;
}
var hitMsg = `Player choose to hit <br><br>${showHands()}<br>${showScores()}`;
return hitMsg;
} else if (input == "stand") {
// Turn off Hit button and Stand button
document.getElementById("hit-button").disabled = true;
document.getElementById("stand-button").disabled = true;
// Turn on Reset button
document.getElementById("reset-button").disabled = false;
//Dealer need to hit if dealer hand is below 17
while (dealerScore < 17) {
var newCard = shuffledDeck.pop();
dealerHand.push(newCard);
dealerScore = dealerScore + newCard.value;
}
// Check to see if dealer went bust
if (dealerScore > 21) {
var bustMsg =
"Dealer went bust! YOU WIN! <br><br>" +
showHands() +
"<br>" +
"Player score is " +
playerScore +
"<br>Dealer score is " +
dealerScore;
return bustMsg;
}
// Conditions for when it's a draw, lost or win
if (playerScore == dealerScore) {
var drawMsg =
"It's a draw! <br><br>" +
showHands() +
"<br>" +
"Player score is " +
playerScore +
"<br>Dealer score is " +
dealerScore;
return drawMsg;
} else if (playerScore < dealerScore) {
var lostMsg =
"Player Lost! <br><br>" +
showHands() +
"<br>" +
"Player score is " +
playerScore +
"<br>Dealer score is " +
dealerScore;
return lostMsg;
} else if (playerScore > dealerScore) {
var winMsg =
"Player WINS! CONGRATULATIONS!<br><br>" +
showHands() +
"<br>" +
"Player score is " +
playerScore +
"<br>Dealer score is " +
dealerScore;
return winMsg;
}
} else if (input == "reset") {
playerHand = [];
dealerHand = [];
playerScore = 0;
dealerScore = 0;
newDeck = [];
shuffledDeck = [];
var resetMsg = "Game has been reset. <br> Please click Start";
// Turn on Start button
document.getElementById("start-button").disabled = false;
// Turn off Reset button
document.getElementById("reset-button").disabled = true;
return resetMsg;
}
};