-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
192 lines (166 loc) · 5.29 KB
/
Copy pathgame.js
File metadata and controls
192 lines (166 loc) · 5.29 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
import Card from "./card.js";
import Modal from "./modal.js";
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.modal = new Modal();
}
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) {
const pairedCardsData = filteredCardsData.concat(filteredCardsData);
const shuffledCardsData = this.shuffle(pairedCardsData);
this.gameContainer.innerHTML = shuffledCardsData
.map((cardData) => {
return new Card({ ...cardData, backFace: this.backFace }).getHtml();
})
.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() {
// Adjusted to use the Modal class
const usedTime = Date.now() - this.startTime;
const seconds = Math.floor(usedTime / 1000);
this.updateBestTime(seconds);
this.modal.show(seconds);
clearInterval(this.timer);
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();
}
hideModal() {
this.modal.hide();
}
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
}
}
export default MemoryGame;