-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
126 lines (111 loc) · 3.21 KB
/
script.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
const cells = document.querySelectorAll(".cell");
const message = document.getElementById("message");
const overlay = document.getElementById("overlay");
const restartBtn = document.getElementById("btn-restart");
const quitBtn = document.getElementById("btn-quit");
const clickAudio = document.getElementById("click");
const gameoverAudio = document.getElementById("gameover");
let currentTurn = "Player 01";
const wins = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
let wonArr;
cells.forEach((cell) => {
cell.addEventListener("mouseenter", hoverIn);
cell.addEventListener("mouseleave", hoverOut);
cell.addEventListener("click", action, { once: true });
});
restartBtn.addEventListener("click", restart);
quitBtn.addEventListener("click", quit);
function restart() {
message.innerText = "Player 01's Turn";
overlay.classList.remove("active");
cells.forEach((cell) => {
cell.addEventListener("mouseenter", hoverIn);
cell.addEventListener("mouseleave", hoverOut);
cell.classList.remove("cross");
cell.classList.remove("cross-hover");
cell.classList.remove("circle");
cell.classList.remove("circle-hover");
cell.classList.remove("highlight");
cell.removeEventListener("click", action);
cell.addEventListener("click", action, { once: true });
cell.style.cursor = "pointer";
});
}
function quit() {
window.close();
}
function action() {
let currentClass = currentTurn === "Player 01" ? "cross" : "circle";
this.classList.remove(`${currentClass}-hover`);
this.classList.add(currentClass);
clickAudio.play();
if (isWinner(currentClass)) {
message.innerText = `${currentTurn} Won !!!`;
wonArr.forEach((i) => cells[i].classList.add("highlight"));
reset();
return;
} else {
const res = Array.from(cells).every((cell) => {
return cell.classList.length === 2;
});
if (res) {
message.innerText = `It is a TIE !!!`;
cells.forEach((cell) => {
cell.classList.add("highlight");
});
reset();
return;
}
}
currentTurn === "Player 01"
? (currentTurn = "Player 02")
: (currentTurn = "Player 01");
message.innerText = `${currentTurn}'s Turn!`;
}
function isWinner(curClass) {
return wins.some((win) => {
const res = win.every((i) => cells[i].classList.contains(curClass));
if (res) {
wonArr = win;
}
return res;
});
}
function hoverIn() {
let currentClass = currentTurn === "Player 01" ? "cross" : "circle";
if (this.classList.contains("cross") || this.classList.contains("circle")) {
this.style.cursor = "not-allowed";
} else {
this.classList.add(`${currentClass}-hover`);
}
}
function hoverOut() {
if (
this.classList.contains("cross-hover") ||
this.classList.contains("circle-hover")
) {
this.classList.remove("cross-hover");
this.classList.remove("circle-hover");
}
}
function reset() {
cells.forEach((cell) => {
cell.removeEventListener("mouseenter", hoverIn);
cell.removeEventListener("mouseleave", hoverOut);
cell.removeEventListener("click", action);
cell.style.cursor = "not-allowed";
});
gameoverAudio.play();
setTimeout(() => {
overlay.classList.add("active");
}, 2000);
}