-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
213 lines (187 loc) · 6.43 KB
/
Copy pathscript.js
File metadata and controls
213 lines (187 loc) · 6.43 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
const cards = document.querySelectorAll(".card");
let gameMode = "relaxed";
let matched = 0;
let cardOne, cardTwo;
let disableDeck = false;
let timerInterval;
let elapsedTime = 0;
let countdownTime = 120;
let flipCount = 0;
// Function to show card previews for half a second
function previewCards() {
// Flip all cards to show their images
cards.forEach((card) => {
card.classList.add("flip");
});
// Schedule a timeout to hide the cards after half a second (500 milliseconds)
setTimeout(() => {
cards.forEach((card) => {
card.classList.remove("flip");
});
}, 500); // Adjust the duration as needed
}
// Function to update the timer display
function updateTimer() {
const timerElement = document.getElementById("timer");
if (gameMode === "challenge") {
if (countdownTime <= 0) {
// Timer reached zero, end the game in challenge mode
clearInterval(timerInterval);
disableDeck = true; // Disable further card clicks
setTimeout(() => {
// Display a game-over message
alert("Game Over - Challenge Mode");
}, 1000); // Wait for 1 second before showing the message
} else {
const minutes = Math.floor(countdownTime / 60);
const seconds = countdownTime % 60;
const formattedTime = `${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
timerElement.textContent = formattedTime;
countdownTime--; // Decrease the countdown time
// Check if the player has matched all cards (beat the challenge)
if (matched == 8) {
clearInterval(timerInterval); // Stop the timer
// Display a message or perform any other actions for completing the challenge
alert("Challenge Completed! Timer Reset.");
resetGame(); // Reset the game, including the timer
}
}
} else if (gameMode === "relaxed") {
const minutes = Math.floor(elapsedTime / 60);
const seconds = elapsedTime % 60;
const formattedTime = `${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
timerElement.textContent = formattedTime;
elapsedTime++; // Increase elapsed time
}
}
// Function to increment the flip counter
function incrementFlipCounter() {
flipCount++;
const flipCounterElement = document.getElementById("flip-count");
flipCounterElement.textContent = flipCount;
}
// Add event listeners to cards to count flips and start the timer
cards.forEach((card) => {
card.addEventListener("click", function () {
if (!disableDeck && !this.classList.contains("flip")) {
incrementFlipCounter();
}
});
});
function flipCard({ target: clickedCard }) {
if (cardOne !== clickedCard && !disableDeck) {
clickedCard.classList.add("flip");
if (!cardOne) {
return (cardOne = clickedCard);
}
cardTwo = clickedCard;
disableDeck = true;
let cardOneImg = cardOne.querySelector(".back-view img").src,
cardTwoImg = cardTwo.querySelector(".back-view img").src;
matchCards(cardOneImg, cardTwoImg);
// Start the timer based on the selected game mode
if (gameMode === "challenge" && !timerInterval) {
timerInterval = setInterval(updateTimer, 1000);
} else if (gameMode === "relaxed" && elapsedTime === 0) {
timerInterval = setInterval(updateTimer, 1000);
}
}
}
function matchCards(img1, img2) {
if (img1 === img2) {
matched++;
if (matched == 8) {
setTimeout(() => {
return resetGame();
}, 1000);
}
cardOne.removeEventListener("click", flipCard);
cardTwo.removeEventListener("click", flipCard);
cardOne = cardTwo = "";
return (disableDeck = false);
}
setTimeout(() => {
cardOne.classList.add("shake");
cardTwo.classList.add("shake");
}, 400);
setTimeout(() => {
cardOne.classList.remove("shake", "flip");
cardTwo.classList.remove("shake", "flip");
cardOne = cardTwo = "";
disableDeck = false;
}, 1200);
}
function shuffleCard() {
matched = 0;
disableDeck = false;
cardOne = cardTwo = "";
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8];
arr.sort(() => (Math.random() > 0.5 ? 1 : -1));
cards.forEach((card, i) => {
card.classList.remove("flip");
let imgTag = card.querySelector(".back-view img");
imgTag.src = `images/img-${arr[i]}.png`;
card.addEventListener("click", flipCard);
});
}
function resetGame() {
// Stop the timer interval and reset elapsed time
clearInterval(timerInterval);
elapsedTime = 0;
// Reset flip counter
flipCount = 0;
const flipCounterElement = document.getElementById("flip-count");
flipCounterElement.textContent = flipCount;
// Reset the matched count
matched = 0;
previewButton.disabled = false;
// Reset the countdown time for challenge mode
if (gameMode === "challenge") {
countdownTime = 120; // Reset the countdown time to 2 minutes (or your desired initial time)
}
// Shuffle the cards and reset their state
shuffleCard();
// Start the timer based on the selected game mode
if (gameMode === "challenge") {
timerInterval = setInterval(updateTimer, 1000);
} else if (gameMode === "relaxed" && elapsedTime === 0) {
timerInterval = setInterval(updateTimer, 1000);
}
}
const relaxedModeRadio = document.getElementById("relaxed-mode");
const challengeModeRadio = document.getElementById("challenge-mode");
relaxedModeRadio.addEventListener("change", function () {
if (gameMode !== "relaxed") {
gameMode = "relaxed";
resetGame();
}
});
challengeModeRadio.addEventListener("change", function () {
if (gameMode !== "challenge") {
gameMode = "challenge";
resetGame();
}
});
// Add a click event listener to the "Preview" button
const previewButton = document.getElementById("preview-button");
previewButton.addEventListener("click", function () {
// Check if any cards are currently flipped (game in progress)
const flippedCards = document.querySelectorAll(".card.flip");
if (flippedCards.length === 0) {
// No cards are flipped, so you can proceed with the preview
// Disable the "Preview" button to prevent multiple clicks
previewButton.disabled = true;
// Call the function to show card previews
previewCards();
} else {
// Cards are flipped, so the button remains disabled
alert("Finish your current game before using the preview feature.");
}
});
// Add a click event listener to the reset button
const resetButton = document.getElementById("reset-button");
resetButton.addEventListener("click", resetGame);
shuffleCard();
cards.forEach((card) => {
card.addEventListener("click", flipCard);
});