-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
190 lines (170 loc) · 5.52 KB
/
script.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
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
// const cardValues = [];
// const cards = document.querySelectorAll(".card");
// const flipCounter = document.querySelector(".flip-counter");
// const timeCounter = document.querySelector(".time-counter");
// let firstCard = null;
// let secondCard = null;
// let matchedCards = 0;
// let flipCount = 0;
// let startTime = null;
// let endTime = null;
// // Create pairs of numbers from 1 to 20
// for (let i = 1; i <= 20; i++) {
// cardValues.push(i);
// cardValues.push(i);
// }
// // Shuffle the array
// for (let i = cardValues.length - 1; i > 0; i--) {
// const j = Math.floor(Math.random() * (i + 1));
// [cardValues[i], cardValues[j]] = [cardValues[j], cardValues[i]];
// }
// for (let i = 0; i < cards.length; i++) {
// cards[i].innerHTML = '<span class="card-number">' + cardValues[i] + '</span>';
// cards[i].setAttribute("data-value", cardValues[i]); // add data-value attribute
// cards[i].addEventListener("click", function () {
// // If the first card has not been selected yet
// if (firstCard === null) {
// firstCard = this;
// this.classList.toggle("flip");
// }
// // If the first card has already been selected, but the second card has not
// else if (secondCard === null) {
// secondCard = this;
// this.classList.toggle("flip");
// flipCount++
// // Check if the two cards match
// if (firstCard.dataset.value === secondCard.dataset.value) {
// firstCard = null;
// secondCard = null;
// matchedCards += 2;
// // Check if all cards have been matched
// if (matchedCards === cards.length) {
// alert(`Congratulations! You have won the game in ${flipCount} flips.`);
// }
// }
// // If two cards have already been selected, but they don't match
// else {
// setTimeout(function () {
// firstCard.classList.remove("flip");
// secondCard.classList.remove("flip");
// firstCard = null;
// secondCard = null;
// }, 1000);
// }
// }
// });
// }
// console.log(flipCount)
const cardValues = [];
const cards = document.querySelectorAll(".card");
const flipCounter = document.querySelector(".flip-counter");
const timeCounter = document.querySelector(".time-counter");
const rulesLink = document.querySelectorAll(".rules-link");
const popup = document.querySelector(".pop-up");
const closePopup = document.querySelector(".xmark");
const restartGame = document.querySelector(".restart-btn");
let firstCard = null;
let secondCard = null;
let matchedCards = 0;
let flipCount = 0;
let startTime = null;
let endTime = null;
// Create pairs of numbers from 1 to 20
for (let i = 1; i <= 20; i++) {
cardValues.push(i);
cardValues.push(i);
}
// Shuffle the array
for (let i = cardValues.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[cardValues[i], cardValues[j]] = [cardValues[j], cardValues[i]];
}
function startGame() {
// Reset counters
flipCount = 0;
startTime = null;
endTime = null;
flipCounter.textContent = "Flipped: 0";
timeCounter.textContent = "Time: 0s";
// Shuffle the cards
for (let i = cards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
cards[i].style.order = j;
}
// Add event listeners to the cards
for (let i = 0; i < cards.length; i++) {
cards[i].innerHTML =
'<span class="card-number">' + cardValues[i] + "</span>";
cards[i].setAttribute("data-value", cardValues[i]); // add data-value attribute
cards[i].classList.remove("flip");
cards[i].addEventListener("click", flipCard);
}
}
function flipCard() {
// Start the timer if this is the first flip
if (startTime === null) {
startTime = new Date();
setInterval(updateTimeCounter, 1000);
}
// If the card is already flipped, don't do anything
if (this.classList.contains("flip")) {
return;
}
// If the first card has not been selected yet
if (firstCard === null) {
firstCard = this;
this.classList.toggle("flip");
}
// If the first card has already been selected, but the second card has not
else if (secondCard === null) {
secondCard = this;
this.classList.toggle("flip");
flipCount++;
// Update the flip counter
flipCounter.textContent = `Flipped: ${flipCount}`;
// Check if the two cards match
if (firstCard.dataset.value === secondCard.dataset.value) {
firstCard = null;
secondCard = null;
matchedCards += 2;
// Check if all cards have been matched
if (matchedCards === cards.length) {
endTime = new Date();
setTimeout(function () {
alert(
`Congratulations! You have won the game in ${flipCount} flips and ${Math.floor(
(endTime - startTime) / 1000
)}s`
);
startGame();
}, 500);
}
}
// If two cards have already been selected, but they don't match
else {
setTimeout(function () {
firstCard.classList.remove("flip");
secondCard.classList.remove("flip");
firstCard = null;
secondCard = null;
}, 1000);
}
}
}
function updateTimeCounter() {
if (startTime !== null && endTime === null) {
const timeElapsed = new Date() - startTime;
const secondsElapsed = Math.floor(timeElapsed / 1000);
timeCounter.textContent = `Time: ${secondsElapsed}s`;
}
}
startGame();
rulesLink.forEach(function (link) {
link.addEventListener("click", function () {
popup.classList.add("active");
});
});
closePopup.addEventListener("click", function () {
popup.classList.remove("active");
});
restartGame.addEventListener("click", startGame);