-
Notifications
You must be signed in to change notification settings - Fork 612
/
script.js
304 lines (261 loc) · 10.4 KB
/
script.js
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
/* ================================================== */
/* ================ GLOBAL VARIABLES ================ */
/* ================================================== */
// Declare Game modes
var GAME_START = 'game start';
var GAME_CARDS_DRAWN = 'cards are drawn';
var GAME_RESULTS_SHOWN = 'results are shown';
var GAME_HIT_OR_STAND = 'hit or stand';
var currentGameMode = GAME_START;
// Declare variable to store player and dealer hands
// We use arrays as each hand will be holding multiple card objects
var playerHand = [];
var dealerHand = [];
// 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 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 that displays the player and dealers hand 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;
};
// Function that displays the total hand values of the player and the dealer 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 = '';
// FIRST CLICK
if (currentGameMode == GAME_START) {
// create a deck of cards
gameDeck = createNewDeck();
// deal 2 cards to player and dealer
playerHand.push(gameDeck.pop());
playerHand.push(gameDeck.pop());
dealerHand.push(gameDeck.pop());
dealerHand.push(gameDeck.pop());
// check player and dealer cards
console.log("Player Hand ==>");
console.log(playerHand);
console.log("Dealer Hand ==>");
console.log(dealerHand);
// update gameMode
currentGameMode = GAME_CARDS_DRAWN;
// reassign output message
outputMessage = 'Everyone has been dealt a card. Click button to calculate cards!';
// return message
return outputMessage;
}
// SECOND CLICK
if (currentGameMode == GAME_CARDS_DRAWN) {
// check for blackjack
var playerHasBlackJack = checkForBlackJack(playerHand);
var dealerHasBlackJack = checkForBlackJack(dealerHand);
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(playerHand, dealerHand) + '<br>Its a Black Jack Tie!';
}
// Condition when only player has black jack
else if (playerHasBlackJack == true && dealerHasBlackJack == false) {
outputMessage = displayPlayerAndDealerHands(playerHand, dealerHand) + '<br>Player wins by Black Jack!';
}
// Condition when only dealer has black jack
else {
outputMessage = displayPlayerAndDealerHands(playerHand, dealerHand) + '<br>Dealer wins by Black Jack!';
}
}
// Condition where neither player nor dealer has black jack
// ask player to input 'hit' or 'stand'
else {
outputMessage = displayPlayerAndDealerHands(playerHand, dealerHand) + '<br> There are no Black Jacks. <br>Please input "hit" or "stand".';
// update gameMode
currentGameMode = GAME_HIT_OR_STAND;
}
// return message
return outputMessage;
}
// THIRD CLICK
if (currentGameMode == GAME_HIT_OR_STAND) {
// Condition where player inputs 'hit'
if (input == 'hit') {
playerHand.push(gameDeck.pop());
outputMessage = displayPlayerAndDealerHands(playerHand, dealerHand) + '<br> You drew another card. <br>Please input "hit" or "stand".';
}
// Condition where player inputs 'stand'
else if (input == 'stand') {
// Calculate hands
var playerHandTotalValue = calculateTotalHandValue(playerHand);
var dealerHandTotalValue = calculateTotalHandValue(dealerHand);
// Dealer's hit or stand logic
while (dealerHandTotalValue < 17) {
dealerHand.push(gameDeck.pop());
dealerHandTotalValue = calculateTotalHandValue(dealerHand);
}
// Conditions for tied game
if ((playerHandTotalValue == dealerHandTotalValue) ||
(playerHandTotalValue > 21 && dealerHandTotalValue > 21)) {
outputMessage = displayPlayerAndDealerHands(playerHand, dealerHand) + "<br>Its a Tie!" + displayHandTotalValues(playerHandTotalValue, dealerHandTotalValue);
}
// Conditions for player win
else if ((playerHandTotalValue > dealerHandTotalValue && playerHandTotalValue <= 21) ||
(playerHandTotalValue <= 21 && dealerHandTotalValue > 21)) {
outputMessage = displayPlayerAndDealerHands(playerHand, dealerHand) + "<br>Player wins!" + displayHandTotalValues(playerHandTotalValue, dealerHandTotalValue);
}
// Dealer wins when above two conditions are not met
else {
outputMessage = displayPlayerAndDealerHands(playerHand, dealerHand) + "<br>Dealer wins!" + displayHandTotalValues(playerHandTotalValue, dealerHandTotalValue);
}
// 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 = GAME_RESULTS_SHOWN;
}
// Input validation when player inputs anything outside of 'hit' or 'stand'
else {
outputMessage = 'wrong input... only "hit" or "stand" are valid.<br><br>' + displayPlayerAndDealerHands(playerHand, dealerHand);
}
// return output message
return outputMessage;
}
};