forked from langfalvidavid/Blackjack-game-hobby-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
443 lines (409 loc) · 15.7 KB
/
script.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
const cardsDP = document.getElementById("cards"); //div that cards are generated into
const sumDP = document.getElementById("sum"); //Sum of cards
const messageDP = document.getElementById("message"); //Message that is visible
const balance = document.getElementById("balance"); //Player's balance
const bet = document.getElementById("bet"); //Bet amount
const errorMsg = document.getElementById("error-msg"); //Error message
const dealerSumDP = document.getElementById("dealerSum"); // dealer's sum display
const newCardBtn = document.getElementById("new-card-btn"); //Button that randomly generates a new card
const restartGameBtn = document.getElementById("restart-game-btn"); //Button that restarts game
const stopBtn = document.getElementById("stop-btn"); //Button that stops player action / starts dealer action
const refillBalanceBtn = document.getElementById("refill-balance-btn"); //Button to refill balance if its 0
const betAllIn = document.getElementById("bet-all-in");
const bet50 = document.getElementById("bet-50");
const bet100 = document.getElementById("bet-100");
const bet200 = document.getElementById("bet-200");
const cards = [
{ image: "2_of_clubs.png", value: 2 },
{ image: "3_of_clubs.png", value: 3 },
{ image: "4_of_clubs.png", value: 4 },
{ image: "5_of_clubs.png", value: 5 },
{ image: "6_of_clubs.png", value: 6 },
{ image: "7_of_clubs.png", value: 7 },
{ image: "8_of_clubs.png", value: 8 },
{ image: "9_of_clubs.png", value: 9 },
{ image: "2_of_diamonds.png", value: 2 },
{ image: "3_of_diamonds.png", value: 3 },
{ image: "4_of_diamonds.png", value: 4 },
{ image: "5_of_diamonds.png", value: 5 },
{ image: "6_of_diamonds.png", value: 6 },
{ image: "7_of_diamonds.png", value: 7 },
{ image: "8_of_diamonds.png", value: 8 },
{ image: "9_of_diamonds.png", value: 9 },
{ image: "2_of_hearts.png", value: 2 },
{ image: "3_of_hearts.png", value: 3 },
{ image: "4_of_hearts.png", value: 4 },
{ image: "5_of_hearts.png", value: 5 },
{ image: "6_of_hearts.png", value: 6 },
{ image: "7_of_hearts.png", value: 7 },
{ image: "8_of_hearts.png", value: 8 },
{ image: "9_of_hearts.png", value: 9 },
{ image: "2_of_spades.png", value: 2 },
{ image: "3_of_spades.png", value: 3 },
{ image: "4_of_spades.png", value: 4 },
{ image: "5_of_spades.png", value: 5 },
{ image: "6_of_spades.png", value: 6 },
{ image: "7_of_spades.png", value: 7 },
{ image: "8_of_spades.png", value: 8 },
{ image: "9_of_spades.png", value: 9 },
{ image: "10_of_spades.png", value: 10 },
{ image: "10_of_hearts.png", value: 10 },
{ image: "10_of_diamonds.png", value: 10 },
{ image: "10_of_clubs.png", value: 10 },
{ image: "ace_of_clubs.png", value: 11 },
{ image: "ace_of_diamonds.png", value: 11 },
{ image: "ace_of_hearts.png", value: 11 },
{ image: "ace_of_spades.png", value: 11 },
{ image: "jack_of_clubs2.png", value: 10 },
{ image: "jack_of_diamonds2.png", value: 10 },
{ image: "jack_of_hearts2.png", value: 10 },
{ image: "jack_of_spades2.png", value: 10 },
{ image: "king_of_clubs2.png", value: 10 },
{ image: "king_of_diamonds2.png", value: 10 },
{ image: "king_of_hearts2.png", value: 10 },
{ image: "king_of_spades2.png", value: 10 },
{ image: "queen_of_clubs2.png", value: 10 },
{ image: "queen_of_diamonds2.png", value: 10 },
{ image: "queen_of_hearts2.png", value: 10 },
{ image: "queen_of_spades2.png", value: 10 },
];
// ---- Preloading images ----
let preloaded = 0;
function preLoader(e) {
for (let i = 0; i < cards.length; i++) {
let tempImage = new Image();
tempImage.addEventListener("load", progress, true);
tempImage.src = cards[i].image;
}
}
//delay
function delay(time) {
return new Promise((resolve) => {
setTimeout(() => {
console.log('timeout');
resolve();
}, time)
});
}
//random card generator
function randomCard() {
return cards[Math.floor(Math.random() * cards.length)];
}
let newCard = randomCard(); //variable for the generated card (i.e. "2_of_clubs-min.jpg")
// ---- New card button ----
let sumVal = 0; //sum of cards
let sumValWithAce = 0;
let aceCounter = 0;
let hasAce = false; //if player pulls ace, we have to display 2 sums
newCardBtn.addEventListener("click", function () {
if (totalBet != 0) {
if (inGame && !stopPressed) {
let value = newCard.value; //value of generated card
//generating new elements for each card
if (value === 11) {
hasAce = true;
aceCounter++;
}
cardsDP.insertAdjacentHTML(
"beforeend",
`<img src="cards/${newCard.image}" id="img" alt="Player's card with value ${value}">`
);
if (hasAce) {
sumVal += value;
sumValWithAce = sumVal - aceCounter * 10;
sumDP.textContent = `Sum: ${sumVal} / ${sumValWithAce}`; //displaying current sum
} else {
sumVal += value;
sumDP.textContent = `Sum: ${sumVal}`; //displaying current sum
}
console.log(value);
console.log("aces: " + aceCounter);
gameLogic(); // decides whether player has lost or still in game
newCard = randomCard(); // pulling new card from deck
betAllIn.style.display = "none";
bet50.style.display = "none";
bet100.style.display = "none";
bet200.style.display = "none";
}
} else {
messageDP.textContent = "Place your bets first!";
messageDP.style.color = "red";
setTimeout(() => {
messageDP.textContent = message[0];
messageDP.style.color = "#fff";
}, 2000);
}
});
let inGame = true; //if false, game is over
let message = [
"Do you want to draw a new card? 😉",
"Congrats! You've got blackjack! 🥳",
"Sorry, you're out of the game! 😭",
"Let's see the dealer's cards! 🤞",
"It's a draw! The dealer wins. 😤",
"Dealer is out! You win! 🥳",
"Sorry, you lost! 😥",
"Congrats! You won! 😃",
"You don't have enough balance!",
];
// ---- Game logic ----
function gameLogic() {
if (sumVal <= 21) {
messageDP.textContent = message[0];
} else if (sumVal === 21) {
messageDP.textContent = message[1];
} else if (!hasAce && sumVal > 21) {
messageDP.textContent = message[2];
inGame = false;
playerWon = false;
balanceSystem();
restartDisplayReverse();
} else if (hasAce && sumValWithAce > 21) {
messageDP.textContent = message[2];
inGame = false;
playerWon = false;
balanceSystem();
restartDisplayReverse();
} else if (!dealerAce && dealerSum > 21) {
messageDP.textContent = message[5];
inGame = false;
playerWon = false;
balanceSystem();
restartDisplayReverse();
} else if (dealerAce && dealerSumWithAce > 21) {
messageDP.textContent = message[5];
inGame = false;
playerWon = false;
balanceSystem();
restartDisplayReverse();
}
}
// ---- Restart game button ----
function restartDisplay() {
betAllIn.style.display = "block";
bet50.style.display = "block";
bet100.style.display = "block";
bet200.style.display = "block";
newCardBtn.style.display = "block";
stopBtn.style.display = "block";
restartGameBtn.style.display = "none";
refillBalanceBtn.style.display = "none";
}
function restartDisplayReverse() {
betAllIn.style.display = "none";
bet50.style.display = "none";
bet100.style.display = "none";
bet200.style.display = "none";
newCardBtn.style.display = "none";
stopBtn.style.display = "none";
restartGameBtn.style.display = "block";
}
// ---- Restart game button ----
restartGameBtn.addEventListener("click", function () {
messageDP.textContent = "Draw a card to start the game! 😀";
sumDP.textContent = "Sum: 0";
sumVal = 0;
dealerSum = 0;
//counting how many elements does the cards div have, then deleting them
let playerChildren = cardsDP.childElementCount;
for (let i = 0; i < playerChildren; i++) {
let del = document.getElementById("img");
del.remove();
}
bet.textContent = "Bet: 0";
totalBet = 0;
inGame = true;
stopPressed = false;
playerWon = false;
restartDisplay();
hasAce = false;
dealerAce = false;
aceCounter = 0;
dealerAceCounter = 0;
if (totalBalance === 0) {
restartDisplayReverse();
restartGameBtn.style.display = "none";
refillBalanceBtn.style.display = "block";
}
});
// ---- Refill balance button ----
refillBalanceBtn.addEventListener("click", function () {
totalBalance += 1000;
balance.textContent = "Balance: " + totalBalance;
restartDisplay();
});
// ---- Stop button ----
let stopPressed = false; //Basically the switch between gamestates
stopBtn.addEventListener("click", function () {
if (
(sumVal <= 21 || (hasAce && sumValWithAce <= 21)) &&
stopPressed === false &&
sumVal != 0
) {
messageDP.textContent = message[3];
stopPressed = true;
const separate = document.createElement("div");
separate.id = "img";
separate.style.margin = "0 1em";
separate.style.display = "inline-block";
separate.style.height = "80%";
separate.style.width = "5px";
separate.style.color = "#fff";
separate.style.backgroundColor = "#fff";
separate.style.animation = "cardAnimation 0.25s";
document.getElementById("cards").appendChild(separate);
delay(500).then(() => dealer());
}
});
// ---- Dealer system ----
let dealerAceCounter = 0;
let dealerSum = 0;
let dealerAce = false;
let dealerSumWithAce = 0;
let playerWon;
async function dealer() {
if (stopPressed) {
do {
let value = newCard.value; //value of generated card
if (value === 11) {
dealerAce = true;
dealerAceCounter++;
}
//generating new elements for each card
cardsDP.insertAdjacentHTML(
"beforeend",
`<img src="cards/${newCard.image}" id="img" alt="Player's card with value ${value}">`
);
dealerSum += value;
dealerSumWithAce = dealerSum - dealerAceCounter * 10;
//Sum display logic if player or dealer has ace or not
if (!hasAce && !dealerAce) {
sumDP.textContent = `Sum: ${sumVal} | ${dealerSum}`; //displaying current sum
} else if (dealerAce && dealerSum <= 21) {
sumDP.textContent = `Sum: ${sumVal} | ${dealerSum} / ${dealerSumWithAce}`;
} else if (dealerAce && dealerSum > 21) {
sumDP.textContent = `Sum: ${sumVal} | ${dealerSumWithAce}`;
} else if (!dealerAce && hasAce && sumVal <= 21) {
sumDP.textContent = `Sum: ${sumVal} / ${sumValWithAce} | ${dealerSum}`;
} else if (!dealerAce && hasAce && sumVal > 21) {
sumDP.textContent = `Sum: ${sumValWithAce} | ${dealerSum}`;
} else if (dealerAce && sumVal < 21 && dealerSum < 21) {
sumDP.textContent = `Sum: ${sumVal} / ${sumValWithAce} | ${dealerSum} / ${dealerSumWithAce}`;
} else if (dealerAce && sumVal > 21 && dealerSum < 21) {
sumDP.textContent = `Sum: ${sumValWithAce} | ${dealerSum} / ${dealerSumWithAce}`;
} else if (dealerAce && sumVal < 21 && dealerSum > 21) {
sumDP.textContent = `Sum: ${sumValWithAce} | ${dealerSumWithAce}`;
} else if (dealerAce && sumVal > 21 && dealerSum > 21) {
sumDP.textContent = `Sum: ${sumValWithAce} | ${dealerSumWithAce}`;
} else if (dealerAce && hasAce && sumVal > 21 && dealerSum > 21) {
sumDP.textContent = `Sum: ${sumValWithAce} | ${dealerSumWithAce}`;
} else if (dealerAce && hasAce && sumVal > 21 && dealerSum < 21) {
sumDP.textContent = `Sum: ${sumValWithAce} | ${dealerSum}`;
} else if (dealerAce && hasAce && sumVal < 21 && dealerSum > 21) {
sumDP.textContent = `Sum: ${sumVal} | ${dealerSumWithAce}`;
} else if (dealerAce && hasAce && sumVal < 21 && dealerSum < 21) {
sumDP.textContent = `Sum: ${sumVal} | ${dealerSum}`;
}
gameLogic(); // decides whether player has lost or still in game
newCard = randomCard(); // pulling new card from deck
await delay(500);
} while (
((dealerSum < sumVal || sumValWithAce > dealerSum) && dealerSum < 21) ||
(dealerAce && dealerSumWithAce < sumVal) ||
dealerSumWithAce < sumValWithAce
);
if (
(dealerSum > 21 && !dealerAce) ||
(dealerAce && dealerSumWithAce > 21)
) {
messageDP.textContent = message[5];
playerWon = true;
} else if (
dealerSum > sumVal ||
(hasAce && dealerSum > sumValWithAce) ||
(dealerAce && (dealerSumWithAce > sumVal || dealerSum > sumVal))
) {
messageDP.textContent = message[6];
playerwon = false;
} else if (
sumVal > dealerSum ||
(hasAce && sumValWithAce > dealerSum) ||
(dealerAce && (sumVal > dealerSumWithAce || sumVal > dealerSum))
) {
messageDP.textContent = message[7];
playerWon = true;
} else {
messageDP.textContent = message[4];
playerWon = false;
}
balanceSystem();
newCardBtn.style.display = "none";
stopBtn.style.display = "none";
restartGameBtn.style.display = "block";
}
}
// ---- Bet increase buttons ----
let totalBet = 0;
betAllIn.addEventListener("click", function () {
if (totalBalance != 0) {
totalBet = totalBalance;
bet.textContent = "Bet: " + totalBet;
} else {
messageDP.textContent = message[8];
messageDP.style.color = "red";
setTimeout(() => {
messageDP.textContent = message[0];
messageDP.style.color = "#fff";
}, 2000);
}
});
bet50.addEventListener("click", function () {
if (totalBet + 50 <= totalBalance) {
totalBet += 50;
bet.textContent = "Bet: " + totalBet;
} else {
messageDP.textContent = message[8];
messageDP.style.color = "red";
setTimeout(() => {
messageDP.textContent = message[0];
messageDP.style.color = "#fff";
}, 2000);
}
});
bet100.addEventListener("click", function () {
if (totalBet + 100 <= totalBalance) {
totalBet += 100;
bet.textContent = "Bet: " + totalBet;
} else {
messageDP.textContent = message[8];
messageDP.style.color = "red";
setTimeout(() => {
messageDP.textContent = message[0];
messageDP.style.color = "#fff";
}, 2000);
}
});
bet200.addEventListener("click", function () {
if (totalBet + 200 <= totalBalance) {
totalBet += 200;
bet.textContent = "Bet: " + totalBet;
} else {
messageDP.textContent = message[8];
messageDP.style.color = "red";
setTimeout(() => {
messageDP.textContent = message[0];
messageDP.style.color = "#fff";
}, 2000);
}
});
//---- Balance system ----
let totalBalance = 1000;
function balanceSystem() {
if (playerWon === true) {
totalBalance += totalBet;
balance.textContent = "Balance: " + totalBalance;
} else {
totalBalance -= totalBet;
balance.textContent = "Balance: " + totalBalance;
}
}