-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
292 lines (254 loc) · 8.49 KB
/
script.js
File metadata and controls
292 lines (254 loc) · 8.49 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
const gameBoard = document.getElementById("game-board");
const movesDisplay = document.getElementById("moves");
const timeDisplay = document.getElementById("timer");
const restartBtn = document.getElementById("restart-btn");
const statusText = document.getElementById("status-text");
const levelDisplay = document.getElementById("level-display");
const profileBtn = document.getElementById("profile-btn");
const profileDropdown = document.getElementById("profile-dropdown");
const nameInput = document.getElementById("name-input");
const saveNameBtn = document.getElementById("save-name-btn");
const playerNameDisplay = document.getElementById("player-name");
const icons = [
"pets",
"rocket_launch",
"favorite",
"star",
"bolt",
"key",
"visibility",
"settings",
"home",
"search",
"menu",
"lock",
"person",
"image",
"mail",
"notifications",
"location_on",
"play_arrow",
"wifi",
"battery_full",
"bluetooth",
"camera_alt",
"directions_car",
"eco",
"water_drop",
"flight",
"train",
"directions_boat",
"music_note",
"light_mode",
"dark_mode",
"palette",
"shopping_cart",
"info",
"wb_sunny",
"local_fire_department", // Added 2 more for level 5 (36 cards)
];
const levels = [
{ level: 1, rows: 4, cols: 3, pairs: 6 },
{ level: 2, rows: 4, cols: 4, pairs: 8 },
{ level: 3, rows: 5, cols: 4, pairs: 10 },
{ level: 4, rows: 6, cols: 5, pairs: 15 },
{ level: 5, rows: 6, cols: 6, pairs: 18 },
];
let cards = [];
let hasFlippedCard = false;
let lockBoard = false;
let firstCard, secondCard;
let moves = 0;
let timerInterval;
let seconds = 0;
let gameStarted = false;
let matchedPairs = 0;
let currentLevelIndex = 0;
function shuffle(array) {
let currentIndex = array.length,
randomIndex;
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex],
];
}
return array;
}
function initGame() {
// Reset state
hasFlippedCard = false;
lockBoard = false;
firstCard = null;
secondCard = null;
moves = 0;
seconds = 0;
gameStarted = false;
matchedPairs = 0;
const currentLevelConfig = levels[currentLevelIndex];
const totalPairs = currentLevelConfig.pairs;
// Update UI
movesDisplay.textContent = moves;
timeDisplay.textContent = "00:00";
statusText.textContent = "Select a card";
levelDisplay.textContent = `${currentLevelConfig.level}/${levels.length}`;
restartBtn.innerHTML = `
<span class="material-symbols-outlined mr-2 text-[20px] group-hover:-rotate-180 transition-transform duration-500">refresh</span>
<span>Restart Level</span>
`;
clearInterval(timerInterval);
// Configure Board Grid
gameBoard.className = `grid gap-3 w-full h-full p-5 bg-surface-light dark:bg-surface-dark rounded-4xl shadow-soft-xl border border-black/[0.04] dark:border-white/[0.08] ring-1 ring-black/[0.02] dark:ring-white/[0.02] transform transition-transform duration-500`;
gameBoard.style.gridTemplateColumns = `repeat(${currentLevelConfig.cols}, minmax(0, 1fr))`;
gameBoard.style.gridTemplateRows = `repeat(${currentLevelConfig.rows}, minmax(0, 1fr))`;
// Create cards
const selectedIcons = icons.slice(0, totalPairs);
const cardIcons = [...selectedIcons, ...selectedIcons]; // Duplicate for pairs
shuffle(cardIcons);
gameBoard.innerHTML = "";
cardIcons.forEach((icon, index) => {
const cardElement = document.createElement("div");
cardElement.className =
"relative w-full h-full perspective-1000 group cursor-pointer";
cardElement.dataset.icon = icon;
cardElement.dataset.index = index;
cardElement.innerHTML = `
<div class="relative w-full h-full text-center transition-all duration-500 ease-out transform-style-3d group-hover:scale-[1.05] group-hover:-translate-y-1 group-hover:shadow-card-hover rounded-xl card-inner">
<!-- Back Face -->
<div class="absolute w-full h-full bg-slate-100 dark:bg-slate-800 rounded-xl flex items-center justify-center backface-hidden border border-black/[0.04] dark:border-white/[0.05] shadow-card card-texture">
<div class="w-2 h-2 rounded-full bg-slate-300 dark:bg-slate-600 opacity-50"></div>
</div>
<!-- Front Face -->
<div class="absolute w-full h-full bg-white dark:bg-slate-700 rounded-xl flex items-center justify-center rotate-y-180 border-2 border-primary ring-2 ring-primary/10 shadow-inner-subtle backface-hidden">
<span class="material-symbols-outlined text-primary text-3xl drop-shadow-sm transform transition-transform">${icon}</span>
</div>
</div>
`;
cardElement.addEventListener("click", flipCard);
gameBoard.appendChild(cardElement);
});
}
function startTimer() {
if (gameStarted) return;
gameStarted = true;
timerInterval = setInterval(() => {
seconds++;
const mins = Math.floor(seconds / 60)
.toString()
.padStart(2, "0");
const secs = (seconds % 60).toString().padStart(2, "0");
timeDisplay.textContent = `${mins}:${secs}`;
}, 1000);
}
function flipCard() {
if (lockBoard) return;
if (this === firstCard) return;
if (!gameStarted) startTimer();
this.querySelector(".card-inner").classList.add("flipped");
if (!hasFlippedCard) {
// First click
hasFlippedCard = true;
firstCard = this;
statusText.textContent = "Select a second card";
return;
}
// Second click
secondCard = this;
incrementMoves();
checkForMatch();
}
function checkForMatch() {
let isMatch = firstCard.dataset.icon === secondCard.dataset.icon;
isMatch ? disableCards() : unflipCards();
}
function disableCards() {
firstCard.removeEventListener("click", flipCard);
secondCard.removeEventListener("click", flipCard);
matchedPairs++;
const currentLevelConfig = levels[currentLevelIndex];
if (matchedPairs === currentLevelConfig.pairs) {
endGame();
} else {
statusText.textContent = "Match found!";
setTimeout(() => {
if (statusText.textContent === "Match found!")
statusText.textContent = "Select a card";
}, 1500);
}
resetBoard();
}
function unflipCards() {
lockBoard = true;
statusText.textContent = "Not a match...";
setTimeout(() => {
firstCard.querySelector(".card-inner").classList.remove("flipped");
secondCard.querySelector(".card-inner").classList.remove("flipped");
statusText.textContent = "Try again";
resetBoard();
}, 1000);
}
function resetBoard() {
[hasFlippedCard, lockBoard] = [false, false];
[firstCard, secondCard] = [null, null];
}
function incrementMoves() {
moves++;
movesDisplay.textContent = moves;
}
function endGame() {
clearInterval(timerInterval);
const isLastLevel = currentLevelIndex === levels.length - 1;
if (isLastLevel) {
statusText.textContent = `You completed all levels!`;
restartBtn.innerHTML = `
<span class="material-symbols-outlined mr-2 text-[20px] group-hover:-rotate-180 transition-transform duration-500">replay</span>
<span>Play Again</span>
`;
restartBtn.onclick = () => {
currentLevelIndex = 0;
restartBtn.onclick = initGame; // Reset handler
initGame();
};
} else {
statusText.textContent = `Level ${levels[currentLevelIndex].level} Complete!`;
restartBtn.innerHTML = `
<span class="material-symbols-outlined mr-2 text-[20px] transition-transform duration-500">arrow_forward</span>
<span>Next Level</span>
`;
// Temporarily override click handler for next level
restartBtn.onclick = () => {
currentLevelIndex++;
restartBtn.onclick = initGame; // Reset handler
initGame();
};
}
}
// Profile Dropdown Logic
profileBtn.addEventListener("click", (e) => {
e.stopPropagation();
profileDropdown.classList.toggle("hidden");
});
document.addEventListener("click", (e) => {
if (!profileBtn.contains(e.target) && !profileDropdown.contains(e.target)) {
profileDropdown.classList.add("hidden");
}
});
saveNameBtn.addEventListener("click", () => {
const newName = nameInput.value.trim();
if (newName) {
playerNameDisplay.textContent = newName;
localStorage.setItem("memoryGamePlayerName", newName);
profileDropdown.classList.add("hidden");
}
});
// Load saved name
const savedName = localStorage.getItem("memoryGamePlayerName");
if (savedName) {
playerNameDisplay.textContent = savedName;
nameInput.value = savedName;
}
restartBtn.onclick = initGame;
// Initialize on load
initGame();