-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
352 lines (307 loc) · 8.18 KB
/
javascript.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
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
"use strict";
// Ordering, shuffling, and valuating the cards
let cardDeck;
let shuffled;
const suits = ["H", "D", "S", "C"];
function orderCards() {
cardDeck = [];
for (let i = 0; i < suits.length; i++) {
let currentSuit = suits[i];
cardDeck.push(
`A${currentSuit}`,
`2${currentSuit}`,
`3${currentSuit}`,
`4${currentSuit}`,
`5${currentSuit}`,
`6${currentSuit}`,
`7${currentSuit}`,
`8${currentSuit}`,
`9${currentSuit}`,
`10${currentSuit}`,
`J${currentSuit}`,
`K${currentSuit}`,
`Q${currentSuit}`
);
}
shuffled = false;
}
function shuffleCards() {
for (let i = cardDeck.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[cardDeck[i], cardDeck[j]] = [cardDeck[j], cardDeck[i]];
}
shuffled = true;
}
function convertCard(card) {
let num = card[0];
if (num === "A") {
return [1, 11];
} else if (num === "J" || num === "K" || num === "Q" || num === "1") {
return [10, 10];
} else {
let x = Number(num);
return [x, x];
}
}
// Initilizing the game
let playerOne;
let dealer;
let currentCard = 0;
let activePlayer;
let onDeck;
const init = function () {
playerOne = {
name: "Player One",
sum: [0, 0],
activeSum: 0,
roundsWon: 0,
chips: 50,
bet: 0,
inHand: [],
choice: "",
inGame: true,
};
dealer = {
name: "Jarvis",
sum: [0, 0],
activeSum: 0,
roundsWon: 0,
inHand: [],
inGame: true,
};
activePlayer = playerOne;
orderCards();
shuffleCards();
round();
};
function changePlayers() {
if (activePlayer === playerOne) {
activePlayer = dealer;
onDeck = playerOne;
} else if (activePlayer === dealer) {
activePlayer = playerOne;
onDeck = dealer;
}
}
function activeSum() {
if (activePlayer.sum[0] === activePlayer.sum[1]) {
activePlayer.activeSum = activePlayer.sum[0];
} else if (
activePlayer.sum[0] > activePlayer.sum[1] &&
activePlayer.sum[0] < 22
) {
activePlayer.activeSum = activePlayer.sum[0];
} else if (
activePlayer.sum[1] > activePlayer.sum[0] &&
activePlayer.sum[1] < 22
) {
activePlayer.activeSum = activePlayer.sum[1];
} else {
activePlayer.activeSum = `n/a`;
}
}
// Players place bets
function placeBets() {
playerOne.bet = Number(
prompt(
`${playerOne.name}, you have ${playerOne.chips} chips. Place your bet.`
)
);
if (isNaN(playerOne.bet)) {
alert("Please type an actual number.");
playerOne.bet = 0;
placeBets();
} else if (playerOne.bet > playerOne.chips) {
alert(
`You wish you could place that bet. Try something under ${playerOne.chips}`
);
playerOne.bet = 0;
placeBets();
} else if (playerOne.bet === 0) {
alert(`Does that mean you pass? C'mon man, place a real bet.`);
placeBets();
} else if (playerOne.bet < 0) {
alert(`Does that mean I give you chips? C'mon man, place a real bet.`);
playerOne.bet = 0;
placeBets();
} else if (playerOne.bet % 1 !== 0) {
alert(`Cut my chips, I cut you. Place a real bet.`);
playerOne.bet = 0;
placeBets();
} else {
changePlayers();
}
}
// Dealer deals cards to each player
function dealCards() {
let currentValue = convertCard(cardDeck[currentCard]);
activePlayer.inHand.push(cardDeck[currentCard]);
activePlayer.sum = [
activePlayer.sum[0] + currentValue[0],
activePlayer.sum[1] + currentValue[1],
];
console.log(cardDeck[currentCard]);
console.log(`${activePlayer.name}: ${activePlayer.inHand}`);
currentCard++;
}
function isBusted() {
if (activePlayer.sum[0] < 22 || activePlayer.sum[1] < 22) {
return false;
} else {
return true;
}
}
function isNatural() {
if (activePlayer.sum[0] === 21 || activePlayer.sum[1] === 21) {
return true;
} else {
return false;
}
}
function notAce() {
if (activePlayer.sum[0] === activePlayer.sum[1]) {
return true;
} else {
return false;
}
}
function playerBust() {
let busted;
if (activePlayer.sum[0] === activePlayer.sum[1]) {
busted = activePlayer.sum[0];
} else if (activePlayer.sum[0] > activePlayer.sum[1]) {
busted = activePlayer.sum[1];
} else {
busted = activePlayer.sum[0];
}
alert(`That brings your total to ${busted}. That's a bust.`);
activePlayer.inGame = false;
}
// Game Play
function round() {
placeBets();
activePlayer = playerOne;
while (dealer.inHand.length < 2) {
dealCards();
changePlayers();
}
activePlayer = playerOne;
if (isNatural()) {
alert(`Look at you! It's a natural blackjack.`);
playerOne.chips = trunc(1.5 * playerOne.bet);
playerOne.roundsWon++;
nextRound();
} else {
console.log(`No player naturals.`);
}
activePlayer = dealer;
if (isNatural()) {
aler(`That's a natural blackjack for the house. Pay up, buttercup.`);
playerOne.chips -= playerOne.bet;
playerOne.roundsWon--;
}
activePlayer = playerOne;
while (activePlayer !== dealer) {
while (activePlayer.choice !== "stand" && activePlayer.choice !== "Stand") {
if (activePlayer.choice === "hit" || activePlayer.choice == "Hit") {
dealCards();
if (isBusted()) {
playerBust();
}
}
if (activePlayer.inGame) {
if (notAce()) {
activePlayer.choice = prompt(
`${activePlayer.name}, you're up. You currently have a ${activePlayer.sum[0]}. Hit or stand?`
);
} else if (activePlayer.sum[0] < 22 && activePlayer.sum[1] < 22) {
activePlayer.choice = prompt(
`${activePlayer.name}, you're up. You currently have either ${activePlayer.sum[0]} or ${activePlayer.sum[1]}. Hit or stand?`
);
} else if (activePlayer.sum[0] < 22) {
activePlayer.choice = prompt(
`${activePlayer.name}, you're up. You currently have a ${activePlayer.sum[0]}. Hit or stand?`
);
} else if (activePlayer.sum[1] < 22) {
activePlayer.choice = prompt(
`${activePlayer.name}, you're up. You currently have a ${activePlayer.sum[1]}. Hit or stand?`
);
}
} else {
break;
}
}
activePlayer.choice = "";
changePlayers();
}
// Round is over. Dealer's turn.
activePlayer = dealer;
activeSum();
while (dealer.activeSum < 17) {
console.log(`Dealer's active sum is ${dealer.activeSum}`);
if (notAce()) {
alert(`I've got a ${dealer.sum[0]}, so I'll hit.`);
} else {
alert(
`I've got either a ${dealer.sum[0]} or a ${dealer.sum[1]}, so I'll hit.`
);
}
dealCards();
activeSum();
}
if (isBusted()) {
dealer.inGame = false;
alert(`And that's a bust for me. The winner this round is...`);
roundWinner();
} else if (dealer.sum[0] === 21 || dealer.sum[1] === 21) {
alert(
`And that's a Blackjack for the house. Let's tally the score. The winner is...`
);
roundWinner();
} else if (dealer.sum[0] >= 17 && dealer.sum[0] < 21) {
alert(`I stand with a ${dealer.sum[0]}. And the winner is...`);
roundWinner();
} else if (dealer.sum[1] >= 17 && dealer.sum[1] < 21) {
alert(`I stand with a ${dealer.sum[0]}. And the winner is...`);
roundWinner();
} else {
alert(`Something went terribly wrong...`);
}
function roundWinner() {
activePlayer = playerOne;
for (let i = 0; i < 2; i++) {
if (activePlayer.inGame) {
activeSum();
changePlayers();
} else {
activePlayer.activeSum = 0;
changePlayers();
}
}
if (playerOne.activeSum > dealer.activeSum) {
alert(`${playerOne.name}!!!`);
playerOne.chips += playerOne.bet;
playerOne.roundsWon++;
} else if (dealer.activeSum > playerOne.activeSum) {
alert(`Me you losers!!!`);
playerOne.chips -= playerOne.bet;
dealer.roundsWon++;
} else if (playerOne.activeSum === dealer.activeSum) {
alert(`It's a tie between ${playerOne.name} and the house!`);
}
}
nextRound();
}
function nextRound() {
playerOne.bet = 0;
playerOne.inGame = true;
dealer.inGame = true;
playerOne.inHand = [];
dealer.inHand = [];
playerOne.activeScore = 0;
dealer.activeScore = 0;
playerOne.sum = [0, 0];
dealer.sum = [0.0];
round();
}
init();