-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
145 lines (121 loc) · 3.57 KB
/
main.js
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
const images = [
"./img/img-1.png",
"./img/img-2.png",
"./img/img-3.png",
"./img/img-4.png",
"./img/img-5.png",
"./img/img-6.png",
"./img/img-7.png",
"./img/img-8.png",
];
let timerInterval=null
let secondsRemaining = 60;
const shuffleArray = (array) => {
return array.sort(() => (Math.random() > 0.5 ? 1 : -1));
};
const random = shuffleArray([...images, ...images]);
const cardList = document.querySelector(".cards");
const renderCards = () => {
random.forEach((img) => {
const markup = `
<li class="card">
<div class="view front-card"></div>
<div class="view back-card">
<img src="${img}" alt="card-image">
</div>
</li>
`;
cardList.insertAdjacentHTML("afterbegin", markup);
});
updateTimer();
};
renderCards();
let cardOne, cardTwo;
let disableCard = false;
let matchedPairs = 0;
function displayResultMessage(isWin) {
const message = document.querySelector(".message");
const messageContainer = document.querySelector(".message-container");
message.textContent = isWin ? "You won!" : "You lost!";
message.style.display = "block";
messageContainer.style.display = "flex";
}
const matchCards = (img1, img2) => {
if (img1 === img2) {
matchedPairs++;
if (matchedPairs == 8) {
clearInterval(timerInterval);
displayResultMessage(true);
}
cardOne.removeEventListener("click", flipCard);
cardTwo.removeEventListener("click", flipCard);
cardOne = cardTwo = "";
disableCard = false;
} else {
setTimeout(() => {
cardOne.classList.add("vibration");
cardTwo.classList.add("vibration");
}, 500);
setTimeout(() => {
cardOne.classList.remove("vibration", "flip");
cardTwo.classList.remove("vibration", "flip");
cardOne = cardTwo = "";
disableCard = false;
}, 1000);
}
};
let gameOver = false;
const resetAllCards = () => {
matchedPairs = 0;
cardOne = cardTwo = "";
cardList.innerHTML = "";
random.sort(() => (Math.random() > 0.5 ? 1 : -1));
renderCards();
// Clear the timer
clearInterval(timerInterval);
timerInterval = null;
secondsRemaining = 60;
updateTimer();
gameOver = false;
const messageContainer = document.querySelector(".message-container");
messageContainer.style.display = "none";
};
function updateTimer() {
const timerDisplay = document.querySelector(".timer");
const minutes = Math.floor(secondsRemaining / 60);
const seconds = secondsRemaining % 60;
// Display the time in MM:SS format
timerDisplay.textContent = `${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
}
function flipCard(e) {
if (gameOver) return;
const card = e.target.closest(".card");
if (!card) return;
// Check if the timer is not running and the game has not been won or lost
if (timerInterval === null && matchedPairs !== 8) {
timerInterval = setInterval(() => {
secondsRemaining--;
if (secondsRemaining <= 0) {
clearInterval(timerInterval);
displayResultMessage(false);
gameOver = true;
}
updateTimer();
}, 1000);
}
// Rest of your flipCard logic
if (card !== cardOne && !disableCard) {
card.classList.add("flip");
if (!cardOne) {
return (cardOne = card);
}
cardTwo = card;
disableCard = true;
const imgOne = cardOne.querySelector("img").src;
const imgTwo = cardTwo.querySelector("img").src;
matchCards(imgOne, imgTwo);
}
}
cardList.addEventListener("click", flipCard);
const resetButton = document.querySelectorAll(".btn");
resetButton.forEach((btn) => btn.addEventListener("click", resetAllCards));