This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
349 lines (298 loc) · 9.92 KB
/
main.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
var hmnScore = 0;
var hmnElement = document.getElementById("humanCount");
var cpuScore = 0;
var cpuElement = document.getElementById("cpuCount");
var hmnWins = 0;
var cpuWins = 0;
var hmnEleWins = document.getElementById("hmnWins");
var cpuEleWins = document.getElementById("cpuWins");
var statusElement = document.querySelector('#message');
var hmnGameEle = document.querySelector('#human-game');
var cpuGameEle = document.querySelector('#cpu-game');
hmnDrawBtn = document.getElementById("hmnDrawBtn");
hmnStandBtn = document.getElementById("hmnStandBtn");
let hmnStanding = false;
let cpuStanding = false;
var backgroundMusic = new Audio('./assets/audio/pazaak.mp3');
var drawSound = new Audio('./assets/audio/deal_card_slide.mp3');
class MainDeck {
constructor() {
this.deck = [];
this.createDeck();
this.shuffleDeck();
}
createDeck() {
for (var i = 1; i < 11; i++) {
for(var x = 1; x < 5; x++) {
let card = i;
this.deck.push(card);
}
}
}
shuffleDeck() {
for (let i = this.deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.deck[i], this.deck[j]] = [this.deck[j], this.deck[i]];
}
}
drawFromDeck(player) {
// play a sound when a card is being delt
drawSound.play();
let card = this.deck.pop();
showCard(player, card);
return card;
}
}
class SideDeck {
constructor() {
this.cardsInDeck = [];
this.createDeck();
}
createDeck() {
for (let i = 1; i <= 4; i++)
{
while(true) {
let randint = Math.floor(Math.random() * (Math.floor(7) - Math.ceil(-6)) + Math.ceil(-6));
if(randint != 0) {
this.cardsInDeck.push(randint);
break;
}
}
}
console.log(this.cardsInDeck);
}
drawFromDeck(index){
this.cardsInDeck = this.cardsInDeck.filter(item => item !== index);
return this.cardsInDeck[index];
}
}
// At beginning of each round create a new deck when we code the round/game logic. for now we just create it here
d = new MainDeck();
cpuSideDeck = new SideDeck();
humanSideDeck = new SideDeck();
showSideDeck(0);
showSideDeck(1);
function dealCard(player) {
switch(player) {
case 0: // when human
if (hmnScore > 20) {
stand();
} else {
hmnScore += d.drawFromDeck(0);
hmnElement.innerHTML = hmnScore;
}
break;
case 1: // when cpu
cpuScore += d.drawFromDeck(1);
cpuElement.innerHTML = cpuScore;
break;
}
}
/*
Assign an image to the numerical value of the card in the side
deck, then and add it to the HTML.
*/
function showSideDeck(player) {
switch(player) {
case 0:
for (i = 0; i < 4; i++) {
card = (humanSideDeck.cardsInDeck[i]);
let cardImage = document.createElement('img');
cardImage.src = `./assets/cards/B${card}.png`;
document.querySelector(`#hmnSideDeck${i}`).innerHTML = card;
document.querySelector(`#hmnSideDeck${i}`).appendChild(cardImage);
}
break;
case 1:
for (i = 0; i < 4; i++) {
card = (cpuSideDeck.cardsInDeck[i]);
let cardImage = document.createElement('img');
cardImage.src = `./assets/cards/back.png`;
document.querySelector(`#cpuSideDeck${i}`).innerHTML = card;
document.querySelector(`#cpuSideDeck${i}`).appendChild(cardImage);
}
break;
}
}
function playHumanSideCard(yourChoice) {
// calculate value of player score based on sideDeck card used
valueSideCard = parseInt(yourChoice.innerHTML);
hmnScore += valueSideCard;
hmnElement.innerHTML = hmnScore;
// place card in play area
let cardImage = document.createElement('img');
cardImage.src = `./assets/cards/B${valueSideCard}.png`;
hmnGameEle.appendChild(cardImage);
// núllstilla
var back = document.getElementById(yourChoice.id);
back.innerHTML = "";
// back.innerHTML= "<img src=./assets/cards/back.png>";
}
function playCpuSideCard(yourChoice) {
// calculate value of player score based on sideDeck card used
valueSideCard = parseInt(yourChoice.innerHTML);
cpuScore += valueSideCard;
cpuElement.innerHTML = cpuScore;
// place card in play area
let cardImage = document.createElement('img');
cardImage.src = `./assets/cards/B${valueSideCard}.png`;
cpuGameEle.appendChild(cardImage);
// núllstilla
var back = document.getElementById(yourChoice.id);
back.innerHTML= "<img src=./assets/cards/back.png>";
}
function showCard(player, card) {
let cardImage = document.createElement('img');
cardImage.src = `./assets/cards/G${card}.png`;
switch(player) {
case 0:
hmnGameEle.appendChild(cardImage);
break;
case 1:
cpuGameEle.appendChild(cardImage);
break;
}
}
function disableSideCard(yourChoice){
document.getElementById(yourChoice.id).style.pointerEvents = 'none';
}
function activateSideCard() {
for (i = 0; i < 4; i++) {
document.getElementById('hmnSideDeck'+i).style.pointerEvents = 'auto';
}
}
function endTurn() {
if (cpuStanding == false) {
dealCard(0);
dealCard(1);
} else if (cpuScore > 20) {
stand();
} else {
dealCard(0);
}
//if ((hmnScore <= 20 && hmnScore > cpuScore) || (hmnScore <=20 && hmnScore > cpuScore)) {
if (cpuScore >= 15 && cpuScore > hmnScore) {
cpuStanding = true;
console.log('cpuStanding' + cpuStanding);
} else if (cpuScore > 20) {
cpuStanding = true;
stand();
}
hmnDrawBtn.innerHTML = 'End turn';
// Play some nice ambient music
backgroundMusic.pause();
backgroundMusic.play();
}
function stand() {
hmnStanding = true; // human stands
var breakLoop = false; // this is needed because i cba figuring out why this doesn't work
/*
God-like AI. Stand on 15 and always try to draw or go over player score.
*/
if (cpuStanding == false) {
hmnDrawBtn.disabled = true;
hmnStandBtn.disabled = true;
do {
dealCard(1);
if (hmnStanding == true && hmnScore <= cpuScore) {
breakLoop = true;
}
} while ((cpuScore <= 15 && hmnScore <=20 && breakLoop == false) || (hmnScore <=20 && hmnScore > cpuScore && breakLoop == false));
}
cpuStanding = true;
console.log('cpuStanding' + cpuStanding);
delayResults();
roundWinner();
d = new MainDeck();
}
function delayResults() {
setTimeout(function() {
/*
hmnGameEle.innerHTML = '<h2 id="humanCount">0</h2>';
cpuGameEle.innerHTML = '<h2 id="cpuCount">0</h2>';
*/
let yourImages = hmnGameEle.querySelectorAll('img');
let dealerImages = cpuGameEle.querySelectorAll('img');
for (i = 0; i < yourImages.length; i++) {
yourImages[i].remove();
}
for (i = 0; i < dealerImages.length; i++) {
dealerImages[i].remove();
}
cpuStanding = false;
hmnStanding = false;
hmnDrawBtn.disabled = false;
hmnStandBtn.disabled = false;
document.querySelector('#humanCount').innerHTML = 0;
hmnScore = 0;
document.querySelector('#cpuCount').innerHTML = 0;
cpuScore = 0;
statusElement.innerHTML = 'Lets Play';
statusElement.style.color = 'orange';
}, 2500);
}
function roundWinner() {
if (hmnScore === cpuScore) {
return statusElement.innerHTML = 'Draw';
} else if (hmnScore > 20) {
cpuWins += 1;
cpuEleWins.innerHTML = cpuWins;
statusElement.innerHTML = 'Player busts!';
statusElement.style.color = '#FFCCFF';
} else if (cpuScore > 20) {
hmnWins += 1;
hmnEleWins.innerHTML = hmnWins;
statusElement.innerHTML = 'Computer busts!';
statusElement.style.color = '#B0FBFF';
} else if ((hmnScore <= 20 && cpuScore <= 20) && (hmnScore > cpuScore)) {
hmnWins += 1;
hmnEleWins.innerHTML = hmnWins;
statusElement.innerHTML = 'Player Wins';
statusElement.style.color = '#B0FBFF';
} else if (cpuScore > 20 && hmnScore <= 20) {
hmnWins += 1;
hmnEleWins.innerHTML = hmnWins;
statusElement.innerHTML = 'Player Wins';
statusElement.style.color = '#B0FBFF';
} else {
cpuWins += 1;
cpuEleWins.innerHTML = cpuWins;
statusElement.innerHTML = 'Computer Wins';
statusElement.style.color = '#FFCCFF';
}
checkWinner();
}
function resetGame() {
hmnWins = 0;
hmnEleWins.innerHTML = hmnWins;
cpuWins = 0;
cpuEleWins.innerHTML = cpuWins;
hmnDrawBtn.innerHTML = 'Start playing';
d = new MainDeck();
cpuSideDeck = new SideDeck();
humanSideDeck = new SideDeck();
showSideDeck(0);
showSideDeck(1);
activateSideCard();
console.log(activateSideCard());
// new tunes
backgroundMusic = new Audio('./assets/audio/pazaak.mp3');
}
function checkWinner() {
if (hmnWins >= 3) {
backgroundMusic.pause();
backgroundMusic = new Audio('./assets/audio/congrats.mp3');
backgroundMusic.play();
statusElement.innerHTML = 'You have won the game! Congratulations!!!';
statusElement.style.color = '#B0FBFF';
resetGame();
} else if (cpuWins >= 3) {
backgroundMusic.pause();
backgroundMusic = new Audio('./assets/audio/defeat.mp3');
backgroundMusic.play();
statusElement.innerHTML = 'The oppression of Sith will never return. You have lost!';
statusElement.style.color = '#FFCCFF';
resetGame();
} else {
}
}