-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
141 lines (120 loc) · 2.86 KB
/
Copy pathscript.js
File metadata and controls
141 lines (120 loc) · 2.86 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
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
const X_CLASS = "x";
const O_CLASS = "o";
let currentPlayer = X_CLASS;
const WINNING_COMBINATIONS = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
const cells = document.querySelectorAll("[data-cell]");
const clickSound = document.getElementById("click-sound");
const board = document.querySelector(".board");
const restartButton = document.getElementById("restartButton");
let gameActive = true;
cells.forEach((cell) => {
cell.addEventListener("click", handleClick, { once: true });
});
restartButton.addEventListener("click", function () {
console.log("Restart button clicked!");
restartGame();
});
function endGame(draw) {
setTimeout(() => {
if (draw) {
alert("It's a draw!");
} else {
alert(`${currentPlayer === X_CLASS ? "Player" : "Computer"} wins!`);
}
gameActive = false;
}, 100);
}
function placeMark(cell, currentPlayer) {
if (currentPlayer === X_CLASS) {
cell.textContent = "X";
cell.classList.add(X_CLASS);
} else {
cell.textContent = "O";
cell.classList.add(O_CLASS);
}
}
function swapTurns() {
currentPlayer = currentPlayer === X_CLASS ? O_CLASS : X_CLASS;
}
function handleClick(e) {
if (!gameActive) return;
const cell = e.target;
if (cell.textContent !== "") {
return;
}
placeMark(cell, currentPlayer);
clickSound.play();
setTimeout(() => {
if (checkWin(currentPlayer)) {
endGame(false);
return;
}
if (isDraw()) {
endGame(true);
return;
}
swapTurns();
setTimeout(computerMove, 500);
}, 50);
}
function computerMove() {
const availableCells = [...cells].filter((cell) => !cell.textContent);
if (availableCells.length === 0) return;
const randomCell =
availableCells[Math.floor(Math.random() * availableCells.length)];
placeMark(randomCell, O_CLASS);
setTimeout(() => {
if (checkWin(O_CLASS)) {
endGame(false);
return;
}
if (isDraw()) {
endGame(true);
return;
}
swapTurns();
}, 50);
}
function checkWin(currentPlayer) {
return WINNING_COMBINATIONS.some((combination) => {
return combination.every((index) => {
return cells[index].classList.contains(currentPlayer);
});
});
}
function isDraw() {
return [...cells].every(
(cell) =>
cell.classList.contains(X_CLASS) || cell.classList.contains(O_CLASS)
);
}
function endGame(draw) {
setTimeout(() => {
if (draw) {
alert("It's a draw!");
} else {
alert(`${currentPlayer === X_CLASS ? "Player" : "Computer"} wins!`);
}
gameActive = false;
}, 100);
}
function restartGame() {
cells.forEach((cell) => {
cell.textContent = "";
cell.classList.remove(X_CLASS, O_CLASS);
});
currentPlayer = X_CLASS;
gameActive = true;
cells.forEach((cell) => {
cell.addEventListener("click", handleClick, { once: true });
});
}