-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
143 lines (135 loc) · 3.66 KB
/
app.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
let sec = document.querySelector("section");
let arr;
let c;
let h2 = document.querySelector("h2");
let hr1 = document.querySelector("#hr1");
let hr2 = document.querySelector("#hr2");
let a;
let winingSound = new Audio("winingSound.mp3");
let ding = new Audio("ding.wav");
let dong = new Audio("dong.wav");
reset();
sec.addEventListener("mouseover", function (e) {
let div = e.target;
if (div.classList.contains("notFixed")) {
if (c % 2) div.innerHTML = "X";
else div.innerHTML = "O";
}
});
sec.addEventListener("mouseout", function (e) {
let div = e.target;
if (div.classList.contains("notFixed")) {
div.innerHTML = "";
}
});
sec.addEventListener("click", function (e) {
let div = e.target;
if (div.classList.contains("notFixed")) {
if (c % 2) {
div.innerHTML = "X";
ding.play();
} else {
div.innerHTML = "O";
dong.play();
}
div.style.cursor = "not-allowed";
arr[Math.floor(div.id / 3)][div.id % 3] = div.innerHTML;
if (check(arr)) {
winingSound.play();
h2.innerHTML = check(arr)[0] + " wins";
document.querySelectorAll("div").forEach(function (div) {
div.classList.remove("notFixed");
});
if (check(arr)[2] == "h") {
hr1.style.top = `${i * 80 + 40}px`;
hr1.style.display = "inherit";
} else if (check(arr)[2] == "v") {
hr1.style.transform = "rotate(90deg)";
hr1.style.left = `${i * 80 + 40}px`;
hr1.style.display = "inherit";
} else if (check(arr)[2] == "d1") {
hr1.style.transform = "rotate(45deg)";
hr1.style.width = "140%";
hr1.style.display = "inherit";
} else {
hr2.style.display = "inherit";
}
a = setTimeout(reset, 3000);
console.log(a);
}
div.classList.remove("notFixed");
c++;
}
});
function check(arr) {
let horizontal = [];
let vertical = ["", "", ""];
let dia1 = "";
let dia2 = "";
for (let i = 0; i < arr.length; i++) {
horizontal[i] = arr[i].join("");
for (let j = 0; j < arr.length; j++) {
if (i == j) dia1 += arr[i][j];
if (i + j == 2) dia2 += arr[i][j];
vertical[j] = vertical[j] + arr[i][j];
}
}
// console.log(horizontal);
// console.log(vertical);
// console.log(dia1, dia2);
for (i = 0; i < 3; i++) {
if (horizontal[i] == "XXX" || horizontal[i] == "OOO") {
return [horizontal[i][0], i, "h"];
} else if (vertical[i] == "XXX" || vertical[i] == "OOO") {
return [vertical[i][0], i, "v"];
}
}
if (dia1 == "XXX" || dia1 == "OOO") {
return [dia1[0], 1, "d1"];
} else if (dia2 == "XXX" || dia2 == "OOO") {
console.log("O wins");
return [dia2[0]];
}
if (
!arr
.map((i) => i.join(""))
.join("")
.includes("-")
) {
h2.innerHTML = "Game Draw!!!!";
a = setTimeout(reset, 3000);
}
}
function reset() {
c = 1;
// console.log(c);
clearTimeout(a);
arr = [
["-", "-", "-"],
["-", "-", "-"],
["-", "-", "-"],
];
sec.innerHTML = "";
hr1.style.display = "none";
hr1.style.top = 0;
hr1.style.left = 0;
hr1.style.width = "100%";
hr1.style.transform = "rotate(0deg)";
hr1.style.transformOrigin = "0";
hr2.style.display = "none";
h2.innerHTML = "";
for (let i = 0; i < 9; i++) {
sec.innerHTML += `<div class="notFixed" id=${i}></div>`;
}
}
let h1 = document.querySelector("h1");
let span = document.querySelector("h1 > span");
span.addEventListener("click", function () {
span.classList.toggle("span");
h1.classList.toggle("h1");
document.body.classList.toggle("body");
sec.classList.toggle("sec");
h2.classList.toggle("h2");
});
let btn = document.querySelector("button");
btn.addEventListener("click", reset);