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
368 lines (347 loc) · 13.1 KB
/
script.js
File metadata and controls
368 lines (347 loc) · 13.1 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// ================== Deck Creation ================= \\
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,
};
// 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;
};
// ================== Deck Shuffling ================= \\
// 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) {
// Select a random index in the deck
var randomIndex = getRandomIndex(cardDeck.length);
// Select the card that corresponds to randomIndex
var randomCard = cardDeck[randomIndex];
// 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;
};
// ================== Misc Variables ================= \\
var deck = shuffleCards(makeDeck())
var playerCards = [];
var dealerCards = [];
//var playerNames = [];
var burstSum = 22
var dealerHitThreshold = 16
var playerSum;
var dealerSum;
var myOutputValue = ''
var playerPoints = Number(100)
var userName = ''
// ================== Game Modes ================= \\
var welcomeMessage = true
var userNameRecord = false
var betting = false
var initialDeal = false
var hitOrStand = false
var gameOver = false
// ================== Card Dealing Function ================= \\
var dealCard = function(cards) {
cards.push(deck.pop());
};
// ================== Sum Comparing Function ================= \\
var compareSums = function(playerSum, dealerSum){
console.log(`comparing the sums`)
playerSum = getSum(playerCards);
dealerSum = getSum(dealerCards);
// draw; both did not burst
if (playerSum == dealerSum && playerSum < burstSum && dealerSum < burstSum){
myOutputValue = `${drawMessage} ${showPlayerCards()} ${showDealerCards()} ${refreshMessage}`
console.log('Draw, both did not burst')
gameOver = true
hitOrStand = false
return myOutputValue
}
// check if player burst first
if (playerSum >= burstSum && dealerSum < burstSum){
myOutputValue = `${playerLostMessage} ${showPlayerCards()} ${showDealerCards()} ${refreshMessage}`
console.log(`Player burst, dealer did not`)
gameOver = true
hitOrStand = false
playerPoints -= currentBet
return myOutputValue
}
// check if dealer burst, if player did not burst
if (playerSum < burstSum && dealerSum >= burstSum){
myOutputValue = `${playerWonMessage} ${showPlayerCards()} ${showDealerCards()} ${refreshMessage}`
console.log(`Dealer burst, player did not`)
gameOver = true
hitOrStand = false
playerPoints += currentBet
return myOutputValue
}
// if both players did not burst, check. Player value is less than dealer
if (playerSum < dealerSum && dealerSum < burstSum && playerSum < burstSum){
myOutputValue = `${playerLostMessage} ${showPlayerCards()} ${showDealerCards()} ${refreshMessage}`
console.log(`Player lost, both did not burst`)
gameOver = true
hitOrStand = false
playerPoints -= currentBet
return myOutputValue
}
// if both players did not burst, check. Player value is more than dealer
if (playerSum > dealerSum && dealerSum < burstSum && playerSum < burstSum){
myOutputValue = `${playerWonMessage} ${showPlayerCards()} ${showDealerCards()} ${refreshMessage}`
console.log(`Player won, both did not burst`)
gameOver = true
hitOrStand = false
playerPoints += currentBet
return myOutputValue
}
}
// ================== Check for BlackJack ================= \\
var checkBlackJack = function(){
console.log(`Checking for BlackJack`)
// check only the first two cards
// check if dealer has blackjack first
if(dealerCards.length == 2 && getSum(dealerCards) == 21){
gameOver = true
hitOrStand = false
playerPoints -= currentBet
myOutputValue = `${playerLostMessage} ${showDealerCards()} ${dealerBlackJackMessage} ${refreshMessage}`
console.log(`Dealer got Black Jack`)
return playerLostMessage
} // if dealer did not have blackjack, check if player has
else if (playerCards.length == 2 && getSum(playerCards) == 21 && getSum(dealerCards) != 21){
gameOver = true
hitOrStand = false
playerPoints += currentBet
myOutputValue = `${playerWonMessage} ${showPlayerCards()} ${playerBlackJackMessage} ${refreshMessage}`
console.log(`Player got Black Jack`)
return playerWonMessage
}
}
// ================== Sum of cards ================= \\
var getSum = function(playerOrDealerCards){
console.log(`Trying to get sum`)
var sum = 0
var aceCounter = 0
// loop to add value of each card to the current sum
for(let cardCounter = 0; cardCounter < playerOrDealerCards.length; cardCounter += 1){
currentCard = playerOrDealerCards[cardCounter]
// if card is 2-10, add exact value of rank
if (currentCard.rank >= 2 && currentCard.rank <= 10) {
sum += currentCard.rank;
} // if card is jack, queen or king, add 10 instead
else if (currentCard.rank >= 11 && currentCard.rank <= 13) {
sum += 10
} // if card is ace, add 11
else if (currentCard.rank == 1){
aceCounter += 1
sum += 11
}
}
// if there is an ace in dealer/player's hand and it exceeds 21, change ace's value, one at a time, from 11 to 1, until it is 21 or less.
if(sum >= burstSum && aceCounter > 0){
for(checkingAllTheAcesCounter = 0; checkingAllTheAcesCounter < aceCounter ; checkingAllTheAcesCounter += 1){
sum -= 10
}
}
console.log(`sum ${sum}`)
console.log(`finished summing`)
return sum
}
// ================== Output Messages ================= \\
var playerWonMessage = `You Won! <br>`
var playerLostMessage = `You Lost! <br>`
var drawMessage = `It is a draw! <br>`
var refreshMessage = `Please type "again" to start a new game. <br>`
var restartMessage = `You do not have enough points to continue betting! Please refresh the website to play again.`
var hitOrStandMessage = `Would you like to "Hit" or "Stand"? <br>`
var playerBlackJackMessage = `You have gotten BlackJack! <br>`
var dealerBlackJackMessage = `Dealer had a BlackJack! <br>`
var userNameInputMessage = `Hello, my name is Kenneth. I will be your dealer. Please input your name. <br>`
var secretCheatMessage = `Nice try! Please do not cheat :'). <br>`
var betMessage = ``
// converting message to string (copied as kept getting undefined in messages)
var convertHandToString = function (hand) {
return `[${hand.map((card) => card.name)}]`;
};
var showPlayerCards = function () {
return `You have hand ${convertHandToString(playerCards)} with sum ${getSum(playerCards)}. <br>`
};
var showDealerCards = function () {
return `Dealer has hand ${convertHandToString(dealerCards)} with sum ${getSum(dealerCards)}. <br>`
}
// ================== Main Function ================= \\
var main = function(input){
// welcomes the player
if(welcomeMessage == true){
console.log (`welcome message and ask player for their name`)
myOutputValue = `${userNameInputMessage}`
welcomeMessage = false
return myOutputValue
}
// check if there is a player name
if(userNameRecord == false && input != ""){
console.log(`asking player for their bet`)
userName = input
userNameRecord = true
myOutputValue = `Hi ${userName}! Let's play Black Jack! You currently have ${playerPoints} points. How much would you like to bet? (Min. 2 pts)`
return myOutputValue
}
// check if game is completed
if(gameOver == true){
// reset the game if user types this input, keeping username and playerPoints
if(input == "again"){
gameOver = false
betting = false
initialDeal = false
playerCards = []
dealerCards = []
deck = shuffleCards(makeDeck())
myOutputValue = `Hi ${userName}! Let's play Black Jack! You currently have ${playerPoints} points. How much would you like to bet? (Min. 2 pts)`
// if player has less than the minimum bet, ask them to restart game
if(playerPoints <2){
myOutputValue = restartMessage
return myOutputValue
}
return myOutputValue
}
return refreshMessage
}
// ask player on bet amount
if(betting == false && playerPoints >= 2 && input >= 2){
// if player inputs more than playerPoints
if(input > playerPoints){
myOutputValue = `${secretCheatMessage} You currently have ${playerPoints} points. How much would you like to bet? (Min. 2 pts)`
return myOutputValue
}
console.log (`recording player's bet and ready to deal cards`)
// track the player's bet
currentBet = Number(input)
console.log()
initialDeal = true
betting = true
myOutputValue = `Sure ${userName}! You bet ${currentBet}. <br> May luck be in your favour. <br> Press submit to deal cards.`
return myOutputValue
}
// deal first card to dealer, then player
if(playerCards.length == 0 && initialDeal == true){
dealCard(playerCards);
dealCard(dealerCards);
}
// deal second card to dealer, then player
if(playerCards.length == 1 && initialDeal == true){
dealCard(playerCards);
dealCard(dealerCards);
initialDeal = false
console.log(`Player Cards`)
console.log(playerCards)
console.log(`Dealer Cards`)
console.log(dealerCards)
console.log(`Finished dealing cards`)
}
if(playerCards.length == 2 && gameOver == false && hitOrStand == false && betting == true){
// Check if either players got Black Jack
checkBlackJack();
if(gameOver == true){
hitOrStand = false
console.log(`There was a BlackJack`)
} else if(gameOver == false){
console.log(`No BlackJack`)
hitOrStand = true
return `${showPlayerCards()} ${hitOrStandMessage}`
}
}
// ================== Player Hit or Stand ================= \\
// If the player input anything other than hit or stand, ask them to do so.
if((hitOrStand == true && input != "hit") && (hitOrStand == true && input != "stand")){
myOutputValue = `${showPlayerCards()} ${hitOrStandMessage}`
return myOutputValue
}
// player chooses hit
if(hitOrStand == true && input == `hit`){
console.log(`Player decided to hit`)
dealCard(playerCards)
console.log(`player cards`)
console.log(playerCards)
myOutputValue = `${showPlayerCards()} ${hitOrStandMessage}`
// player busts while hitting
if(getSum(playerCards) >= burstSum){
gameOver = true
hitOrStand = false
playerPoints -= currentBet
myOutputValue = `${playerLostMessage} ${showPlayerCards()} You Bust!`
return myOutputValue
}
}
// player chooses stand
if(hitOrStand == true && input == `stand`){
// player can no longer hit
hitOrStand = false
console.log(`Player decided to stand`)
// ================== Dealer Hit or Stand ================= \\
// If dealer has 16 or less total value
while (getSum(dealerCards) <= dealerHitThreshold && gameOver == false) {
dealCard(dealerCards);
console.log(`dealer drawing more cards`)
dealerSum = getSum(dealerCards);
console.log(`Dealer cards`)
console.log(dealerCards)
// Dealer bursts
if (dealerSum >= burstSum) {
gameOver = true;
hitOrStand = false
playerPoints += currentBet
return `${playerWonMessage} ${showPlayerCards()} ${showDealerCards()} Dealer bust! `;
}
console.log(`Dealer stopped drawing more cards`)
}
// Determine a winner
compareSums()
}
return myOutputValue
}