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
345 lines (302 loc) · 10.3 KB
/
script.js
File metadata and controls
345 lines (302 loc) · 10.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
// Declare Game modes
var gameStart = "Game start";
var drawGameCards = "Cards are drawn";
var showGameResults = "Results shown";
var gameHitOrStand = "Hit or stand";
var currentGameMode = gameStart;
// Declare variable to store human and computer hands
// We use arrays as each hand will be holding multiple card objects
var humanHand = [];
var computerHand = [];
// Declare an empty variable to hold deck of cards
var gameDeck = [];
/* =========== DECK CREATION FUNCTIONS ============== */
// Function that creates a deck of cards, used by createNewDeck function
var createDeck = function () {
// deck array
var deck = [];
// for 'while loop' to create suits for cards
var suits = ["diamonds", "clubs", "hearts", "spades"];
var indexSuits = 0;
while (indexSuits < suits.length) {
var currSuit = suits[indexSuits];
// 13 ranks... ace to king - rank to define "card positions"
var indexRanks = 1;
while (indexRanks <= 13) {
var cardName = indexRanks;
// define card value - differentiate from rank: 'ace' = 1 / 11, 'jack' & 'queen' & 'king' = 10
if (cardName == 1) {
cardName = "ace";
// define ace value as 11 all the way. if handValue > 10, -11 to total value
// vs. coding a function to redefine the value for 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;
};
// Function that generates a random number, used by shuffle deck function
var getRandomIndex = function (size) {
return Math.floor(Math.random() * size);
};
// Function that shuffles a deck, used by createNewDeck function
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;
};
// Function that creates and shuffles a deck
var createNewDeck = function () {
var newDeck = createDeck();
var shuffledDeck = shuffleDeck(newDeck);
return shuffledDeck;
};
/* ================ GAME FUNCTIONS ================ */
// Function that checks a hand for black jack
var checkForBlackJack = function (handArray) {
// Loop through human hand
// if there is a blackjack return true
// else return false
var humanCardOne = handArray[0];
var humanCardTwo = 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 (
(humanCardOne.name == "ace" && humanCardTwo.rank >= 10) ||
(humanCardTwo.name == "ace" && humanCardOne.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 human or computers 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 that displays the human and computers hand in a message
var displayHumanAndComputerHands = function (
humanHandArray,
computerHandArray
) {
var humanMessage = "Human hand ✋ <br>";
var index = 0;
while (index < humanHandArray.length) {
humanMessage =
humanMessage +
"- " +
humanHandArray[index].name +
" of " +
humanHandArray[index].suit +
"<br>";
index = index + 1;
}
index = 0;
var computerMessage = "Computer hand 💻 <br>";
while (index < computerHandArray.length) {
computerMessage =
computerMessage +
"- " +
computerHandArray[index].name +
" of " +
computerHandArray[index].suit +
"<br>";
index = index + 1;
}
return humanMessage + "<br>" + computerMessage;
};
// Function that displays the total hand values of the human and the computer in a message
var displayHandTotalValues = function (humanHandValue, computerHandValue) {
var totalHandValueMessage =
"<br>Human ✋ total hand value: " +
humanHandValue +
"<br>Computer 💻 total hand value: " +
computerHandValue;
return totalHandValueMessage;
};
/* ================= MAIN FUNCTION ================ */
var main = function (input) {
var myOutputValue = "";
// FIRST CLICK
if (currentGameMode == gameStart) {
// create a deck of cards
gameDeck = createNewDeck();
// deal 2 cards to human and computer
humanHand.push(gameDeck.pop());
humanHand.push(gameDeck.pop());
computerHand.push(gameDeck.pop());
computerHand.push(gameDeck.pop());
// check human and computer cards
console.log("Human Hand ==>");
console.log(humanHand);
console.log("Computer Hand ==>");
console.log(computerHand);
// update gameMode
currentGameMode = drawGameCards;
// reassign output message
myOutputValue =
"Both human ✋ and computer 💻 have 2 cards now. Click 'Get lucky!' to calculate cards!";
// return message
return myOutputValue;
}
// SECOND CLICK
if (currentGameMode == drawGameCards) {
// check for blackjack
var humanHasBlackJack = checkForBlackJack(humanHand);
var computerHasBlackJack = checkForBlackJack(computerHand);
console.log("Does human have Black Jack? ==>", humanHasBlackJack);
console.log("Does computer have Black Jack? ==>", computerHasBlackJack);
// Condition when either human or computer has black jack
if (humanHasBlackJack == true || computerHasBlackJack == true) {
// Condition where both have black jack
if (humanHasBlackJack == true && computerHasBlackJack == true) {
outputMessage =
displayHumanAndComputerHands(humanHand, computerHand) +
"<br>it's a blackjack tie! 👔👔👔";
}
// Condition when only human has black jack
else if (humanHasBlackJack == true && computerHasBlackJack == false) {
outputMessage =
displayHumanAndComputerHands(humanHand, computerHand) +
"<br>blackjack! human ✋ wins!";
}
// Condition when only computer has black jack
else {
outputMessage =
displayHumanAndComputerHands(humanHand, computerHand) +
"<br>blackjack! computer 💻 wins!";
}
}
// Condition where neither human nor computer has black jack
// ask human to input 'hit' or 'stand'
else {
outputMessage =
displayHumanAndComputerHands(humanHand, computerHand) +
'<br> There are no blackjacks. <br>Please input "hit" or "stand".';
// update gameMode
currentGameMode = gameHitOrStand;
}
// return message
return myOutputValue;
}
// THIRD CLICK
if (currentGameMode == gameHitOrStand) {
// Condition where human inputs 'hit'
if (input == "hit") {
humanHand.push(gameDeck.pop());
myOutputValue =
displayHumanAndComputerHands(humanHand, computerHand) +
'<br> You drew another card. <br>Please input "hit" or "stand".';
}
// Condition where human inputs 'stand'
else if (input == "stand") {
// Calculate hands
var humanHandTotalValue = calculateTotalHandValue(humanHand);
var computerHandTotalValue = calculateTotalHandValue(computerHand);
// computer's hit or stand logic
while (computerHandTotalValue < 17) {
computerHand.push(gameDeck.pop());
computerHandTotalValue = calculateTotalHandValue(computerHand);
}
// Conditions for tied game
if (
humanHandTotalValue == computerHandTotalValue ||
(humanHandTotalValue > 21 && computerHandTotalValue > 21)
) {
myOutputValue =
displayHumanAndComputerHands(humanHand, computerHand) +
"<br>Its a Tie! 👔" +
displayHandTotalValues(humanHandTotalValue, computerHandTotalValue);
}
// Conditions for human win
else if (
(humanHandTotalValue > computerHandTotalValue &&
humanHandTotalValue <= 21) ||
(humanHandTotalValue <= 21 && computerHandTotalValue > 21)
) {
myOutputValue =
displayHumanAndComputerHands(humanHand, computerHand) +
"<br>Human ✋ is the final winner!" +
displayHandTotalValues(humanHandTotalValue, computerHandTotalValue);
}
// Computer wins when above two conditions are not met
else {
myOutputValue =
displayHumanAndComputerHands(humanHand, computerHand) +
"<br>Computer 💻 is the final winner!" +
displayHandTotalValues(humanHandTotalValue, computerHandTotalValue);
}
// update game mode - GAME_RESULTS_SHOWN is not used in this base example
// However, you may wish to implement your own game modes for further functionality
// i.e. going back to GAME_START to loop the game
currentGameMode = showGameResults;
}
// Input validation when human inputs anything outside of 'hit' or 'stand'
else {
myOutputValue =
'Please only input "hit" or "stand".<br><br>' +
displayHumanAndComputerHands(humanHand, computerHand);
}
// return output message
return myOutputValue;
}
};