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
352 lines (308 loc) · 9.63 KB
/
script.js
File metadata and controls
352 lines (308 loc) · 9.63 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
/*pseudocode
1. create deck, shuffle, deal cards, evaluate winner
game states
- current game mode
- game start
- cards drawn
- final results
general things to do
- define player and dealer with a global array
var player = []
var dealer = []
- create and shuffle deck
- draw 2 cards for player and dealer
- win conditions
-- blackjack
-- higher hand value
- display hands of both player and computer and declare winner
2. player - hit or stand
psedocode for h/s - new game state for h/s and choice of h(add card)/s(compare card with player) must be there for player
3. dealer - hit or stand
psedocode for h/s - dealer must h/s ONLY after player h/s is done AND dealer MUST hit if total deal value is less than 17. if more than 17, choose to stand
4. variable value of Ace - either 1 or 11
if total hand value < 21, ace must be 11. else, ace is 1 (use calculatetotalValue function)
*/
//global variables - game state
var start = "game start";
var cardDrawn = "cards drawn";
var results = "results shown";
var hitOrStand = "hit or stand";
var gameStateNow = start;
//global arrays
//storing player and dealer cards
var player = [];
var dealer = [];
// empty array to hold deck of cards
var gameDeck = [];
//helper functions
//create new deck of cards
var createDeck = function () {
// deck array
var deck = [];
var suits = ["Diamonds", "Clubs", "Hearts", "Spades"];
var indexSuits = 0;
while (indexSuits < suits.length) {
var currSuit = suits[indexSuits];
var indexRanks = 1;
while (indexRanks <= 13) {
var cardName = indexRanks;
if (cardName == 1) {
cardName = "Ace";
}
if (cardName == 11) {
cardName = "Jack";
}
if (cardName == 12) {
cardName = "Queen";
}
if (cardName == 13) {
cardName = "King";
}
var card = {
name: cardName,
suit: currSuit,
rank: indexRanks,
};
deck.push(card);
indexRanks = indexRanks + 1;
}
indexSuits = indexSuits + 1;
}
return deck;
};
// number randomiser function for shuffling deck
var getRandomIndex = function (max) {
return Math.floor(Math.random() * max);
};
// shuffles the new deck
var shuffleDeck = function (cards) {
var index = 0;
while (index < cards.length) {
var randomIndex = getRandomIndex(cards.length);
var currentItem = cards[index];
var randomItem = cards[randomIndex];
cards[index] = randomItem;
cards[randomIndex] = currentItem;
index = index + 1;
}
return cards;
};
// creates and shuffles a new deck - together with 2 original helper fxns
var createNewDeck = function () {
var newDeck = createDeck();
var shuffledDeck = shuffleDeck(newDeck);
return shuffledDeck;
};
// checking for black jack
var checkForBlackJack = function (handArray) {
var player1 = handArray[0];
var player2 = handArray[1];
var verifyBlackjack = false;
// ace + (10 or suits) - either order is fine for blackjack
if (
(player1.name == "Ace" && player2.rank >= 10) ||
(player2.name == "Ace" && player1.rank >= 10)
) {
verifyBlackjack = true;
}
return verifyBlackjack;
};
// check for total hand value in a round
var calculateHandValue = function (handArray) {
var totalHandValue = 0;
// ace card tracker variable
var aceCounter = 0;
// adding the ranks together
var index = 0;
while (index < handArray.length) {
var currentCard = handArray[index];
// king, queen, and jack are counted as 10.
if (
currentCard.name == "King" ||
currentCard.name == "Queen" ||
currentCard.name == "Jack"
) {
totalHandValue = totalHandValue + 10;
}
// ace as 11
else if (currentCard.name == "Ace") {
totalHandValue = totalHandValue + 11;
aceCounter = aceCounter + 1;
// rest of them by their ranks
} else {
totalHandValue = totalHandValue + currentCard.rank;
}
index = index + 1;
}
// index reset back to 0 for ace counter
index = 0;
/*loops through tp find the number of aces found by ace counter 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;
};
// player and dealers hand will be shown in a message
var displayPlayerAndDealerHands = function (playerHandArray, dealerHandArray) {
var playerMessage = "Player hand:<br>";
var index = 0;
while (index < playerHandArray.length) {
playerMessage =
playerMessage +
playerHandArray[index].name +
" of " +
playerHandArray[index].suit +
"<br>";
index = index + 1;
}
index = 0;
var dealerMessage = "Dealer hand:<br>";
while (index < dealerHandArray.length) {
dealerMessage =
dealerMessage +
dealerHandArray[index].name +
" of " +
dealerHandArray[index].suit +
"<br>";
index = index + 1;
}
return playerMessage + "<br>" + dealerMessage;
};
// total hand values of the player and the dealer will be shown in a message
var displayHandTotalValues = function (playerHandValue, dealerHandValue) {
var totalHandValueMessage =
"<br>Player total hand value: " +
playerHandValue +
"<br>Dealer total hand value: " +
dealerHandValue;
return totalHandValueMessage;
};
//main function
var main = function (input) {
var outputMessage = "";
// start game mode
if (gameStateNow == start) {
// create deck
gameDeck = createNewDeck();
// give 2 cards each to player and dealer
player.push(gameDeck.pop());
player.push(gameDeck.pop());
dealer.push(gameDeck.pop());
dealer.push(gameDeck.pop());
// check player and dealer cards
console.log("Player Hand ==>");
console.log(player);
console.log("Dealer Hand ==>");
console.log(dealer);
// update game state
gameStateNow = cardDrawn;
// output message
outputMessage =
"Everyone has been dealt a card. Click button to calculate cards!";
// return message
return outputMessage;
}
// second game state
if (gameStateNow == cardDrawn) {
// check for blackjack
var playerHasBlackJack = checkForBlackJack(player);
var dealerHasBlackJack = checkForBlackJack(dealer);
console.log("Does Player have Black Jack? ==>", playerHasBlackJack);
console.log("Does Dealer have Black Jack? ==>", dealerHasBlackJack);
// Condition when either player or dealer has black jack
if (playerHasBlackJack == true || dealerHasBlackJack == true) {
// Condition where both have black jack
if (playerHasBlackJack == true && dealerHasBlackJack == true) {
outputMessage =
displayPlayerAndDealerHands(player, dealer) +
"<br>Its a Blackjack Tie!";
}
// Condition when only player has black jack
else if (playerHasBlackJack == true && dealerHasBlackJack == false) {
outputMessage =
displayPlayerAndDealerHands(player, dealer) +
"<br>Player wins by Blackjack!";
}
// Condition when only dealer has black jack
else {
outputMessage =
displayPlayerAndDealerHands(player, dealer) +
"<br>Dealer wins by Blackjack!";
}
}
// Condition where neither player nor dealer has black jack
// ask player to input 'hit' or 'stand'
else {
outputMessage =
displayPlayerAndDealerHands(player, dealer) +
'<br> There are no Black Jacks. <br>Please input "hit" or "stand".';
// update gameMode
gameStateNow = hitOrStand;
}
// return message
return outputMessage;
}
// third game state
if (gameStateNow == hitOrStand) {
// Condition where player inputs 'hit'
if (input == "hit") {
player.push(gameDeck.pop());
outputMessage =
displayPlayerAndDealerHands(player, dealer) +
'<br> You drew another card. <br>Please input "hit" or "stand".';
}
// Condition where player inputs 'stand'
else if (input == "stand") {
// Calculate hands
var playerHandTotalValue = calculateHandValue(player);
var dealerHandTotalValue = calculateHandValue(dealer);
// Dealer's hit or stand logic
while (dealerHandTotalValue < 17) {
dealer.push(gameDeck.pop());
dealerHandTotalValue = calculateHandValue(dealer);
}
// Conditions for tied game
if (
playerHandTotalValue == dealerHandTotalValue ||
(playerHandTotalValue > 21 && dealerHandTotalValue > 21)
) {
outputMessage =
displayPlayerAndDealerHands(player, dealer) +
"<br>It's a Tie!" +
displayHandTotalValues(playerHandTotalValue, dealerHandTotalValue);
}
// player win - player must be below 21 and higher than dealer
else if (
(playerHandTotalValue > dealerHandTotalValue &&
playerHandTotalValue <= 21) ||
(playerHandTotalValue <= 21 && dealerHandTotalValue > 21)
) {
outputMessage =
displayPlayerAndDealerHands(player, dealer) +
"<br>Player wins!" +
displayHandTotalValues(playerHandTotalValue, dealerHandTotalValue);
}
// dealer win - dealer must be below 21 and higher than player
else {
outputMessage =
displayPlayerAndDealerHands(player, dealer) +
"<br>Dealer wins!" +
displayHandTotalValues(playerHandTotalValue, dealerHandTotalValue);
}
// update game mode
gameStateNow = results;
}
// input validation
else {
outputMessage =
'wrong input... only "hit" or "stand" are valid.<br><br>' +
displayPlayerAndDealerHands(player, dealer);
}
}
//results mode - release the results
return outputMessage;
};