Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
328 lines (294 loc) · 10.7 KB
/
Copy pathscript.js
File metadata and controls
328 lines (294 loc) · 10.7 KB
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
/**
* @fileoverview MindMatch - Memory Challenge Game
* @version 1.0.0
* @author Your Name
* @description A memory matching game with multiple categories, animations,
* and a countdown timer. Features include category selection, move tracking,
* and win/lose states.
*
* Game Structure:
* - Welcome Page: Category selection and game start
* - Game Play: Card matching with timer and move tracking
* - Results: Win/lose screens with stats
*/
/* ------------------ Welcome & Page Elements ------------------ */
const welcomePage = document.getElementById("welcome-page");
const gameContainer = document.getElementById("game-container");
const winPage = document.getElementById("win-page");
const losePage = document.getElementById("lose-page");
const startBtn = document.getElementById("start-btn");
const playAgainWinBtn = document.getElementById("play-again-win");
const playAgainLoseBtn = document.getElementById("play-again-lose");
/* ------------------ Game Play Elements ------------------ */
const grid = document.getElementById("grid");
const movesEl = document.getElementById("moves");
const matchesEl = document.getElementById("matches");
const resetBtn = document.getElementById("reset-btn");
const timerEl = document.getElementById("timer");
/* ------------------ Result Elements ------------------ */
const timeRemainingEl = document.getElementById("time-remaining");
const finalMovesEl = document.getElementById("final-moves");
const finalMovesLoseEl = document.getElementById("final-moves-lose");
const finalMatchesEl = document.getElementById("final-matches");
let timer;
let timeLeft;
// Category selection handlers
const fruitsBtn = document.getElementById("fruits-btn");
const foodBtn = document.getElementById("food-btn");
const animalsBtn = document.getElementById("animals-btn");
/**
* Updates the selected category and prepares the game with new emojis
* @param {string} category - The category to switch to ('fruits', 'food', or 'animals')
*/
function selectCategory(category) {
// Remove selected class from all buttons
[fruitsBtn, foodBtn, animalsBtn].forEach(btn =>
btn.classList.remove("selected")
);
// Add selected class to clicked button
document.getElementById(`${category}-btn`).classList.add("selected");
// Update current category
currentCategory = category;
emojis = categoryEmojis[category];
cards = [...emojis, ...emojis];
// Show start button with animation
startBtn.style.display = "block";
}
// Add click handlers for category buttons
fruitsBtn.addEventListener("click", () => selectCategory("fruits"));
foodBtn.addEventListener("click", () => selectCategory("food"));
animalsBtn.addEventListener("click", () => selectCategory("animals"));
// Start button click handler
startBtn.addEventListener("click", () => {
welcomePage.style.display = "none";
gameContainer.style.display = "block";
resetGame();
});
const categoryEmojis = {
fruits: ["🍎", "🍌", "🍇", "🍒", "🍉", "🍍", "🥝", "🍓"],
food: ["🍕", "🌮", "🍔", "🍜", "🍣", "🥪", "🌭", "🍪"],
animals: ["🦁", "🐘", "🦒", "🦊", "🐼", "🐸", "🦄", "🦋"]
};
let currentCategory = "fruits";
let emojis = categoryEmojis[currentCategory];
let cards = [...emojis, ...emojis];
let flippedCards = [];
let lockBoard = false;
let moves = 0;
let matches = 0;
/**
* Randomly shuffles an array using the Fisher-Yates algorithm
* @param {Array} array - The array to shuffle
* @returns {Array} The shuffled array
*/
function shuffle(array) {
return array.sort(() => Math.random() - 0.5);
}
/**
* Creates the game board with shuffled cards and starts the timer
* Creates card elements, adds event listeners, and initializes the game state
*/
function createBoard() {
grid.innerHTML = "";
cards = shuffle(cards);
cards.forEach((emoji) => {
const card = document.createElement("div");
card.classList.add("card");
card.innerHTML = `
<div class="card-inner">
<div class="card-front">${emoji}</div>
<div class="card-back"></div>
</div>
`;
card.addEventListener("click", () => flipCard(card, emoji));
grid.appendChild(card);
});
startTimer();
}
/**
* Starts the game timer with 90 seconds countdown
* Updates display every second and ends game if time runs out
*/
function startTimer() {
timeLeft = 90; // 1 minute 30 seconds in seconds
updateTimerDisplay();
timer = setInterval(() => {
timeLeft--;
updateTimerDisplay();
if (timeLeft <= 0) {
endGame(false);
}
}, 1000);
}
/**
* Updates the timer display in MM:SS format
* Converts seconds to minutes and seconds, pads with zeros
*/
function updateTimerDisplay() {
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
timerEl.textContent = `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
/**
* Displays the win screen with final stats
* Shows remaining time and number of moves taken
*/
function showWinPage() {
gameContainer.style.display = 'none';
winPage.style.display = 'flex';
// populate new win UI
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
const timeText = `${minutes}:${seconds.toString().padStart(2, '0')}`;
document.getElementById('win-time').textContent = timeText;
document.getElementById('win-moves').textContent = moves;
document.getElementById('win-matches').textContent = matches;
const accuracy = Math.round((matches / emojis.length) * 100);
document.getElementById('win-accuracy').textContent = `${accuracy}%`;
const score = computeScore(true);
document.getElementById('win-score').textContent = score;
// celebratory confetti
createConfetti();
// stop confetti after a while
setTimeout(clearConfetti, 4500);
}
/**
* Displays the lose screen with final stats
* Shows total moves and matches achieved
*/
function showLosePage() {
gameContainer.style.display = 'none';
losePage.style.display = 'flex';
// populate new lose UI
document.getElementById('lose-moves').textContent = moves;
document.getElementById('lose-matches').textContent = matches;
const accuracy = Math.round((matches / emojis.length) * 100);
document.getElementById('lose-accuracy').textContent = `${accuracy}%`;
document.getElementById('lose-score').textContent = computeScore(false);
}
function endGame(won) {
clearInterval(timer);
if (won) {
showWinPage();
} else {
showLosePage();
}
}
/**
* Compute a simple score based on matches, moves and time left
* @param {boolean} won
* @returns {number}
*/
function computeScore(won) {
// base points per match, bonus for time left, penalty for moves
const base = matches * 150;
const timeBonus = Math.max(0, timeLeft) * 3;
const movePenalty = Math.max(0, moves) * 2;
let total = base + timeBonus - movePenalty;
if (!won) total = Math.max(0, Math.floor(total / 3));
return Math.max(0, Math.round(total));
}
/* ---------------- Confetti helpers ---------------- */
const confettiContainer = document.getElementById('confetti-container');
function randomBetween(min, max) { return Math.random() * (max - min) + min; }
function createConfetti() {
if (!confettiContainer) return;
const colors = ['#ff6b6b', '#ffd166', '#21e6c1', '#e94560', '#c3f3ff'];
const count = 90;
for (let i = 0; i < count; i++) {
const el = document.createElement('div');
el.classList.add('confetti');
el.style.background = colors[i % colors.length];
el.style.left = `${randomBetween(0, 100)}%`;
el.style.top = `${randomBetween(-10, -1)}%`;
el.style.width = `${randomBetween(6, 12)}px`;
el.style.height = `${randomBetween(8, 16)}px`;
// random fall duration and delay
const duration = randomBetween(3.5, 6.5);
const delay = randomBetween(0, 0.8);
el.style.animationDuration = `${duration}s`;
el.style.animationDelay = `${delay}s`;
el.style.transform = `rotate(${randomBetween(0,360)}deg)`;
confettiContainer.appendChild(el);
}
}
function clearConfetti() {
if (!confettiContainer) return;
while (confettiContainer.firstChild) confettiContainer.removeChild(confettiContainer.firstChild);
}
/* ---------------- Result action wiring ---------------- */
// Back to menu handlers
const backToMenuWin = document.getElementById('back-to-menu-win');
const backToMenuLose = document.getElementById('back-to-menu-lose');
if (backToMenuWin) backToMenuWin.addEventListener('click', () => {
winPage.style.display = 'none';
welcomePage.style.display = 'flex';
clearConfetti();
});
if (backToMenuLose) backToMenuLose.addEventListener('click', () => {
losePage.style.display = 'none';
welcomePage.style.display = 'flex';
});
// Share score handlers (copy a simple score text to clipboard)
// Share score removed — share buttons/handlers were deleted per design
/**
* Handles card flip animation and game logic
* @param {HTMLElement} card - The card element being flipped
* @param {string} emoji - The emoji on the card
*/
function flipCard(card, emoji) {
if (lockBoard || card.classList.contains("flip")) return;
card.classList.add("flip");
flippedCards.push({ card, emoji });
if (flippedCards.length === 2) {
moves++;
movesEl.textContent = moves;
checkForMatch();
}
}
/**
* Checks if two flipped cards match and handles the result
* If cards match, updates score and checks for win condition
* If cards don't match, flips them back after a delay
*/
function checkForMatch() {
const [first, second] = flippedCards;
if (first.emoji === second.emoji) {
matches++;
matchesEl.textContent = matches;
flippedCards = [];
if (matches === emojis.length) {
setTimeout(() => {
endGame(true);
}, 500);
}
} else {
lockBoard = true;
setTimeout(() => {
first.card.classList.remove("flip");
second.card.classList.remove("flip");
flippedCards = [];
lockBoard = false;
}, 1000);
}
}
function resetGame() {
clearInterval(timer);
moves = 0;
matches = 0;
movesEl.textContent = 0;
matchesEl.textContent = 0;
flippedCards = [];
lockBoard = false;
winPage.style.display = 'none';
losePage.style.display = 'none';
gameContainer.style.display = 'block';
createBoard();
}
resetBtn.addEventListener("click", resetGame);
playAgainWinBtn.addEventListener("click", resetGame);
playAgainLoseBtn.addEventListener("click", resetGame);
// Don't create board immediately, wait for start button
if (gameContainer.style.display !== "none") {
createBoard();
}