-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (104 loc) · 2.94 KB
/
script.js
File metadata and controls
132 lines (104 loc) · 2.94 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
const gameBoard = document.getElementById("gameBoard");
const movesCounter = document.getElementById("moves");
const timeCounter = document.getElementById("time");
const restartBtn = document.getElementById("restart");
const emojis = ["🍎", "🍌", "🍇", "🍓", "🍒", "🥝", "🍍", "🍉"];
let cards = [];
let firstCard = null;
let secondCard = null;
let lockBoard = false;
let moves = 0;
let matchedPairs = 0;
let timer = null;
let seconds = 0;
let timerStarted = false;
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const randomIndex = Math.floor(Math.random() * (i + 1));
[array[i], array[randomIndex]] = [array[randomIndex], array[i]];
}
return array;
}
function startTimer() {
if (timerStarted) return;
timerStarted = true;
timer = setInterval(() => {
seconds++;
timeCounter.textContent = seconds;
}, 1000);
}
// ==============================
// SETUP GAME
// ==============================
function setupGame() {
// Reset everything
gameBoard.innerHTML = "";
moves = 0;
matchedPairs = 0;
seconds = 0;
timerStarted = false;
clearInterval(timer);
movesCounter.textContent = moves;
timeCounter.textContent = seconds;
firstCard = null;
secondCard = null;
lockBoard = false;
// Prepare cards
cards = shuffle([...emojis, ...emojis]);
// Create cards dynamically
cards.forEach((emoji) => {
const card = document.createElement("div");
card.classList.add("card");
card.dataset.emoji = emoji;
card.textContent = "❓";
card.addEventListener("click", () => flipCard(card));
gameBoard.appendChild(card);
});
}
function flipCard(card) {
if (lockBoard) return;
if (card === firstCard) return;
if (card.classList.contains("matched")) return;
startTimer();
card.textContent = card.dataset.emoji;
card.classList.add("flipped");
if (!firstCard) {
firstCard = card;
return;
}
secondCard = card;
moves++;
movesCounter.textContent = moves;
checkMatch();
}
function checkMatch() {
const isMatch = firstCard.dataset.emoji === secondCard.dataset.emoji;
if (isMatch) {
firstCard.classList.add("matched");
secondCard.classList.add("matched");
matchedPairs++;
resetCards();
if (matchedPairs === emojis.length) {
clearInterval(timer);
setTimeout(() => {
alert(`🎉 Congratulations! You won in ${moves} moves and ${seconds} seconds!`);
}, 300);
}
} else {
lockBoard = true;
setTimeout(() => {
firstCard.textContent = "❓";
secondCard.textContent = "❓";
firstCard.classList.remove("flipped");
secondCard.classList.remove("flipped");
resetCards();
}, 1000);
}
}
function resetCards() {
firstCard = null;
secondCard = null;
lockBoard = false;
}
restartBtn.addEventListener("click", setupGame);
setupGame();