-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
249 lines (217 loc) · 6.63 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
const gridContainer = document.querySelector(".grid-container");
const myAudio = document.getElementById("Myaudio");
let cards = [];
let firstCard, secondCard;
let lockBoard = false;
let score = 0;
let attempts = 0;
let maxAttempts = 15;
let matchedCardsCount = 0;
let timer;
let timeLeft = 60;
let isTimerRunning = false;
let gameOver = false;
myAudio.volume = .1;
document.querySelector(".score").textContent = score;
document.querySelector(".time").textContent = `Time Left: ${timeLeft}s`;
window.onload = function() {
showIntroModal();
};
function fetchCards() {
return fetch("./data/cards.json")
.then((res) => res.json())
.then((data) => {
cards = [...data, ...data];
shuffleCards();
generateCards();
});
}
function showIntroModal() {
const introModal = document.createElement("div");
introModal.classList.add("intro-modal");
introModal.innerHTML = `
<div class="modal-content">
<h2>Welcome to Memory Card Game!</h2>
<p>Test your memory and match all the cards!</p>
<p>Mechanics:</p>
<ul>
<li><strong>• 15 Attempts</strong> to match the cards</li>
<li><strong>• 1 Minute</strong> time limit</li>
<li>• Match pairs of cards by flipping them</li>
<li>• If all pairs are matched before time runs out, you win!</li>
</ul>
<button onclick="startGame()">Start Game</button>
</div>
`;
document.body.appendChild(introModal);
setTimeout(() => {
introModal.classList.add("modal-show");
}, 100);
const listItems = introModal.querySelectorAll("ul li");
listItems.forEach((item, index) => {
item.style.animationDelay = `${index * 0.3}s`;
item.classList.add('fadeIn');
});
}
function startGame() {
const introModal = document.querySelector(".intro-modal");
if (introModal) {
introModal.classList.remove("modal-show");
setTimeout(() => {
introModal.remove();
if (cards.length === 0) {
fetchCards();
} else {
shuffleCards();
generateCards();
}
}, 500);
}
}
function shuffleCards() {
let currentIndex = cards.length,
randomIndex, temporaryValue;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = cards[currentIndex];
cards[currentIndex] = cards[randomIndex];
cards[randomIndex] = temporaryValue;
}
}
function generateCards() {
gridContainer.innerHTML = "";
for (let card of cards) {
const cardElement = document.createElement("div");
cardElement.classList.add("card");
cardElement.setAttribute("data-name", card.name);
cardElement.innerHTML = `
<div class="front">
<img class="front-image" src=${card.image} />
</div>
<div class="back"></div>
`;
gridContainer.appendChild(cardElement);
cardElement.addEventListener("click", flipcard);
}
}
function startTimer() {
if (isTimerRunning) return;
isTimerRunning = true;
timer = setInterval(() => {
if (gameOver) {
clearInterval(timer);
return;
}
timeLeft--;
document.querySelector(".time").textContent = `Time Left: ${timeLeft}s`;
if (timeLeft <= 0) {
clearInterval(timer);
timeLeft = 60;
isTimerRunning = false;
gameOver = true;
setTimeout(showTryAgainModal, 500);
}
}, 1000);
}
function flipcard() {
if (lockBoard || attempts >= maxAttempts || timeLeft <= 0) {
if (attempts >= maxAttempts && !document.querySelector(".modal")) {
setTimeout(showTryAgainModal, 500);
}
return;
}
if (this === firstCard) return;
this.classList.add("flipped");
if (!firstCard) {
firstCard = this;
if (!isTimerRunning) startTimer();
return;
}
secondCard = this;
lockBoard = true;
checkforMatch();
}
function checkforMatch() {
let isMatch = firstCard.dataset.name === secondCard.dataset.name;
if (isMatch) {
matchedCardsCount++;
disabledCards();
} else {
attempts++;
score++;
document.querySelector(".score").textContent = score;
unflipCards();
}
if (matchedCardsCount === cards.length / 2) {
setTimeout(showWinnerModal, 500);
}
}
function disabledCards() {
firstCard.removeEventListener("click", flipcard);
secondCard.removeEventListener("click", flipcard);
resetBoard();
}
function unflipCards() {
setTimeout(() => {
firstCard.classList.remove("flipped");
secondCard.classList.remove("flipped");
resetBoard();
}, 1000);
}
function resetBoard() {
firstCard = null;
secondCard = null;
lockBoard = false;
}
function showTryAgainModal() {
const tryAgainModal = document.createElement("div");
tryAgainModal.classList.add("modal", "try-again");
tryAgainModal.innerHTML = `
<div class="modal-content">
<h2>Oops, you've reached the maximum attempts or time!</h2>
<p>Don't worry, you can try again and improve your memory!</p>
<button onclick="restart()">Try Again</button>
</div>
`;
document.body.appendChild(tryAgainModal);
setTimeout(() => {
tryAgainModal.classList.add("modal-show");
}, 100);
}
function showWinnerModal() {
clearInterval(timer);
gameOver = true;
timeLeft = 60;
const winnerModal = document.createElement("div");
winnerModal.classList.add("modal", "you-win");
winnerModal.innerHTML = `
<div class="modal-content">
<h2>Congratulations!</h2>
<p>You matched all the cards!</p>
<button onclick="restart()">Play Again</button>
</div>
`;
document.body.appendChild(winnerModal);
setTimeout(() => {
winnerModal.classList.add("modal-show");
}, 100);
generateConfetti();
}
function restart() {
resetBoard();
matchedCardsCount = 0;
attempts = 0;
score = 0;
document.querySelector(".score").textContent = score;
document.querySelector(".time").textContent = `Time Left: 60s`;
timeLeft = 60;
gameOver = false;
isTimerRunning = false;
clearInterval(timer);
gridContainer.innerHTML = "";
shuffleCards();
generateCards();
const modals = document.querySelectorAll(".modal");
modals.forEach(modal => modal.remove());
}