-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
465 lines (362 loc) · 14.2 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
const moves = document.getElementById("moves");
const timeValue = document.getElementById("time");
const mach = document.getElementById("matches");
const startButton = document.getElementById("start");
const restartButton = document.getElementById("restart");
const gameContainer = document.querySelector(".game-container");
const result = document.getElementById("result");
const mainContainer = document.querySelector(".Memory-game-main-container");
const startButtonContainer = document.getElementById("startButton");
var gameMode = "beginner";
let cards;
let interval;
let firstCard = false;
let secondCard = false;
let firstClicked = true;
let firstTimeClicked = false;
let stars = 3;
let maches = 0;
let isStart = true;
const items = [
{ name: "bee", image: "./imgs/bee.png" },
{ name: "crocodile", image: "./imgs/crocodile.png" },
{ name: "macaw", image: "./imgs/macaw.png" },
{ name: "gorilla", image: "./imgs/gorilla.png" },
{ name: "tiger", image: "./imgs/tiger.png" },
{ name: "monkey", image: "./imgs/monkey.png" },
{ name: "chameleon", image: "./imgs/chameleon.png" },
{ name: "piranha", image: "./imgs/piranha.png" },
{ name: "anaconda", image: "./imgs/anaconda.png" },
{ name: "sloth", image: "./imgs/sloth.png" },
{ name: "cockatoo", image: "./imgs/cockatoo.png" },
{ name: "toucan", image: "./imgs/toucan.png" },
{ name: "snail", image: "./imgs/snail.png" },
{ name: "octopus", image: "./imgs/octopus.png" },
{ name: "owl", image: "./imgs/owl.png" },
{ name: "sea-turtle", image: "./imgs/sea-turtle.png" },
{ name: "crab", image: "./imgs/crab.png" },
{ name: "cow", image: "./imgs/cow.png" },
{ name: "fox", image: "./imgs/fox.png" },
{ name: "panda", image: "./imgs/panda.png" },
{ name: "deer", image: "./imgs/deer.png" },
{ name: "bat", image: "./imgs/bat.png" },
{ name: "strawberry", image: "./imgs/strawberry.png" },
{ name: "grapes", image: "./imgs/grapes.png" },
{ name: "grapefruit", image: "./imgs/grapefruit.png" },
{ name: "mango", image: "./imgs/mango.png" },
{ name: "watermelon", image: "./imgs/watermelon.png" },
{ name: "bananas", image: "./imgs/bananas.png" },
{ name: "coconut", image: "./imgs/coconut.png" },
{ name: "sea-urchin", image: "./imgs/sea-urchin.png" },
{ name: "forest", image: "./imgs/forest.png" },
{ name: "tree", image: "./imgs/tree.png" },
{ name: "christmas-tree", image: "./imgs/christmas-tree.png" },
{ name: "palm-tree", image: "./imgs/palm-tree.png" },
];
let seconds = 0;
let minutes = 0;
let movesCount = 0;
let winCount = 0;
const timeGenerator = () => {
seconds += 1;
if (seconds >= 60) {
minutes += 1;
seconds = 0;
}
let secondsValue = seconds < 10 ? `0${seconds}` : seconds;
let minutesValue = minutes < 10 ? `0${minutes}` : minutes;
timeValue.innerHTML = `<span>Time: </span>${minutesValue}:${secondsValue}`;
};
const movesCounter = () => {
movesCount += 1;
moves.innerHTML = movesCount < 10 ? `<span>Moves: 0${movesCount}</span>` : `<span>Moves: ${movesCount}</span>`;
};
const generateRandom = (size = 4) => {
let tempArray = [...items];
let cardValues = [];
size = (size * size) / 2;
for (let i = 0; i < size; i++) {
const randomIndex = Math.floor(Math.random() * tempArray.length);
cardValues.push(tempArray[randomIndex]);
tempArray.splice(randomIndex, 1);
}
return cardValues;
};
const matrixGenerator = (cardValues, size = 4) => {
gameContainer.innerHTML = "";
cardValues = [...cardValues, ...cardValues];
cardValues.sort(() => Math.random() - 0.5);
for (let i = 0; i < size * size; i++) {
gameContainer.innerHTML += `
<div class="card-container" data-card-value="${cardValues[i].name}">
<div class="card-before">?</div>
<div class="card-after">
<img src="${cardValues[i].image}" class="image"/>
</div>
</div>
`;
}
gameContainer.style.gridTemplateColumns = `repeat(${size},1fr)`;
let firstCardValue;
let secondCardValue;
cards = document.querySelectorAll(".card-container");
cards.forEach((card) => {
card.addEventListener("click", async () => {
console.log(card);
// Start counter
if(firstClicked) {
startCounter();
firstClicked = false;
}
if(card.classList.contains("flipped")) return;
//If selected card is not matched yet then only run (i.e already matched card when clicked would be ignored)
if (!card.classList.contains("matched")) {
//flip the cliked card
card.classList.add("flipped");
//if it is the firstcard (!firstCard since firstCard is initially false)
if (!firstCard) {
//so current card will become firstCard
firstCard = card;
//current cards value becomes firstCardValue
firstCardValue = card.getAttribute("data-card-value");
// console.log(firstCardValue);
} else {
//increment moves since user selected second card
movesCounter();
// console.log(movesCount);
//secondCard and value
secondCard = card;
let secondCardValue = card.getAttribute("data-card-value");
if (firstCardValue == secondCardValue) {
//if both cards match add matched class so these cards would beignored next time
firstCard.classList.add("matched");
secondCard.classList.add("matched");
firstCard.classList.toggle('active');
secondCard.classList.toggle('active');
maches += 1;
// console.log(maches);
mach.innerHTML = `<span>Maches: ${maches}</span>`;
//set firstCard to false since next card would be first now
firstCard = false;
//winCount increment as user found a correct match
winCount += 1;
console.log(movesCount, " ", winCount);
if (winCount == Math.floor(cardValues.length / 2)) {
// if(winCount == 2){
clearInterval(interval);
startButton.classList.remove("hide");
mainContainer.classList.add("hide");
startButtonContainer.classList.remove("reduce-startButton-area");
win(movesCount, minutes, seconds);
}
} else {
//if the cards dont match
//flip the cards back to normal
let [tempFirst, tempSecond] = [firstCard, secondCard];
firstCard = false;
secondCard = false;
let delay = setTimeout(() => {
tempFirst.classList.remove("flipped");
tempSecond.classList.remove("flipped");
}, 600);
}
}
}
});
});
};
function openPopup() {
document.getElementById('popup').classList.remove('hidePopUp')
document.getElementById('overlay').classList.remove('hidePopUp')
}
// Function to close the popup
function closePopup() {
document.getElementById('popup').classList.add('hidePopUp')
document.getElementById('overlay').classList.add('hidePopUp')
}
function calculateScore(moves, timeInSeconds) {
const maxScore = 5000;
const timePenaltyPerSecond = 10;
const movesPenaltyPerMove = 100;
// Calculate time penalty
const timePenalty = Math.max(0, timeInSeconds - 30) * timePenaltyPerSecond;
// Calculate moves penalty
const movesPenalty = Math.max(0, moves - 24) * movesPenaltyPerMove;
// Calculate final score
const finalScore = Math.max(0, maxScore - timePenalty - movesPenalty);
return finalScore;
}
function win( moves, minutes, seconds) {
firstClicked = true;
let res1 = document.getElementById("res-1");
let res2 = document.getElementById("res-2");
let res3 = document.getElementById("res-3");
let timevalue = minutes * 60 + seconds;
score = Math.round(calculateScore(moves * 2, timevalue));
res1.innerHTML = `You won !!!<br>SCORE: ${score}`;// Display the score
res2.innerHTML = `Moves: ${moves}<br>`;
res3.innerHTML = `Time taken: ${timevalue} seconds`
openPopup();
stopGame();
}
function loose(){
const res1 = document.getElementById('res-1');
const res2 = document.getElementById('res-2');
res1.innerHTML = `You Loose with 0 stars`;
res2.innerHTML = `and Moves: ${movesCount}`
openPopup();
stopGame();
}
function startCounter() {
clearInterval(interval);
interval = setInterval(timeGenerator, 1000);
}
let restart = document.querySelector(".restart");
restart.addEventListener("click", restartGame, false);
function restartGame() {
makeWindowFullScreen();
if(gameMode == "beginner") {
memoryCardBeginnerGame();
}else if(gameMode == "intermediate"){
memoryCardIntermediateGame();
}else if(gameMode == "expert"){
memoryCardExpertGame();
}
}
//Start game
startButton.addEventListener("click", () => {
startMemoryGame();
initializer();
});
function stopGame() {
resetData();
}
function startMemoryGame() {
startButton.classList.add("hide");
restartButton.classList.remove("hide")
mainContainer.classList.remove("hide");
mainContainer.classList.remove("reset-size-main-container");
startButtonContainer.classList.add("reduce-startButton-area");
makeWindowFullScreen();
movesCount = 0;
seconds = 0;
minutes = 0;
maches = 0;
clearInterval(interval);
startCounter();
moves.innerHTML = `<span>Moves:</span> ${movesCount}`;
timeValue.innerHTML = `<span>Time:00:00</span>`;
mach.innerHTML = `<span>Maches:0</span>`;
}
/// MAKE WINDOW FULL SCREEN
let isFullscreen = false;
function makeWindowFullScreen() {
const elem = document.documentElement;
document.querySelector('.start').addEventListener('click', () => {
if(!isFullscreen) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.webkitRequestFullscreen) { /* Safari */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) { /* IE11 */
elem.msRequestFullscreen();
}
isFullscreen = true;
}
});
document.querySelector('.restart').addEventListener('click', () => {
if(!isFullscreen){
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.webkitRequestFullscreen) { /* Safari */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) { /* IE11 */
elem.msRequestFullscreen();
}
isFullscreen = true;
}
});
}
//Initialize values and func calls
const initializer = () => {
result.innerText = "";
winCount = 0;
let cardValues = generateRandom();
matrixGenerator(cardValues);
};
function resetData() {
winCount = 0;
movesCount = 0;
seconds = 0;
minutes = 0;
maches = 0;
firstClicked = true;
moves.innerHTML = `<span>Moves: </span>${movesCount}`;
timeValue.innerHTML = `<span>Time: 00:00</span>`;
mach.innerHTML = `<span>Maches: 0</span>`;
clearInterval(interval);
}
function memoryCardBeginnerGame(size = 4) {
gameSetUpBeginner(100, 95, 400, 90, 300, 20, 20);
gameMode = "beginner";
stopGame();
result.innerText = "";
winCount = 0;
let cardValues = generateRandom(size);
matrixGenerator(cardValues, size);
}
function memoryCardIntermediateGame(size = 6) {
gameMode = "intermediate";
stopGame();
result.innerText = "";
winCount = 0;
let cardValues = generateRandom(size);
matrixGenerator(cardValues, size);
}
function memoryCardExpertGame(size = 8) {
gameMode = "expert";
stopGame();
result.innerText = "";
winCount = 0;
let cardValues = generateRandom(size);
matrixGenerator(cardValues, 8);
}
initializer();
function gameSetUpBeginner(containerWidth, wrapperWidth, wrapperHeight, gameContainerWidth, gameContainerHeight, cardBefore, cardAfter) {
const wrapperContainer = document.querySelector(".memory-game-wrapper");
const GameContainer = document.querySelector(".game-container");
const CardBefore = document.querySelector(".card-before");
const CardAfter = document.querySelector(".card-after");
wrapperContainer.style.setProperty("--memoryGame-wrapper-size-width", wrapperWidth+"%");
wrapperContainer.style.setProperty("--memoryGame-wrapper-size-height", wrapperHeight+"px");
GameContainer.style.setProperty("--memoryGame-gameContainer-size-width", gameContainerWidth+"%");
GameContainer.style.setProperty("--memoryGame-gameContainer-size-height", gameContainerWidth+"%");
CardBefore.style.setProperty("--memoryGame-cardBefore-size-width", cardBefore+"%");
CardBefore.style.setProperty("--memoryGame-cardBefore-size-height", cardBefore+"%");
CardAfter.style.setProperty("--memoryGame--cardAfter-size-width", cardAfter+"%");
CardAfter.style.setProperty("--memoryGame--cardAfter-size-height", cardAfter+"%");
}
function gameSetUpIntermediateGame() {
const wrapperContainer = document.querySelector(".memory-game-wrapper");
const GameContainer = document.querySelector(".game-container");
const CardBefore = document.querySelector(".card-before");
const CardAfter = document.querySelector(".card-after");
// mainContainer.style.setProperty("--memoryGame-mainContainer-size-width", containerWidth+"px");
wrapperContainer.style.setProperty("--memoryGame-wrapper-size-width", wrapperWidth+"px");
cardBefore.style.setProperty("--memoryGame-cardBefore-size-width", cardBefore+"px");
cardBefore.style.setProperty("--memoryGame-cardBefore-size-height", cardBefore+"px");
cardAfter.style.setProperty("--memoryGame--cardAfter-size-width", cardAfter+"px");
cardAfter.style.setProperty("--memoryGame--cardAfter-size-height", cardAfter+"px");
}
function gameSetUpExpert() {
const wrapperContainer = document.querySelector(".memory-game-wrapper");
const GameContainer = document.querySelector(".game-container");
const CardBefore = document.querySelector(".card-before");
const CardAfter = document.querySelector(".card-after");
// mainContainer.style.setProperty("--memoryGame-mainContainer-size-width", containerWidth+"px");
wrapperContainer.style.setProperty("--memoryGame-wrapper-size-width", wrapperWidth+"px");
cardBefore.style.setProperty("--memoryGame-cardBefore-size-width", cardBefore+"px");
cardBefore.style.setProperty("--memoryGame-cardBefore-size-height", cardBefore+"px");
cardAfter.style.setProperty("--memoryGame--cardAfter-size-width", cardAfter+"px");
cardAfter.style.setProperty("--memoryGame--cardAfter-size-height", cardAfter+"px");
}