-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
134 lines (119 loc) · 3.47 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
"use strict";
const cards = document.querySelectorAll(".card");
const imgsContainer = document.querySelectorAll(".element");
const imgs = document.querySelectorAll(".element img");
const restartBtn = document.querySelector("button");
const numberOfMoves = document.querySelector(".moves");
const bestScore = document.querySelector(".best--score");
const bestTime = document.querySelector(".best--time");
const time = document.querySelector(".time");
const totalTime = document.querySelector(".tot-time");
const totalMoves = document.querySelector(".tot-moves");
const done = document.querySelector(".done");
const again = document.querySelector(".again");
let activeCard,
numberOfActiveCards = 0,
solved = 0,
moves = 0,
best = -1,
best_Time = -1,
intervalId = null,
elapsedTime = null;
const startTimer = () => {
if (intervalId) {
clearInterval(intervalId);
}
elapsedTime = 0;
time.textContent = elapsedTime;
intervalId = setInterval(() => {
elapsedTime++;
time.textContent = elapsedTime;
}, 1000);
};
const updateMoves = (moves) => {
numberOfMoves.textContent = moves;
};
const generateRandomimgs = () => {
let arr = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8];
arr = arr.sort(() => Math.random() - 0.5);
for (let i = 0; i < 16; i++) {
imgs[i].src = `css/imgs/${arr[i]}.jpg`;
}
};
const init = () => {
generateRandomimgs();
imgsContainer.forEach((img) => {
img.style.opacity = 0;
img.style.transform = "translate(-50%, -50%) rotateY(-180deg)";
});
cards.forEach((card) => {
card.style.transform = " translate(-50%, -50%) rotateY(180deg)";
});
startTimer();
numberOfMoves.textContent = "0";
solved = 0;
if (best === -1) {
bestScore.textContent = "";
} else bestScore.textContent = best;
if (best_Time === -1) {
bestTime.textContent = "";
} else bestTime.textContent = best_Time;
done.classList.add("hidden");
moves = 0;
};
restartBtn.addEventListener("click", () => {
init();
});
init();
cards.forEach((card) =>
card.addEventListener("click", (e) => {
if (Number(card.children[0].style.opacity) === 0) {
card.style.transform = " translate(-50%, -50%) rotateY(-180deg)";
card.children[0].style.transform =
"translate(-50%, -50%) rotateY(-180deg)";
card.children[0].style.opacity = 1;
moves++;
numberOfActiveCards++;
updateMoves(moves);
if (numberOfActiveCards === 1) activeCard = card;
else {
numberOfActiveCards = 0;
if (
activeCard.children[0].children[0].src ===
card.children[0].children[0].src
) {
activeCard.children[0].style.opacity = "50%";
card.children[0].style.opacity = "50%";
solved++;
} else {
setTimeout(() => {
card.style.transform = " translate(-50%, -50%) rotateY(180deg)";
card.children[0].style.transform =
"translate(-50%, -50%) rotateY(-180deg)";
card.children[0].style.opacity = 0;
activeCard.style.transform =
" translate(-50%, -50%) rotateY(180deg)";
activeCard.children[0].style.transform =
"translate(-50%, -50%) rotateY(-180deg)";
activeCard.children[0].style.opacity = 0;
}, 600);
}
}
if (solved === 8) {
solved = 0;
clearInterval(intervalId);
if (best === -1 || moves < best) {
best = moves;
}
if (best_Time === -1 || elapsedTime < best_Time)
best_Time = elapsedTime;
totalMoves.textContent = moves;
totalTime.textContent = elapsedTime;
setTimeout(() => {
done.classList.remove("hidden");
}, 500);
again.addEventListener("click", init);
}
}
})
);