-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
445 lines (418 loc) · 13.4 KB
/
app.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
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
const playerScore_span = document.getElementById("player-score-points");
const dealerScore_span = document.getElementById("dealer-score-points");
const playerCards_div = document.querySelector("#playerCards > p");
const dealerCards_div = document.querySelector("#dealerCards > p");
const winner_div = document.getElementById("winner");
const loser_div = document.getElementById("loser");
const tie_div = document.getElementById("tie");
const cardImagePlayer_span = document.getElementById("card-image-player");
const cardImageDealer_span = document.getElementById("card-image-dealer");
const play_button = document.getElementById("btn");
const totalMoney_span = document.getElementById("total-money");
const betMade_p = document.getElementById("bet-made");
const betMade_span = document.getElementById("total-bet");
const bet10_button = document.getElementById("btn-bet10");
const bet20_button = document.getElementById("btn-bet20");
const bet50_button = document.getElementById("btn-bet50");
let totalMoney = 100;
let userBet = 0;
let bet10 = 10;
let bet20 = 20;
let bet50 = 50;
let winner = false;
let tie = false;
let dealerCards = [];
let playerCards = [];
let dealerScore = 0;
let playerScore = 0;
let ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "K", "Q", "A"];
let suits = ["S", "C", "H", "D"];
let cardValues = {
"2":2,
"3":3,
"4":4,
"5":5,
"6":6,
"7":7,
"8":8,
"9":9,
"10":10,
"J":10,
"K":10,
"Q":10,
"A": [1, 11]
}
let gameEnds = false;
let cards = shuffleDeck(createDeck());
function createDeck(){
let deck = []
//Create deck made of mixing ranks and suits
for (let i = 0; i < suits.length; i++){
for (let j=0; j < ranks.length; j++){
deck.push(ranks[j] + suits[i]);
}
}
return deck;
}
function shuffleDeck(deck){
//Shuffling the deck
for(let i = 0; i < ranks.length; i++){
let temp = deck[i];
let randomIndex = Math.floor(Math.random() * deck.length);
deck[i] = deck[randomIndex];
deck[randomIndex] = temp;
}
return deck;
}
function main(){
bet10_button.addEventListener("click",function(){
bet10_button.style.visibility="hidden";
bet20_button.style.visibility="hidden";
bet50_button.style.visibility="hidden";
betMade_p.style.visibility ="visible";
betMade_span.innerHTML = bet10;
if(totalMoney >= bet10){
updateBetAmount(bet10);
play(userBet);
betMade_p.innerHTML = `Your bet: $${bet10}`;
loser_div.innerHTML = "";
}
else{
loser_div.innerHTML = "GAME OVER!";
betMade_p.innerHTML="NO MONEY";
play_button.style.visibility="visible";
}
})
bet20_button.addEventListener("click", function(){
bet10_button.style.visibility="hidden";
bet20_button.style.visibility="hidden";
bet50_button.style.visibility="hidden";
betMade_p.style.visibility ="visible";
betMade_span.innerHTML = bet20;
if(totalMoney >= bet20){
updateBetAmount(bet20);
play(userBet);
betMade_p.innerHTML = `Your bet: $${bet20}`;
loser_div.innerHTML = "";
}
else if(totalMoney == 0){
loser_div.innerHTML = "GAME OVER!";
betMade_p.innerHTML="NO MONEY";
play_button.style.visibility="visible";
}
else{
loser_div.innerHTML = "Not enough money. Choose another amount";
betMade_p.style.visibility="hidden";
bet10_button.style.visibility="visible";
bet20_button.style.visibility="visible";
bet50_button.style.visibility="visible";
}
})
bet50_button.addEventListener("click", function(){
bet10_button.style.visibility="hidden";
bet20_button.style.visibility="hidden";
bet50_button.style.visibility="hidden";
betMade_p.style.visibility ="visible";
betMade_span.innerHTML = bet50;
if(totalMoney >= bet50){
updateBetAmount(bet50);
play(userBet);
betMade_p.innerHTML = `Your bet: $${bet50}`;
loser_div.innerHTML = "";
}
else if(totalMoney == 0){
loser_div.innerHTML = "GAME OVER!";
betMade_p.innerHTML="NO MONEY";
play_button.style.visibility="visible";
}
else{
loser_div.innerHTML = "Not enough money. Choose another amount";
betMade_p.style.visibility="hidden";
bet10_button.style.visibility="visible";
bet20_button.style.visibility="visible";
bet50_button.style.visibility="visible";
}
})
}
function updateBetAmount(betAmount){
userBet = betAmount;
}
function play(betAmount){
//Get 2 first cards from shuffle deck for both dealer and player
let n = 2;
for(let i = 0; i < n; i++){
let twoFirstCardsPlayer = cards.slice(i, i+1);
twoFirstCardsPlayer.forEach(card => playerCards.push(card));
showCard();
let twoFirstCardsComputer = cards.slice(i+n, i+1+n);
twoFirstCardsComputer.forEach(card => dealerCards.push(card));
showCardDealer();
}
getScore();
ifBlackJackAtfirst(betAmount);
}
function ifBlackJackAtfirst(betAmount){
//Handling case blackjack as soon as pressing play
if(playerScore == 21 && dealerScore != 21){
playerScore_span.innerHTML = "BLACKJACK!";
dealerScore_span.innerHTML = dealerScore;
winner_div.innerHTML = "YOU WIN!"
gameEnds = true;
winner = true;
cardsFaceUP();
bet(betAmount);
}
else if(playerScore == 21 && dealerScore == 21){
playerScore_span.innerHTML = "BLACKJACK!";
dealerScore_span.innerHTML = "BLACKJACK!";
tie_div.innerHTML = "IT IS A TIE"
gameEnds = true;
cardsFaceUP();
bet(betAmount);
}
}
function determineWinner(betAmount){
//Different scenarios win/lose
if(playerScore == 21 && dealerScore != 21){
playerScore_span.innerHTML = "BLACKJACK!";
winner_div.innerHTML = "YOU WIN!"
gameEnds = true;
winner = true;
}
else if(dealerScore == 21 && playerScore != 21){
dealerScore_span.innerHTML = "BLACKJACK!";
loser.innerHTML = "YOU LOST...";
gameEnds = true;
}
else if(playerScore == 21 && dealerScore == 21){
playerScore_span.innerHTML = "BLACKJACK!";
dealerScore_span.innerHTML = "BLACKJACK!";
tie_div.innerHTML = "IT IS A TIE"
gameEnds = true;
tie = true;
}
else if(playerScore > 21){
loser_div.innerHTML = "YOU LOST...";
gameEnds = true;
}
else if(dealerScore > 21 && playerScore <= 21){
winner_div.innerHTML = "YOU WIN!";
gameEnds = true;
winner = true;
}
else if(dealerScore == playerScore){
tie_div.innerHTML = "IT IS A TIE";
gameEnds = true;
tie = true;
}
else if(playerScore > dealerScore && playerScore <=21){
winner_div.innerHTML = "YOU WIN!";
gameEnds = true;
winner = true;
}
else if(dealerScore > playerScore && dealerScore <=21){
loser.innerHTML = "YOU LOST...";
gameEnds = true;
}
bet(betAmount);
//Show dealer's score when the game is over
showingDealerScore();
}
function showingDealerScore(){
if(dealerScore < 21){
dealerScore_span.innerHTML = dealerScore;
gameEnds = true;
}
else if(dealerScore == 21){
dealerScore_span.innerHTML = "BLACKJACK!";
gameEnds = true;
}
else if(dealerScore > 21){
dealerScore_span.innerHTML = "BUSTS!";
gameEnds = true;
}
cardsFaceUP();
}
function hit(){
let betAmount = userBet;
//Giving extra card for the player
if(playerScore != 0 && playerScore != 21 && gameEnds == false){
playerCards.push(addCard())
getScore();
showCard();
if(playerScore < 21){
playerScore_span.innerHTML = playerScore;
}
else if(playerScore > 21){
playerScore_span.innerHTML = "BUSTS!";
determineWinner(betAmount);
}
else{
playerScore_span.innerHTML = "BLACKJACK!";
determineWinner(betAmount);
}
}
}
function stand(){
let betAmount = userBet;
//Giving cards to the dealer till it has more than 16 points
if(dealerScore != 0 && gameEnds == false){
while(dealerScore <= 16){
dealerCards.push(addCard())
showCardDealer();
getScore();
}
if(dealerScore>21){
dealerScore_span.innerHTML= "BUSTS!"
}
determineWinner(betAmount);
}
}
function getScore(){
playerScore = 0;
dealerScore = 0;
playerCards.forEach(function (card){
//handling Aces situation: 1 or 11 points
if(card.includes("A"))
{
if(playerScore + cardValues['A'][1] <=21)
{
playerScore += cardValues['A'][1];
}
else{
playerScore += cardValues['A'][0];
}
}
//If not Ace included
else{
playerScore += cardValues[card.slice(0, -1)];
}
});
dealerCards.forEach(function (card){
if(card.includes("A"))
{
if(dealerScore + cardValues['A'][1] <=21)
{
dealerScore += cardValues['A'][1];
}
else{
dealerScore += cardValues['A'][0];
}
}
else{
dealerScore += cardValues[card.slice(0, -1)];
}
});
playerScore_span.innerHTML = playerScore;
}
function addCard(){
//Taking a card from the end of the pile
return cards.pop();
}
function reset(){
//Reset values for new round of the game
if(gameEnds == true){
bet10_button.style.visibility="visible";
bet20_button.style.visibility="visible";
bet50_button.style.visibility="visible";
betMade_p.style.visibility ="hidden";
cards = shuffleDeck(createDeck());
dealerCards = [];
playerCards = [];
dealerScore = 0;
playerScore = 0;
gameEnds = false;
winner = false;
tie = false;
playerScore_span.innerHTML = playerScore;
dealerScore_span.innerHTML = "?";
playerCards_div.innerHTML = playerCards;
dealerCards_div.innerHTML = dealerCards;
cardImagePlayer_span.innerHTML = "";
cardImageDealer_span.innerHTML = "";
winner_div.innerHTML = "";
loser_div.innerHTML = "";
tie_div.innerHTML = "";
}
}
function playAgain(){
//Reseting values in case of Game Over
bet10_button.style.visibility="visible";
bet20_button.style.visibility="visible";
bet50_button.style.visibility="visible";
play_button.style.visibility="hidden";
cards = shuffleDeck(createDeck());
dealerCards = [];
playerCards = [];
dealerScore = 0;
playerScore = 0;
gameEnds = false;
winner = false;
tie = false;
//Asign $100 again
totalMoney = 100;
betMade_p.style.visibility ="hidden";
totalMoney_span.innerHTML = totalMoney;
playerScore_span.innerHTML = playerScore;
dealerScore_span.innerHTML = "?";
playerCards_div.innerHTML = playerCards;
dealerCards_div.innerHTML = dealerCards;
cardImagePlayer_span.innerHTML = "";
cardImageDealer_span.innerHTML = "";
winner_div.innerHTML = "";
loser_div.innerHTML = "";
tie_div.innerHTML = "";
}
function showCard(card){
//Showing poker images for player
let pokerImage = document.createElement("img");
playerCards.forEach(function(card){
pokerImage.style = 'width: 140px; height:200px; margin:20px'
pokerImage.src = `images/PNG-cards-1.3/${card}.png`;
cardImagePlayer_span.appendChild(pokerImage);
})
}
function showCardDealer(card){
//Showing poker images for dealer
let pokerImageDealer = document.createElement("img");
dealerCards.forEach(function(card, i){
if(i == 0){
//1st card faced down
pokerImageDealer.style = 'width: 140px; height:200px; margin:20px'
pokerImageDealer.src = 'images/reverse.png';
cardImageDealer_span.appendChild(pokerImageDealer);
}
else{
pokerImageDealer.style = 'width: 140px; height:200px; margin:20px'
pokerImageDealer.src = `images/PNG-cards-1.3/${card}.png`;
cardImageDealer_span.appendChild(pokerImageDealer);
}
})
}
function cardsFaceUP(card){
//Revealing all dealer cards
cardImageDealer_span.innerHTML ="";
let pokerImageDealer = document.createElement("img");
dealerCards.forEach(function(card){
let pokerImageDealer = document.createElement("img");
pokerImageDealer.style = 'width: 140px; height:200px; margin:20px'
pokerImageDealer.src = `images/PNG-cards-1.3/${card}.png`;
cardImageDealer_span.appendChild(pokerImageDealer);
})
}
function bet(betAmount){
//Double check there is a moment it stop summing up or down
if (gameEnds == true){
if(winner == true && tie == false){
totalMoney += betAmount;
}
else if(winner == false && tie == false){
totalMoney -= betAmount;
}
else if(winner == false && tie == true){
totalMoney = totalMoney;
}
}
totalMoney_span.innerHTML = totalMoney;
}
main()