-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindrx.js
More file actions
110 lines (109 loc) · 2.67 KB
/
indrx.js
File metadata and controls
110 lines (109 loc) · 2.67 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
let minut = 1,
timden,
timm = minut * 60;
let time = 1000;
let winn = 0;
let titr = document.getElementsByTagName("span");
let bloksCelictor = document.querySelector(".game");
let bloks = Array.from(bloksCelictor.children);
let ordarRang = [...Array(bloks.length).keys()];
shuffel(ordarRang);
bloks.forEach((blok, i) => {
blok.style.order = ordarRang[i];
blok.addEventListener("click", () => {
flip(blok);
});
});
function shuffel(array) {
let current = array.length,
temp,
random;
while (current > 0) {
random = Math.floor(Math.random() * current);
current--;
temp = array[current];
array[current] = array[random];
array[random] = temp;
}
return array[current];
}
function flip(celiktBlock) {
celiktBlock.classList.add("is-flip");
let allflip = bloks.filter((flipBlock) =>
flipBlock.classList.contains("is-flip")
);
if (allflip.length === 2) {
stopckik();
setTimeout(() => {
chekblok(allflip[0], allflip[1]);
}, time);
}
}
function stopckik() {
bloksCelictor.classList.add("no-cklik");
setTimeout(() => {
bloksCelictor.classList.remove("no-cklik");
}, time);
}
function chekblok(firsBlock, secandBlock) {
if (firsBlock.innerHTML === secandBlock.innerHTML) {
firsBlock.classList.remove("is-flip");
secandBlock.classList.remove("is-flip");
firsBlock.classList.add("win");
secandBlock.classList.add("win");
winn++;
if (winn === 8) {
console.log("a");
win();
}
} else {
firsBlock.classList.remove("is-flip");
secandBlock.classList.remove("is-flip");
timm -= 5;
titr[1].classList.remove("hide");
setTimeout(() => {
titr[1].classList.add("hide");
}, 500);
}
}
bloksCelictor.addEventListener("click", () => {
if (titr[0].innerHTML === "Memory Game") {
firsmemory();
timden = setInterval(() => {
timer();
}, 1000);
}
});
function timer() {
let min = Math.floor(timm / 60),
sec = timm % 60;
titr[0].innerHTML = min + ":" + sec;
if (timm > 0) {
timm--;
} else {
clearInterval(timden);
gameOver();
}
}
function gameOver() {
bloksCelictor.classList.add("no-cklik");
titr[0].innerHTML = "game over";
setTimeout(() => {
location.reload();
}, 3000);
}
function win() {
bloksCelictor.classList.add("no-cklik");
document.getElementsByTagName("h3")[0].innerHTML = "winner";
setTimeout(() => {
location.reload();
}, 3000);
}
function firsmemory() {
bloks.forEach((element) => {
element.classList.add("is-flip");
setTimeout(() => {
element.classList.remove("is-flip");
}, 2000);
});
}