forked from Wavyeli322/MemoryMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
238 lines (209 loc) · 6.6 KB
/
script.js
File metadata and controls
238 lines (209 loc) · 6.6 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
document.addEventListener("DOMContentLoaded", () => {
const allPairs = {
"Alabama": "Montgomery",
"Alaska": "Juneau",
"Arizona": "Phoenix",
"Arkansas": "Little Rock",
"California": "Sacramento",
"Colorado": "Denver",
"Connecticut": "Hartford",
"Delaware": "Dover",
"Florida": "Tallahassee",
"Georgia": "Atlanta",
"Hawaii": "Honolulu",
"Idaho": "Boise",
"Illinois": "Springfield",
"Indiana": "Indianapolis",
"Iowa": "Des Moines",
"Kansas": "Topeka",
"Kentucky": "Frankfort",
"Louisiana": "Baton Rouge",
"Maine": "Augusta",
"Maryland": "Annapolis",
"Massachusetts": "Boston",
"Michigan": "Lansing",
"Minnesota": "Saint Paul",
"Mississippi": "Jackson",
"Missouri": "Jefferson City",
"Montana": "Helena",
"Nebraska": "Lincoln",
"Nevada": "Carson City",
"New Hampshire": "Concord",
"New Jersey": "Trenton",
"New Mexico": "Santa Fe",
"New York": "Albany",
"North Carolina": "Raleigh",
"North Dakota": "Bismarck",
"Ohio": "Columbus",
"Oklahoma": "Oklahoma City",
"Oregon": "Salem",
"Pennsylvania": "Harrisburg",
"Rhode Island": "Providence",
"South Carolina": "Columbia",
"South Dakota": "Pierre",
"Tennessee": "Nashville",
"Texas": "Austin",
"Utah": "Salt Lake City",
"Vermont": "Montpelier",
"Virginia": "Richmond",
"Washington": "Olympia",
"West Virginia": "Charleston",
"Wisconsin": "Madison",
"Wyoming": "Cheyenne"
};
let firstCard = null;
let secondCard = null;
let matchedPairs = 0;
let currentLevel = parseInt(localStorage.getItem("currentLevel")) || 1;
let score = parseInt(localStorage.getItem("score")) || 0;
let timeLeft = calculateTimeLeft(currentLevel);
let timer; // Global timer variable
let gamePaused = false; // Track if the game is paused
const cards = document.querySelectorAll(".card");
const scoreDisplay = document.querySelector("#score");
const timerDisplay = document.querySelector("#timer");
const pauseMenu = document.getElementById('pauseMenu'); // Pause menu
function calculateTimeLeft(level) {
const initialTime = 150;
const timeReductionPerLevel = 10;
return Math.max(initialTime - (level - 1) * timeReductionPerLevel, 30);
}
function updateScoreDisplay() {
if (scoreDisplay) {
scoreDisplay.textContent = `Score: ${score}`;
}
}
function updateTimerDisplay() {
if (timerDisplay) {
timerDisplay.textContent = `Time: ${timeLeft}s`;
}
}
function startTimer() {
updateTimerDisplay();
timer = setInterval(() => {
if (!gamePaused) {
timeLeft--;
updateTimerDisplay();
if (timeLeft <= 0) {
endGame("Time's up! Try again.");
}
}
}, 1000);
}
const gameBoard = document.querySelector(".game-board");
function pauseGame() {
gamePaused = true;
clearInterval(timer);
gameBoard.style.visibility = "hidden"; // Hide without changing layout
pauseMenu.style.display = "block";
}
function resumeGame() {
gamePaused = false;
startTimer();
gameBoard.style.visibility = "visible"; // Restore visibility
pauseMenu.style.display = "none";
}
function saveProgress() {
localStorage.setItem('gameProgress', JSON.stringify({ level: currentLevel, score: score }));
alert("Game progress saved!");
}
function returnToSelectMode() {
alert("Returning to Select Mode.");
window.location.href = "select-mode.html";
}
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function setupCards() {
const states = Object.keys(allPairs);
const capitals = Object.values(allPairs);
const numPairs = cards.length / 2;
const selectedPairs = [];
while (selectedPairs.length < numPairs * 2) {
const randomIndex = Math.floor(Math.random() * states.length);
const state = states[randomIndex];
const capital = allPairs[state];
if (!selectedPairs.includes(state) && !selectedPairs.includes(capital)) {
selectedPairs.push(state, capital);
}
}
shuffle(selectedPairs);
cards.forEach((card, index) => {
card.dataset.value = selectedPairs[index];
card.textContent = "?";
card.classList.remove("matched", "correct", "wrong");
card.addEventListener("click", handleCardClick);
});
}
function handleCardClick(e) {
if (gamePaused) return; // Disable interaction when paused
const clickedCard = e.target;
if (clickedCard === firstCard || clickedCard.classList.contains("matched") || secondCard) return;
clickedCard.textContent = clickedCard.dataset.value;
if (!firstCard) {
firstCard = clickedCard;
} else {
secondCard = clickedCard;
checkMatch();
}
}
function checkMatch() {
const isMatch =
allPairs[firstCard.dataset.value] === secondCard.dataset.value ||
allPairs[secondCard.dataset.value] === firstCard.dataset.value;
if (isMatch) {
firstCard.classList.add("matched", "correct");
secondCard.classList.add("matched", "correct");
matchedPairs++;
score += 10;
localStorage.setItem("score", score);
updateScoreDisplay();
resetCards();
if (matchedPairs === cards.length / 2) {
clearInterval(timer);
setTimeout(() => {
alert(`Level ${currentLevel} completed!`);
currentLevel++;
localStorage.setItem("currentLevel", currentLevel);
timeLeft = calculateTimeLeft(currentLevel);
window.location.href = `classic${currentLevel}.html`;
}, 500);
}
} else {
firstCard.classList.add("wrong");
secondCard.classList.add("wrong");
setTimeout(() => {
firstCard.textContent = "?";
secondCard.textContent = "?";
firstCard.classList.remove("wrong");
secondCard.classList.remove("wrong");
resetCards();
}, 1000);
score -= 5;
localStorage.setItem("score", score);
updateScoreDisplay();
}
}
function resetCards() {
firstCard = null;
secondCard = null;
}
function endGame(message) {
clearInterval(timer);
alert(`${message} Final Score: ${score}`);
localStorage.setItem("currentLevel", 1);
localStorage.setItem("score", 0);
window.location.href = "score.html";
}
window.pauseGame = pauseGame;
window.resumeGame = resumeGame;
window.saveProgress = saveProgress;
window.returnToSelectMode = returnToSelectMode;
// Game initialization
updateScoreDisplay();
setupCards();
startTimer();
});