-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
95 lines (83 loc) · 2.07 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
let randomarray = [];
let points = 0;
let i = 0;
let clickedtiles = [];
function soundclick() {
var audio = new Audio();
audio.src = "mixkit-arcade-game-jump-coin-216.wav";
audio.play();
}
function gameover() {
var audio = new Audio();
audio.src = "mixkit-piano-game-over-1941.wav";
audio.play();
}
function clicked(Event) {
soundclick();
let tile = Event.target.getAttribute("id");
clickedtiles.push(tile);
console.log(clickedtiles);
if (clickedtiles.length == randomarray.length) {
console.log("length equal");
checker();
}
}
function checker2() {
for (let k = 0; k < randomarray.length; k++) {
if (randomarray[k] === parseInt(clickedtiles[k])) {
if (k + 1 === randomarray.length) {
return true;
} else {
continue;
}
} else {
return false;
}
}
}
function checker() {
const ans = clickedtiles.every((i) => {
return randomarray.includes(parseInt(i));
});
if (randomarray.length === clickedtiles.length) {
console.log("if is true");
if (checker2()) {
while (clickedtiles.length) {
clickedtiles.pop();
}
console.log("checker2 is true");
points++;
console.log(points);
random();
} else {
gameover();
alert("game over points scored:" + points);
}
}
}
function anime(randomarray) {
for (let i = 0; i < randomarray.length; i++) {
setTimeout(() => {
document.getElementById(randomarray[i]).classList.remove("animate");
setTimeout(() => {
document.getElementById(randomarray[i]).classList.add("animate");
}, 2000);
}, 1000);
}
}
function random() {
let random_no = Math.floor(Math.random() * 36);
if (randomarray.includes(random_no)) {
random();
} else {
randomarray.push(random_no);
console.log(randomarray);
anime(randomarray);
}
}
for (let j = 0; j < 36; j++) {
document.querySelectorAll(".cell").forEach((item) => {
item.addEventListener("click", clicked);
});
}
random();