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
323 lines (283 loc) · 8.17 KB
/
script.js
File metadata and controls
323 lines (283 loc) · 8.17 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
//make a deck of cards
var makeDeck = function () {
var cardDeck = [];
var suitIndex = 0;
var suit = [`hearts`, `spades`, `diamonds`, `clubs`];
while (suitIndex < 4) {
var currentSuit = suit[suitIndex];
var rankIndex = 1;
while (rankIndex < 14) {
var cardName = ``;
if (rankIndex == 1) {
cardName = `ace`;
} else if (rankIndex == 11) {
cardName = `jack`;
} else if (rankIndex == 12) {
cardName = `queen`;
} else if (rankIndex == 13) {
cardName = `king`;
} else {
cardName = rankIndex;
}
var card = {
name: cardName,
suit: currentSuit,
rank: rankIndex,
};
if (card.rank == 11 || card.rank == 12 || card.rank == 13) {
card.rank = 10;
}
cardDeck.push(card);
rankIndex += 1;
}
suitIndex += 1;
}
return cardDeck;
};
var cardDeck = makeDeck();
//get a random number
var getRandomNum = function (max) {
return Math.floor(Math.random() * max);
};
//shuffle deck of cards
var shuffleDeck = function (cardDeck) {
var cardDeckLength = cardDeck.length;
var cardIndex = 0;
while (cardIndex < cardDeckLength) {
var randomNum = getRandomNum(cardDeckLength);
var randomCard = cardDeck[randomNum];
var currentCard = cardDeck[cardIndex];
cardDeck[randomNum] = currentCard;
cardDeck[cardIndex] = randomCard;
cardIndex += 1;
}
return cardDeck;
};
var shuffledCardDeck = shuffleDeck(cardDeck);
var STARTGAME = `start game`;
var HITORSTAND = `hit or stand`;
var HIT = `hit`;
var STAND = `stand`;
var ENDROUND = `end of round`;
var COMPUTERCHOICE = `computer to choose hit or stand`;
var BET = `player bet`;
var gameMode = BET;
var playerCards = [];
var computerCards = [];
var playerSum = 0;
var computerSum = 0;
var playerMoney = 100;
var playerBet = 0;
var betMoney = function (betAmount) {
var message = ``;
if (isNaN(betAmount) !== true) {
playerBet = betAmount;
console.log(playerBet);
message =
`You have ${playerMoney}.` +
"<br><br>" +
`Your bet is ${playerBet}.` +
"<br><Br>" +
`Press <b>Submit</b> to draw two cards.`;
gameMode = STARTGAME;
} else {
message = `Please enter bet amount.`;
}
return message;
};
//computer and player get dealt two cards to start. Store in global variables.
var drawFirstTwoCards = function () {
//computer draws 2 cards
computerCards.push(shuffledCardDeck.pop());
computerCards.push(shuffledCardDeck.pop());
console.log(computerCards);
computerSum = computerCards[0].rank + computerCards[1].rank;
console.log(computerSum);
//player draws 2 cards
playerCards.push(shuffledCardDeck.pop());
playerCards.push(shuffledCardDeck.pop());
console.log(playerCards);
playerSum = playerCards[0].rank + playerCards[1].rank;
console.log(playerSum);
if (
playerSum == 21 ||
computerSum == 21 ||
(playerSum == 21 && computerSum == 21)
) {
gameMode = ENDROUND;
return endRound();
} else {
var showDrawnCards = displayCards();
var message =
showDrawnCards +
"<br><br>" +
`Would you like to <b>hit</b> or <b>stand</b>?`;
gameMode = HITORSTAND;
console.log("After drawing first 2 cards: " + gameMode);
}
return message;
};
var displayCards = function () {
var message = ``;
var showComputerCard = `Dealer has drawn:` + "<br>";
var showPlayerCard = `You have drawn:` + "<br>";
var showPlayerSum = displayPlayerSum();
var showComputerSum = displayComputerSum();
if (gameMode == ENDROUND) {
for (let index = 0; index < computerCards.length; index++) {
showComputerCard +=
`<b>${computerCards[index].name} of ${computerCards[index].suit}</b>` +
"<br>";
}
showComputerCard += showComputerSum;
} else {
showComputerCard +=
`<b>${computerCards[0].name} of ${computerCards[0].suit}</b>` +
"<br>" +
`& ${computerCards.length - 1} covered cards` +
"<br>";
}
for (let index = 0; index < playerCards.length; index++) {
showPlayerCard +=
`<b>${playerCards[index].name} of ${playerCards[index].suit}</b>` +
"<br>";
}
showPlayerCard += showPlayerSum;
message = showComputerCard + "<br><br>" + showPlayerCard;
return message;
};
var displayPlayerSum = function () {
var sumOfPlayer = 0;
var showSum = ``;
for (let index = 0; index < playerCards.length; index++) {
sumOfPlayer += playerCards[index].rank;
}
playerSum = sumOfPlayer;
//if ace + card < 12, + 10 to sumOfPlayer.
for (let index = 0; index < playerCards.length; index++) {
var currentCardName = playerCards[index].name;
if (currentCardName == `ace` && playerSum < 12) {
playerSum += 10;
console.log(playerSum);
}
}
showSum = `SUM: ${playerSum}` + "<br>";
return showSum;
};
var displayComputerSum = function () {
var sumOfComp = 0;
var showSum = ``;
for (let index = 0; index < computerCards.length; index++) {
sumOfComp += computerCards[index].rank;
}
computerSum = sumOfComp;
for (let index = 0; index < computerCards.length; index++) {
var currentCardName = computerCards[index].name;
if (currentCardName == `ace` && computerSum < 12) {
computerSum += 10;
console.log(computerSum);
}
}
showSum = `SUM: ${computerSum}` + "<br>";
return showSum;
};
//Player decides if they want to hit (draw a card) or stand (end their turn)
var hitOrStand = function (userChoice) {
var message = ``;
if (userChoice == HIT) {
playerCards.push(shuffledCardDeck.pop());
console.log(playerCards);
var playerDecision = `You have chosen to HIT.`;
var showDrawnCards = displayCards();
message =
showDrawnCards +
"<br><br>" +
playerDecision +
"<br><br>Would you like to <b>hit</b> or <b>stand</b>?";
//ASK AGAIN HIT OR STAND
} else if (userChoice == STAND) {
console.log(playerCards);
gameMode = COMPUTERCHOICE;
return getComputerChoice();
} else {
message = `please enter hit or stand`;
}
return message;
};
//Computer to draw card if <17
var getComputerChoice = function () {
if (computerSum < 17) {
computerCards.push(shuffledCardDeck.pop());
var lastCardIndex = computerCards.length - 1;
computerSum += computerCards[lastCardIndex].rank;
console.log(computerSum);
return getComputerChoice();
} else {
gameMode = ENDROUND;
}
return endRound();
};
var endRound = function () {
gameMode = ENDROUND;
var showDrawnCards = displayCards();
var message =
`PLAYER SUM: ${playerSum} vs DEALER SUM: ${computerSum}.` + "<br>";
if (playerSum > 21 && computerSum > 21) {
message += `You both bust. Tie.`;
} else if (computerSum > 21 && playerSum <= 21) {
playerMoney += playerBet;
message += `Dealer bust! You won.`;
} else if (computerSum <= 21 && playerSum > 21) {
playerMoney -= playerBet;
message += `You bust! Dealer won.`;
} else if (playerSum <= 21 && computerSum <= 21) {
if (playerSum < computerSum) {
playerMoney -= playerBet;
message += `Dealer won.`;
} else if (playerSum > computerSum) {
playerMoney += playerBet;
message += `You won.`;
} else {
message += `Tie.`;
}
}
var restartGame = `Press submit to play again.`;
gameMode = STARTGAME;
playerCards = [];
computerCards = [];
playerSum = 0;
computerSum = 0;
return (
showDrawnCards +
"<br>" +
message +
"<br><br>" +
`Money: ${playerMoney}` +
"<br><br>" +
restartGame
);
//if both > 21, bust.
//if computer > 21 and player <= 21, player wins.
// if player > 21 and computer <= 21, computer wins.
//if player and computer both <= 21:
// if player > computer, player wins.
// if computer > player, computer wins.
//if player = computer, tie.
};
var main = function (input) {
var myOutputValue = ``;
console.log(gameMode);
if (gameMode == BET) {
myOutputValue = betMoney(input);
} else if (gameMode == STARTGAME) {
myOutputValue = drawFirstTwoCards();
} else if (gameMode == HITORSTAND) {
myOutputValue = hitOrStand(input);
} else if (gameMode == ENDROUND) {
myOutputValue = endRound();
} else if (gameMode == COMPUTERCHOICE) {
myOutputValue = getComputerChoice();
} else {
}
return myOutputValue;
};