-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
342 lines (306 loc) · 10.8 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
const button1 = document.querySelector("#button1");
const button2 = document.querySelector("#button2");
const button3 = document.querySelector("#button3");
const textright = document.querySelector("#textright");
const textleft = document.querySelector("#textleft");
const balance = document.querySelector("#balance");
const dealercard = document.querySelector("#dealercard");
const yourcard = document.querySelector("#yourcard");
let drawCardSound = new Audio('Sounds/drawcard.mp3');
let dealerWinSound = new Audio('Sounds/dealerwin.wav');
let playerWinSound = new Audio('Sounds/playerwin.mp3');
let gameDrawnSound = new Audio('Sounds/gamedrawsound.wav');
drawCardSound.volume = 0.1;
dealerWinSound.volume = 0.1;
playerWinSound.volume = 0.1;
gameDrawnSound.volume = 0.1;
// Current game money and the live state
let money = balance.innerText;
let gameLive = false;
const deck = [
{ suit: 'Hearts', type: '2', value: 2 },
{ suit: 'Hearts', type: '3', value: 3 },
{ suit: 'Hearts', type: '4', value: 4 },
{ suit: 'Hearts', type: '5', value: 5 },
{ suit: 'Hearts', type: '6', value: 6 },
{ suit: 'Hearts', type: '7', value: 7 },
{ suit: 'Hearts', type: '8', value: 8 },
{ suit: 'Hearts', type: '9', value: 9 },
{ suit: 'Hearts', type: '10', value: 10 },
{ suit: 'Hearts', type: 'J', value: 10 },
{ suit: 'Hearts', type: 'Q', value: 10 },
{ suit: 'Hearts', type: 'K', value: 10 },
{ suit: 'Hearts', type: 'A', value: 11 },
{ suit: 'Diamonds', type: '2', value: 2 },
{ suit: 'Diamonds', type: '3', value: 3 },
{ suit: 'Diamonds', type: '4', value: 4 },
{ suit: 'Diamonds', type: '5', value: 5 },
{ suit: 'Diamonds', type: '6', value: 6 },
{ suit: 'Diamonds', type: '7', value: 7 },
{ suit: 'Diamonds', type: '8', value: 8 },
{ suit: 'Diamonds', type: '9', value: 9 },
{ suit: 'Diamonds', type: '10', value: 10 },
{ suit: 'Diamonds', type: 'J', value: 10 },
{ suit: 'Diamonds', type: 'Q', value: 10 },
{ suit: 'Diamonds', type: 'K', value: 10 },
{ suit: 'Diamonds', type: 'A', value: 11 },
{ suit: 'Clubs', type: '2', value: 2 },
{ suit: 'Clubs', type: '3', value: 3 },
{ suit: 'Clubs', type: '4', value: 4 },
{ suit: 'Clubs', type: '5', value: 5 },
{ suit: 'Clubs', type: '6', value: 6 },
{ suit: 'Clubs', type: '7', value: 7 },
{ suit: 'Clubs', type: '8', value: 8 },
{ suit: 'Clubs', type: '9', value: 9 },
{ suit: 'Clubs', type: '10', value: 10 },
{ suit: 'Clubs', type: 'J', value: 10 },
{ suit: 'Clubs', type: 'Q', value: 10 },
{ suit: 'Clubs', type: 'K', value: 10 },
{ suit: 'Clubs', type: 'A', value: 11 },
{ suit: 'Spades', type: '2', value: 2 },
{ suit: 'Spades', type: '3', value: 3 },
{ suit: 'Spades', type: '4', value: 4 },
{ suit: 'Spades', type: '5', value: 5 },
{ suit: 'Spades', type: '6', value: 6 },
{ suit: 'Spades', type: '7', value: 7 },
{ suit: 'Spades', type: '8', value: 8 },
{ suit: 'Spades', type: '9', value: 9 },
{ suit: 'Spades', type: '10', value: 10 },
{ suit: 'Spades', type: 'J', value: 10 },
{ suit: 'Spades', type: 'Q', value: 10 },
{ suit: 'Spades', type: 'K', value: 10 },
{ suit: 'Spades', type: 'A', value: 11 }
];
let userCardCount = 0;
let userSum = 0;
let dealerSum = 0;
let winner;
function drawCard() {
drawCardSound.play();
const index = Math.floor(Math.random() * deck.length);
const card = deck[index];
// If the card drawn is an Ace and it's the first Ace
if (card.type === 'A' && userCardCount <= 1) {
return {...card, value: 11};
}
// If the card drawn is an Ace but it's not the first Ace
else if (card.type === 'A' && userCardCount > 1) {
return {...card, value: 1};
}
return card;
}
//separate draw functions for user and dealer
function drawUser() {
const card = drawCard(); // select a random card
const suitU = document.querySelector("#suitU");
const typeU = document.querySelector("#typeU");
const valueU = document.querySelector("#valueU");
userCardCount++; // Increment the user card count
textleft.style.color = "aliceblue";
suitU.innerText = card.suit;
typeU.innerText = card.type;
userSum += card.value; // Adding this line to increase userSum with each draw
valueU.innerText = userSum; // Displaying the sum instead of the last drawn card value
if (userSum < 21) {
drawDealer();
}
else if (userSum === 21) {
checkWinnerAndFinishGame();
}
else if (userSum > 21) {
checkWinnerAndFinishGame();
}
}
function drawDealer() {if (dealerSum < 17 || dealerSum < userSum) {
const card = drawCard(); // select random card
const suitD = document.querySelector("#suitD");
const typeD = document.querySelector("#typeD");
const valueD = document.querySelector("#valueD");
// Display dealer card info
suitD.innerText = card.suit;
typeD.innerText = card.type;
dealerSum += card.value; // Adding this line to increase dealerSum with each draw
valueD.innerText = dealerSum; // Displaying the sum instead of the last drawn card value
{if (dealerSum === 21) {
winner = "Dealer";
money = money;
textleft.innerText = "Dealer Wins";
dealerWinSound.play();
textleft.style.color = "red";
}
else if (dealerSum > 21) {
winner= "Player";
money += 2 * bet;
textleft.innerText = 'You Win';
playerWinSound.play();
textleft.style.color = "rgb(3, 255, 3)";
}}
}
else {
checkWinnerAndFinishGame();
}}
// Bet amounts functions
let bet
function bet100() {
if (money >= 100 && !gameLive) {
money -= 100;
balance.innerText = money;
update(locations[1]);
gameLive = true;
showDealerCard();
drawUser();
bet = 100;
} else if (gameLive===true) {
alert("Game ongoing, you cannot bet now");
} else {
alert("You do not have 100 cash");
}
}
function bet500() {
if (money >= 500 && !gameLive) {
money -= 500;
balance.innerText = money;
update(locations[1]);
gameLive = true;
showDealerCard();
drawUser();
bet = 500;
} else if (gameLive===true) {
alert("Game ongoing, you cannot bet now");
} else {
alert("You do not have 500 cash");
}
}
function bet1000() {
if (money >= 1000 && !gameLive) {
money -= 1000;
balance.innerText = money;
update(locations[1]);
gameLive = true;
showDealerCard();
drawUser();
bet = 1000;
} else if (gameLive===true) {
alert("Game ongoing, you cannot bet now");
} else {
alert("You do not have 1000 cash");
}
}
// Function to check the winner
function checkWinnerAndFinishGame() {
gameLive = false;
update(locations[0]);
// Check if either player or dealer has busted or has reached blackjack
{if (dealerSum > 21) {
winner = 'Player';
}
else if (userSum > 21) {
winner = 'Dealer';
}
else if (userSum === 21) {
winner = 'Player';
}
else if (dealerSum === 21) {
winner = 'Dealer';
}
else if (userSum === dealerSum) {
winner = 'Drawn';
}
else {
// Determine who is closest to 21 and assign the winner
winner = userSum > dealerSum ? 'Player' : 'Dealer';
}}
// Updating balance and logs according to the winner
{if (winner === 'Player') {
money += 2 * bet;
textleft.innerText = 'You Win';
playerWinSound.play();
textleft.style.color = "rgb(3, 255, 3)";
}
else if (winner === 'Dealer') {
money = money;
textleft.innerText = "Dealer Wins";
dealerWinSound.play();
textleft.style.color = "red";
}
else if (winner === 'Drawn') {
money += bet;
textleft.innerText = 'Game drawn';
gameDrawnSound.play();
textleft.style.color = "#ffbd08";
}}
balance.innerText = money;
userCardCount = 0; // Resetting the user card count
userSum = 0; // Resetting user score
dealerSum = 0; // Resetting dealer score
}
// Player action functions
function hold() {if (gameLive && userCardCount >= 2) {
if (dealerSum < userSum) {
drawDealer();
checkWinnerAndFinishGame();
}
else {
checkWinnerAndFinishGame();
}
}
else {
alert('You must draw at least two cards before holding.');
}
}
function double() {
money -= bet;
bet = 2 * bet;
balance.innerText = money;
textright.innerText = "Doubled Bet";
drawUser ();
}
// unhiding cards (actually its usercard i made a mistake then didnt risk changing much again)
function showDealerCard() {
if (gameLive) {
setTimeout(() => {
yourcard.style.display = "block";
textleft.style.animationName = '';
textleft.style.borderRight = 'none';
setTimeout(() => {
dealercard.style.display = "block";
}, 1000);
}, 1000);
}
else {
dealercard.style.display = "none";
yourcard.style.display = "none";
}
}
const locations = [
{
name: "Bet Menu",
"button text": ["$100", "$500", "$1000"],
"button functions": [bet100, bet500, bet1000],
textleft: "Choose your bet",
textright: "Choose New Bet",
},
{
name: "Game Menu",
"button text": ["Draw", "Hold", "Double"],
"button functions": [drawUser, hold, double],
textleft: "Dealing...",
textright: "Choose",
}
];
function update(location) {
button1.innerText = location["button text"][0];
button2.innerText = location["button text"][1];
button3.innerText = location["button text"][2];
button1.onclick = location["button functions"][0];
button2.onclick = location["button functions"][1];
button3.onclick = location["button functions"][2];
textright.innerText = location.textright;
textleft.innerText = location.textleft;
}
// initial settings
update(locations[0]); // initial loc at bet menu
showDealerCard(); // initial hide game (since gamelive is set to false)
//setting welcome screen when first opened/reload
textleft.innerText = 'Welcome to BlackJack';
textright.innerText = 'Choose Bet';