Repository navigation
Expand file tree
/
Copy pathscriptessix.js
More file actions
250 lines (217 loc) · 6.81 KB
/
Copy pathscriptessix.js
File metadata and controls
250 lines (217 loc) · 6.81 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
class MemoryGame {
constructor(gameContainer, backFace, cardsData, cardsSelect) {
this.gameContainer = gameContainer;
this.backFace = backFace;
this.cardsData = cardsData;
this.cardsSelect = cardsSelect;
this.hasFlippedCard = false;
this.lockBoard = false;
this.firstCard = null;
this.secondCard = null;
this.pairedCards = 0;
this.timer = null;
this.startTime = null;
this.bestTime = localStorage.getItem("bestTime") || null;
this.displayBestTime();
this.init();
}
init() {
//adding eventlistener to gamecontainer instead of on card
this.gameContainer.addEventListener("click", (event) => {
// Check if the clicked element is a memory card
if (event.target.closest(".memory-card")) {
this.flipCard(event.target.closest(".memory-card"));
}
});
this.updateCards();
this.hideModal();
}
generateCards(filteredCardsData) {
console.log("Filtered cards length:", filteredCardsData.length);
const pairedCardsData = filteredCardsData.concat(filteredCardsData);
const shuffledCardsData = this.shuffle(pairedCardsData);
this.gameContainer.innerHTML = shuffledCardsData
.map(
(cardData) => `
<div class="memory-card" data-framework="${cardData.framework}">
<img class="front-face" src="${cardData.frontFace}" alt="${cardData.framework}">
<img class="back-face" src="${this.backFace}" alt="Memory Card">
</div>
`
)
.join("");
this.totalPairs = filteredCardsData.length; // Set the total number of pairs
}
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;
}
flipCard(card) {
if (this.lockBoard) return;
if (card === this.firstCard) return;
card.classList.add("flip");
if (!this.hasFlippedCard) {
// Start timer on first flip
this.startTimer();
this.hasFlippedCard = true;
this.firstCard = card;
} else {
this.secondCard = card;
this.checkForMatch();
}
}
checkForMatch() {
let isMatch =
this.firstCard.dataset.framework === this.secondCard.dataset.framework;
isMatch ? this.disableCards() : this.unflipCards();
}
disableCards() {
this.firstCard.removeEventListener("click", this.flipCard);
this.secondCard.removeEventListener("click", this.flipCard);
this.pairedCards += 1; // Increment the count of matched pairs
if (this.pairedCards === this.totalPairs) {
setTimeout(() => {
this.showModal();
}, 1000);
}
this.resetBoard();
}
showModal() {
clearInterval(this.timer); // Stop the timer
const usedTime = Date.now() - this.startTime;
const seconds = Math.floor(usedTime / 1000);
this.updateBestTime(seconds);
const timeSpentElement = document.getElementById("timeSpent");
timeSpentElement.textContent = `${seconds}`;
const modal = document.getElementById("modal");
modal.style.display = "block";
// Reset the timer variable
this.timer = null;
// Ensure the event listener for the new game button is attached
// (This is assuming the button is always present in the modal and doesn't get dynamically added/removed)
const newGameBtn = document.getElementById("newGameBtn");
newGameBtn.onclick = () => this.resetGame();
}
updateBestTime(currentTime) {
if (this.bestTime === null || currentTime < this.bestTime) {
this.bestTime = currentTime;
localStorage.setItem("bestTime", currentTime);
this.displayBestTime();
}
}
displayBestTime() {
const bestTimeElement = document.getElementById("bestTime");
if (bestTimeElement) {
bestTimeElement.textContent = `Best Time: ${
this.bestTime || "N/A"
} seconds`;
}
}
unflipCards() {
this.lockBoard = true;
setTimeout(() => {
this.firstCard.classList.remove("flip");
this.secondCard.classList.remove("flip");
this.resetBoard();
}, 1500);
}
startTimer() {
if (this.timer === null) {
this.startTime = Date.now();
this.timer = setInterval(() => {
const usedTime = Date.now() - this.startTime;
const seconds = Math.floor(usedTime / 1000);
document.getElementById("timer").innerText = `Time: ${seconds} seconds`;
}, 1000);
}
}
resetBoard() {
[this.hasFlippedCard, this.lockBoard] = [false, false];
[this.firstCard, this.secondCard] = [null, null];
}
updateCards() {
// Reset the game state if a game is in progress
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
this.pairedCards = 0;
this.hasFlippedCard = false;
this.lockBoard = false;
this.firstCard = null;
this.secondCard = null;
}
const numberOfCards = parseInt(this.cardsSelect.value);
const filteredCardsData = this.cardsData.slice(0, numberOfCards / 2); // Divide by 2 because each card appears twice
this.generateCards(filteredCardsData);
// Update the timer display to 0
const timerElement = document.getElementById("timer");
if (timerElement) {
timerElement.textContent = "Time: 0 seconds";
}
this.pairedCards = 0; // Reset the paired cards count
}
resetGame() {
clearInterval(this.timer); // Clear the timer
this.timer = null;
this.hideModal();
this.pairedCards = 0;
this.updateCards(); // Regenerate cards based on current selection
// this.startTimer(); // Restart the timer
//document.getElementById("timer").innerText = "Time: 0 seconds";
}
hideModal() {
const modal = document.getElementById("modal");
modal.style.display = "none";
}
}
// Initialize the game
const gameContainer = document.querySelector(".memory-game");
const cardsSelect = document.getElementById("cards");
const backFace = "img/js-badge.svg";
const cardsData = [
{
framework: "aurelia",
frontFace: "img/aurelia.svg",
},
{
framework: "vue",
frontFace: "img/vue.svg",
},
{
framework: "angular",
frontFace: "img/angular.svg",
},
{
framework: "backbone",
frontFace: "img/backbone.svg",
},
{
framework: "ember",
frontFace: "img/ember.svg",
},
{
framework: "react",
frontFace: "img/react.svg",
},
];
const memoryGame = new MemoryGame(
gameContainer,
backFace,
cardsData,
cardsSelect
);
// Event listener for the update button
const updateButton = document.getElementById("btn");
updateButton.addEventListener("click", () => memoryGame.updateCards());
// Event listener for the new game button
const newGameBtn = document.getElementById("newGameBtn");
newGameBtn.addEventListener("click", () => memoryGame.resetGame());