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
469 lines (425 loc) · 14.1 KB
/
script.js
File metadata and controls
469 lines (425 loc) · 14.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// global variables
// game modes
var USERNAME_MODE = "Username Mode";
var SHUFFLE_DECK_MODE = "Shuffle Deck Mode";
var DEAL_CARD_MODE = "Deal Card Mode";
var PLAYER_MODE = "Player Mode";
var COMPUTER_MODE = "Computer Mode";
var DECIDE_WINNER_MODE = "Decide Winner Mode";
var BET_MODE = "Bet Mode";
var CHOOSE_NUM_OF_PLAYER = "Choose number of players";
var GAME_MODE = CHOOSE_NUM_OF_PLAYER;
// global values
var deck = [];
var computerHand = [];
var players = [];
var currentPlayer = 0;
var numOfPlayers = 0;
//________________________ Main Game________________ //
// main function to call the different function to run the game in different modes
var main = function (input) {
var myOutputValue = "";
if (GAME_MODE === CHOOSE_NUM_OF_PLAYER) {
myOutputValue = setNumPlayers(input);
} else if (GAME_MODE === USERNAME_MODE) {
myOutputValue = setUsername(input);
} else if (GAME_MODE === SHUFFLE_DECK_MODE) {
myOutputValue = createNewDeck(input);
} else if (GAME_MODE === BET_MODE) {
myOutputValue = playerBet(input);
} else if (GAME_MODE === DEAL_CARD_MODE) {
myOutputValue = dealCards(input);
} else if (GAME_MODE === PLAYER_MODE) {
myOutputValue = playerTurn(input);
} else if (GAME_MODE === COMPUTER_MODE) {
myOutputValue = computerTurn(input);
}
// console log to track which mode we are current in
console.log("GAME_MODE = " + GAME_MODE);
return myOutputValue;
};
//______________________ Mode to set num of players_________________ //
var setNumPlayers = function (input) {
var myOutputValue = `Welcome to Blackjack ♣︎♥︎♠︎♦︎!<br><br>How many players are there ⛹🏻♂️ ⛹🏻♀️ ?`;
if (Number(input)) {
numOfPlayers = input;
myOutputValue = `${input} players vs Computer/Dealer 😈!<br><br>Tell us your names! =)<br><br>Separate your names with a space`;
GAME_MODE = USERNAME_MODE;
}
return myOutputValue;
};
//______________________ Mode to set username _________________ //
// Using this function to set username and also to create the important Players array which will house all the players information including their hand, bet, points etc. Hand is another array which will house the cards objects
var setUsername = function (input) {
var myOutputValue = `Tell us your names! =)<br><br>Separate your names with a space`;
// if Go! is hit without any input, do not run the below code
if (input != "") {
var splitNames = input.split(" ");
var names = "";
var counter = 0;
while (counter < numOfPlayers) {
// Define players array
var createPlayer = {
order: counter,
name: `${splitNames[counter]}`,
points: 100,
bet: 0,
hand: [],
};
players.push(createPlayer);
names = names + ` ${players[counter].name},`;
counter += 1;
}
console.log(players);
myOutputValue = `Hello ${names}!<br><br>Welcome to Blackjack! ♣︎♥︎♠︎♦︎`;
GAME_MODE = SHUFFLE_DECK_MODE;
}
return myOutputValue;
};
//__________________ Mode for shuffle Deck ___________//
// function to creat a new deck and shuffle. This function will also reset all the players hand.
var createNewDeck = function () {
var myOutputValue = `The deck has been shuffled! 🂡🂱🃁🃑 <br><br>${players[0].name} please place your bets.💰`;
var newDeck = shuffleCards(makeDeck());
deck = newDeck;
console.log(deck);
computerHand = [];
// this is to reset all player's hand
var counter = 0;
while (counter < players.length) {
players[counter].hand = [];
counter += 1;
}
GAME_MODE = BET_MODE;
return myOutputValue;
};
//________ mode for players to bet _____________//
// mode to set the the bets for each player. will push the bets in the Players array with key = bet.
var playerBet = function (input) {
var myOutputValue = "";
if (
Number(input) &&
currentPlayer < players.length - 1 &&
Number(input) <= players[currentPlayer].points
) {
// setting the bet into each player's bet key
players[currentPlayer].bet = input;
myOutputValue = `${players[currentPlayer].name}'s bet is ${input}`;
currentPlayer += 1;
} else if (
Number(input) &&
currentPlayer == players.length - 1 &&
Number(input) <= players[currentPlayer].points
) {
myOutputValue = `${players[currentPlayer].name}'s bet is ${input}`;
GAME_MODE = DEAL_CARD_MODE;
currentPlayer = 0;
} else {
myOutputValue =
myOutputValue +
`<br><br>${players[currentPlayer].name} please place your bets.💰`;
}
if (Number(input) > players[currentPlayer].points) {
myOutputValue =
myOutputValue + `<br><br>Your bet cannot be more than your points.`;
}
return myOutputValue;
};
//_______________ Mode to deal cards _____________//
// this function will deal the cards to players and computer.
var dealCards = function () {
var myOutputValue = "";
// using loops to push each players card into their respective hand's array
var dealCounter = 1;
while (dealCounter <= 2) {
var computerCard = deck.pop();
computerHand.push(computerCard);
while (currentPlayer < players.length) {
var playerCard = deck.pop();
players[currentPlayer].hand.push(playerCard);
currentPlayer += 1;
}
dealCounter += 1;
currentPlayer = 0;
}
// immediately after cards are dealt, we will run a check for any blackjacks.
while (currentPlayer < players.length) {
myOutputValue =
myOutputValue +
`<br><br>${players[currentPlayer].name} cards are` +
`${currentHand(players[currentPlayer].hand)}`;
if (isBlackjack(players[currentPlayer].hand)) {
myOutputValue = myOutputValue + ` BLACKJACK!!`;
}
currentPlayer += 1;
}
currentPlayer = 0;
// check that if Computer has blackjack game is over.
if (isBlackjack(computerHand)) {
myOutputValue =
myOutputValue +
`<br><br>Computer BLACKJACK with ${currentHand(computerHand)}🎉` +
`<br><br>This round is over.`;
// if Computer has blackjack, program would update the points with the bets. This is done with the checkWin function (function found at end of code)
var counter = 0;
while (counter < players.length) {
myOutputValue = myOutputValue + checkWin(players[counter]);
counter += 1;
}
// getPointsSummary is just to give a update of all Players Points.
myOutputValue = myOutputValue + `${getPointsSummary()}`;
GAME_MODE = SHUFFLE_DECK_MODE;
} else {
myOutputValue =
myOutputValue +
`<br><br>Computer's face up card is ${cardName(computerHand[0])}` +
`<br><br>${players[currentPlayer].name} will go first`;
GAME_MODE = PLAYER_MODE;
}
return myOutputValue;
};
//__________________ Mode where it is Player's Turn______//
// function will allow players to either hit or stand.
var playerTurn = function (input) {
var myOutputValue = ``;
if (input == "hit") {
var playerCard = deck.pop();
players[currentPlayer].hand.push(playerCard);
myOutputValue =
`${players[currentPlayer].name} hit ` + `${cardName(playerCard)}`;
}
myOutputValue =
myOutputValue +
`<br><br>${players[currentPlayer].name} cards are ` +
`${currentHand(players[currentPlayer].hand)}`;
// condition if that player has a blackjack, there is nothing to be done. Just wait for other players turn.
if (isBlackjack(players[currentPlayer].hand)) {
myOutputValue =
`${players[currentPlayer].name} cards are ` +
`${currentHand(players[currentPlayer].hand)}` +
`<br><br>${players[currentPlayer].name} has a BLACKJACK!🎉`;
currentPlayer += 1;
} else if (
checkScore(players[currentPlayer].hand) > 21 &&
currentPlayer !== players.length - 1
) {
myOutputValue =
myOutputValue + `<br><br>${players[currentPlayer].name} BUST!!`;
currentPlayer += 1;
} else if (checkScore(players[currentPlayer].hand) < 22) {
myOutputValue = myOutputValue + `<br><br>Please type "hit or "stand"`;
}
if (
checkScore(players[currentPlayer].hand) > 21 &&
currentPlayer == players.length - 1
) {
myOutputValue =
myOutputValue +
`<br><br>${players[currentPlayer].name} BUST!!` +
`<br><br>Computer's face down card was ` +
`${cardName(computerHand[0])}` +
`<br><br>It is now Computer's turn.`;
GAME_MODE = COMPUTER_MODE;
}
if (input == "stand" && currentPlayer == players.length - 1) {
// after last player has made their decision, we ill move to next game mode
myOutputValue =
`Computer's face down card was ` +
`${cardName(computerHand[0])}` +
`<br><br>It is now Computer's turn.`;
GAME_MODE = COMPUTER_MODE;
} else if (input == "stand") {
currentPlayer += 1;
myOutputValue = `It is now ${players[currentPlayer].name}'s turn.`;
}
return myOutputValue;
};
//__________________ Mode where it is computer's Turn______//
// function will automatically decide how many cards Computer needs to draw in order to hit 17.
var computerTurn = function (input) {
var myOutputValue = "";
var counter = checkScore(computerHand);
while (counter < 17) {
var computerCard = deck.pop();
computerHand.push(computerCard);
counter = checkScore(computerHand);
}
myOutputValue = `Computer's hand is ${currentHand(computerHand)}.`;
if (checkScore(computerHand) > 21) {
myOutputValue = myOutputValue + ` Computer BUST!!`;
}
// checkWin function will determine if player has won or lose and update their points.
currentPlayer = 0;
while (currentPlayer < players.length) {
myOutputValue = myOutputValue + checkWin(players[currentPlayer]);
currentPlayer += 1;
}
myOutputValue = myOutputValue + `${getPointsSummary()}`;
GAME_MODE = SHUFFLE_DECK_MODE;
currentPlayer = 0;
return myOutputValue;
};
//____________________ SUPPORTING Functions____________ //
var checkWin = function (input) {
var myOutputValue = "";
if (isBlackjack(input.hand) == true && isBlackjack(computerHand) == false) {
myOutputValue =
myOutputValue +
`<br><br>${input.name}'s cards are ` +
`${currentHand(input.hand)}. ` +
`${input.name} BLACKJACK double your bet! 🙌🏻 `;
input.points += Number(input.bet) * 2;
} else if (
(checkScore(input.hand) > checkScore(computerHand) &&
checkScore(input.hand) < 22) ||
(checkScore(input.hand) < 22 && checkScore(computerHand) > 21)
) {
myOutputValue =
myOutputValue +
`<br><br>${input.name}'s cards are ` +
`${currentHand(input.hand)}. ` +
`${input.name} WON 🙌🏻 !`;
input.points += Number(input.bet);
} else if (
(checkScore(input.hand) < checkScore(computerHand) &&
checkScore(computerHand) < 22) ||
(checkScore(input.hand) > 22 && checkScore(computerHand) < 22)
) {
input.points -= Number(input.bet);
myOutputValue =
myOutputValue +
`<br><br>${input.name}'s cards are ` +
`${currentHand(input.hand)}. ` +
`${input.name} LOST ☹️ `;
} else if (
checkScore(input.hand) == checkScore(computerHand) ||
(checkScore(input.hand) > 21 && checkScore(computerHand) > 21) ||
(isBlackjack(input.hand) == true && isBlackjack(computerHand) == true)
) {
myOutputValue =
`<br><br>${input.name}'s cards are ` +
`${currentHand(input.hand)}. ` +
`It's a Draw!`;
}
return myOutputValue;
};
var makeDeck = function () {
var cardDeck = [];
var suits = ["hearts", "diamonds", "clubs", "spades"];
var suitIndex = 0;
while (suitIndex < suits.length) {
var currentSuit = suits[suitIndex];
var rankCounter = 1;
while (rankCounter <= 13) {
var power = rankCounter;
var cardName = rankCounter;
if (cardName == 1) {
cardName = "Ace";
} else if (cardName == 11) {
cardName = "Jack";
power = 10;
} else if (cardName == 12) {
cardName = "Queen";
power = 10;
} else if (cardName == 13) {
cardName = "King";
power = 10;
}
var card = {
name: cardName,
suit: currentSuit,
rank: rankCounter,
power: power,
};
cardDeck.push(card);
rankCounter += 1;
}
suitIndex += 1;
}
return cardDeck;
};
var getRandomIndex = function (max) {
return Math.floor(Math.random() * max);
};
var shuffleCards = function (cardDeck) {
var currentIndex = 0;
while (currentIndex < cardDeck.length) {
var randomIndex = getRandomIndex(cardDeck.length);
var randomCard = cardDeck[randomIndex];
var currentCard = cardDeck[currentIndex];
cardDeck[currentIndex] = randomCard;
cardDeck[randomIndex] = currentCard;
currentIndex = currentIndex + 1;
}
return cardDeck;
};
var cardName = function (card) {
if (card.suit == "clubs") {
var suit = "♣︎";
} else if (card.suit == "hearts") {
var suit = "♥︎";
} else if (card.suit == "spades") {
var suit = "♠︎";
} else if (card.suit == "diamonds") {
var suit = "♦︎";
}
return `${card.name}${suit}`;
};
var currentHand = function (input) {
var counter = 0;
var name = "";
while (counter < input.length) {
name = name + ` ${cardName(input[counter])}`;
counter += 1;
}
return name;
};
var checkScore = function (input) {
var counter = 0;
var score = 0;
while (counter < input.length) {
score = score + input[counter].power;
counter += 1;
}
if (numOfAce(input) == 1 && score <= 11) {
score += 10;
}
if (numOfAce(input) == 2 && score <= 11) {
score += 10;
}
if (numOfAce(input) == 3 && score <= 11) {
score += 10;
}
if (numOfAce(input) == 4 && score <= 11) {
score += 10;
}
return score;
};
var numOfAce = function (input) {
var number = 0;
var counter = 0;
while (counter < input.length) {
if (input[counter].rank == 1) {
number += 1;
}
counter += 1;
}
return number;
};
var isBlackjack = function (input) {
return (
(input[0].name == "Ace" && input[1].rank >= 10) ||
(input[1].name == "Ace" && input[0].rank >= 10)
);
};
var getPointsSummary = function () {
var myOutputValue = "<br>";
var counter = 0;
while (counter < players.length) {
myOutputValue =
myOutputValue +
`<br>${players[counter].name}'s points ➡ ${players[counter].points}`;
counter += 1;
}
return myOutputValue;
};