-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
363 lines (317 loc) · 12.4 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
//global variables
let hasBlackJack = false;
let tieLoss = false;
let dealerRole = false;
let checker = false;
let roleChance = 0;
let cards = [];
let dealerCards = [];
let dealerSum = 0;
let wager = 0;
let playerSum = 0;
let isAlive = true;
let dealerMessage;
let message = "";
let messageEl = document.getElementById("message-el");
let sumEl = document.getElementById("sum-el");
let dealerTotalEl = document.getElementById("dealerTotal-el");
let playerChipsEl = document.getElementById("playerChips-el");
let wagerMessage = document.getElementById("wagerMessage-el");
let stayMessage = document.getElementById("stayMessage-el");
let cardImage = document.getElementById("img1-el");
let dealerImage = document.getElementById("img2-el");
let moneyImage = document.getElementById("moneyImage-el");
let jack = false;
let queen = false;
let king = false;
let player = {
money: 1000
};
document.getElementById("hit-btn").disabled = true;
document.getElementById("stay-btn").disabled = true;
// draws a card
function getRandomCard() {
let randomNumber = Math.floor(Math.random() * 13) + 1;
// logic to turn face cards to 10 and ace to 11 while keeping same picture
if (randomNumber === 10) {
return 10;
} else if (randomNumber === 11) {
jack = true;
return 10;
} else if (randomNumber === 12) {
queen = true;
return 10;
} else if (randomNumber === 13) {
king = true;
return 10;
} else if (randomNumber === 1) {
return 11;
} else {
return randomNumber;
}
}
// Starts a new game
function startNewGame() {
// resetting face pics
jack = false;
queen = false;
king = false;
//generating player cards
let firstPlayerCard = getRandomCard();
let secondPlayerCard = getRandomCard();
cards = [firstPlayerCard, secondPlayerCard];
//generating dealers cards
let firstDealerCard = getRandomCard();
let secondDealerCard = getRandomCard();
dealerCards = [firstDealerCard, secondDealerCard];
//getting the sums
playerSum = firstPlayerCard + secondPlayerCard;
dealerSum = firstDealerCard + secondDealerCard;
//re-enabling the buttons
document.getElementById("hit-btn").disabled = false;
document.getElementById("stay-btn").disabled = false;
//clears the cards
clearCards();
//run render
renderRole();
stayMessage.innerHTML = "";
messageEl.innerHTML = "Do you want to hit or stay?";
//displaying heading
let = heading1 = document.getElementById("playerHeading-el");
let heading2 = document.getElementById("dealerHeading-el");
heading1.innerHTML = "Your Cards";
heading2.innerHTML = "Dealer Cards";
renderMoney();
}
function renderRole() {
//displaying sums
sumEl.textContent = "Player Sum: " + playerSum;
dealerTotalEl.textContent = "Dealer Sum: " + dealerSum;
//checking player total
if (playerSum > 21) {
messageEl.textContent = "BUST";
player.money = player.money - wager;
checker = true;
document.getElementById("hit-btn").disabled = true;
document.getElementById("stay-btn").disabled = true;
isAlive = false;
} else if (playerSum == 21) {
messageEl.textContent = "BLACKJACK!";
player.money = player.money + (wager * 2);
checker = true;
document.getElementById("hit-btn").disabled = true;
document.getElementById("stay-btn").disabled = true;
hasBlackJack = true;
} else if (dealerSum > 21) {
messageEl.textContent = "Dealer busted. You win!";
player.money = player.money + (wager * 1.5);
checker = true;
document.getElementById("hit-btn").disabled = true;
document.getElementById("stay-btn").disabled = true;
} else if (dealerSum == 21) {
messageEl.textContent = "Dealer has BlackJack! You lose";
player.money = player.money - wager;
checker = true;
document.getElementById("hit-btn").disabled = true;
document.getElementById("stay-btn").disabled = true;
isAlive = false;
} else if (dealerSum > 21 && playerSum > 21) {
messageEl.textContent = "You both lose!"
player.money = player.money - (wager / 2);
checker = true;
document.getElementById("hit-btn").disabled = true;
document.getElementById("stay-btn").disabled = true;
tieLoss = true;
} else if (dealerSum == 21 & playerSum == 21) {
messageEl.textContent = "Both have BlackJack!"
player.money = player.money + (wager * 1.5);
checker = true;
document.getElementById("hit-btn").disabled = true;
document.getElementById("stay-btn").disabled = true;
} else {
isAlive = true;
hasBlackJack = false;
checker = false;
}
//Showing money owned
playerChipsEl.textContent = "$" + player.money;
// Showing players cards img
for (i = 0; i < cards.length; i++) {
if (cards[i] === 11) {
cardImage.innerHTML += '<img class="card1" src="img/card14.png" alt="test">';
} else if (cards[i] === 2) {
cardImage.innerHTML += '<img class="card1" src="img/card2.png" alt="test">';
} else if (cards[i] === 3) {
cardImage.innerHTML += '<img class="card1" src="img/card3.png" alt="test">';
} else if (cards[i] === 4) {
cardImage.innerHTML += '<img class="card1" src="img/card4.png" alt="test">';
} else if (cards[i] === 5) {
cardImage.innerHTML += '<img class="card1" src="img/card5.png" alt="test">';
} else if (cards[i] === 6) {
cardImage.innerHTML += '<img class="card1" src="img/card6.png" alt="test">';
} else if (cards[i] === 7) {
cardImage.innerHTML += '<img class="card1" src="img/card7.png" alt="test">';
} else if (cards[i] === 8) {
cardImage.innerHTML += '<img class="card1" src="img/card8.png" alt="test">';
} else if (cards[i] === 9) {
cardImage.innerHTML += '<img class="card1" src="img/card9.png" alt="test">';
} else if (cards[i] === 10 && jack === true) {
cardImage.innerHTML += '<img class="card1" src="img/card11.png" alt="test">';
} else if (cards[i] === 10 && queen === true) {
cardImage.innerHTML += '<img class="card1" src="img/card12.png" alt="test">';
} else if (cards[i] === 10 && king === true) {
cardImage.innerHTML += '<img class="card1" src="img/card13.png" alt="test">';
} else {
cardImage.innerHTML += '<img class="card1" src="img/card10.png" alt="test">';
}
}
// Showing dealer cards img
for (i = 0; i < dealerCards.length; i++) {
if (dealerCards[i] === 11) {
dealerImage.innerHTML += '<img class="card1" src="img/card14.png" alt="test">';
} else if (dealerCards[i] === 2) {
dealerImage.innerHTML += '<img class="card1" src="img/card2.png" alt="test">';
} else if (dealerCards[i] === 3) {
dealerImage.innerHTML += '<img class="card1" src="img/card3.png" alt="test">';
} else if (dealerCards[i] === 4) {
dealerImage.innerHTML += '<img class="card1" src="img/card4.png" alt="test">';
} else if (dealerCards[i] === 5) {
dealerImage.innerHTML += '<img class="card1" src="img/card5.png" alt="test">';
} else if (dealerCards[i] === 6) {
dealerImage.innerHTML += '<img class="card1" src="img/card6.png" alt="test">';
} else if (dealerCards[i] === 7) {
dealerImage.innerHTML += '<img class="card1" src="img/card7.png" alt="test">';
} else if (dealerCards[i] === 8) {
dealerImage.innerHTML += '<img class="card1" src="img/card8.png" alt="test">';
} else if (dealerCards[i] === 9) {
dealerImage.innerHTML += '<img class="card1" src="img/card9.png" alt="test">';
} else if (dealerCards[i] === 10 && jack === true) {
dealerImage.innerHTML += '<img class="card1" src="img/card11.png" alt="test">';
} else if (dealerCards[i] === 10 && queen === true) {
dealerImage.innerHTML += '<img class="card1" src="img/card12.png" alt="test">';
} else if (dealerCards[i] === 10 && king === true) {
dealerImage.innerHTML += '<img class="card1" src="img/card13.png" alt="test">';
} else {
dealerImage.innerHTML += '<img class="card1" src="img/card10.png" alt="test">';
}
}
renderMoney();
}
// gives player another card
function hit() {
if (isAlive === true && hasBlackJack === false) {
let playerNewCard = getRandomCard()
cards.push(playerNewCard);
playerSum += playerNewCard;
}
if (dealerSum < 17) {
let dealerNewCard = getRandomCard();
dealerCards.push(dealerNewCard);
dealerSum += dealerNewCard;
stayMessage.innerHTML = "The dealer draws a card as well.";
disabler();
} else if (dealerSum >= 17) {
roleChance = Math.floor(Math.random() * 2) + 1;
if (roleChance == 1) {
let roleCard = getRandomCard();
dealerCards.push(roleCard);
dealerSum += roleCard;
dealerRole = true;
stayMessage.innerHTML = "The dealer chose to draw another card.";
disabler();
} else {
stayMessage.innerHTML = "The dealer chose to stay."
disabler();
}
}
//clears cards
cardImage.innerHTML = "";
dealerImage.innerHTML = "";
renderRole();
noWinner();
}
// stay function
function stay() {
messageEl.textContent = "You chose to stay";
if (dealerSum < 17) {
let newCard = getRandomCard();
dealerCards.push(newCard);
dealerSum += newCard;
stayMessage.innerHTML = "The dealer drew another card.";
disabler();
renderRole();
} else {
let roler = Math.floor(Math.random() * 3) + 1;
if (roler === 1) {
let newCard = getRandomCard();
dealerCards.push(newCard);
dealerSum += newCard;
disabler();
renderRole();
stayMessage.innerHTML = "The dealer chose to draw a card.";
} else {
stayMessage.innerHTML = "The dealer chose to stay";
disabler();
renderRole();
}
}
noWinner();
clearCards();
renderRole();
}
//bet input
function inputBet() {
wager = document.getElementById("test").value;
if (wager == "") {
wagerMessage.innerHTML = "You have to enter a number.";
console.log(wager);
} else if (wager >= 0) {
wagerMessage.innerHTML = "Your wager is set to $" + wager;
} else {
wagerMessage.innerHTML = "ERROR";
}
}
//clearing card images
function clearCards() {
//clearing player cards
cardImage.innerHTML = "";
//clearing dealer cards
dealerImage.innerHTML = "";
}
// disables hit me and stay
function disabler() {
document.getElementById("hit-btn").disabled = true;
document.getElementById("stay-btn").disabled = true;
}
//under 21
function noWinner() {
//who's closer?
if (playerSum > dealerSum && playerSum < 21 && checker === false) {
stayMessage.innerHTML += "</br>You were closer to 21. You win!";
messageEl.innerHTML = "";
player.money = player.money + (wager * 1.5);
disabler();
} else if (dealerSum > playerSum && playerSum < 21 && checker === false) {
stayMessage.innerHTML += "</br>The dealer was closer to 21. You lose!";
messageEl.innerHTML = "";
player.money = player.money - wager;
disabler();
} else if (playerSum === dealerSum && checker === false) {
stayMessage.innerHTML += "</br>It was a tie!";
messageEl.innerHTML = "";
disabler();
}
renderMoney();
}
//render money
function renderMoney() {
if (player.money >= 1 && player.money < 1000) {
moneyImage.innerHTML = '<img class="moneyIm" src="img/100s.png" alt="yourMoney">';
} else if (player.money >= 1000 && player.money < 10000) {
moneyImage.innerHTML = '<img class="moneyIm" src="img/thousand.png" alt="yourMoney">';
} else if (player.money >= 10000) {
moneyImage.innerHTML = '<img class="moneyIm" src="img/tenthousands.png" alt="yourMoney">';
} else if (player.money <= 0) {
moneyImage.innerHTML = '<img class="moneyIm" src="img/wat2.png" alt="yourMoney">';
}
}