-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (125 loc) · 4.62 KB
/
index.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
document.addEventListener("DOMContentLoaded", () => {
const board = document.getElementById("board");
const cells = document.querySelectorAll(".cell");
const playerXInput = document.getElementById("playerX");
const playerOInput = document.getElementById("playerO");
const startButton = document.getElementById("start-button");
const winnerMessage = document.getElementById("winner-message");
const performanceBox = document.getElementById("performance-box");
const restartButton = document.getElementById("restart-button");
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],
];
let currentPlayer = "X";
let isGameOver = false;
let playerXName = "";
let playerOName = "";
let playerXWins = 0;
let playerOWins = 0;
function checkWin() {
for (const combination of WINNING_COMBINATIONS) {
const [a, b, c] = combination;
if (cells[a].textContent && cells[a].textContent === cells[b].textContent && cells[a].textContent === cells[c].textContent) {
isGameOver = true;
return combination;
}
}
return null;
}
function checkDraw() {
for (const cell of cells) {
if (!cell.textContent) {
return false;
}
}
isGameOver = true;
return true;
}
function handleWin(winner, combination) {
if (winner === "X") {
playerXWins++;
} else if (winner === "O") {
playerOWins++;
}
isGameOver = true;
winnerMessage.textContent = `${winner === "X" ? playerXName : playerOName} is the Winner!`;
performanceBox.textContent = `Player X Wins: ${playerXWins} | Player O Wins: ${playerOWins}`;
performanceBox.classList.remove("hidden");
restartButton.classList.remove("hidden");
for (const index of combination) {
cells[index].classList.add("winner");
}
}
function restartGame() {
cells.forEach((cell) => {
cell.textContent = "";
cell.classList.remove("winner");
});
currentPlayer = "X";
isGameOver = false;
winnerMessage.textContent = "";
performanceBox.classList.add("hidden");
restartButton.classList.add("hidden");
board.style.display = "none";
playerXInput.value = "";
playerOInput.value = "";
document.getElementById("player-names").style.display = "flex";
winnerMessage.classList.add("hidden");
startButton.classList.remove("hidden");
}
function handleClick(event) {
const cell = event.target;
if (!cell.textContent && !isGameOver) {
cell.textContent = currentPlayer;
const winningCombination = checkWin();
if (winningCombination) {
handleWin(currentPlayer, winningCombination);
} else if (checkDraw()) {
winnerMessage.textContent = "It's a draw!";
winnerMessage.classList.remove("hidden");
restartButton.classList.remove("hidden");
} else {
currentPlayer = currentPlayer === "X" ? "O" : "X";
}
}
}
function handleCellHover(event) {
const cell = event.target;
if (!cell.textContent && !isGameOver) {
cell.style.backgroundColor = "#eee";
}
}
function handleCellLeave(event) {
const cell = event.target;
if (!cell.textContent && !isGameOver) {
cell.style.backgroundColor = "#f0f0f0";
}
}
function handleStartButtonClick() {
playerXName = playerXInput.value || "Player X";
playerOName = playerOInput.value || "Player O";
if (playerXName === playerOName) {
playerXName += " (X)";
playerOName += " (O)";
} else {
playerXName += " (X)";
playerOName += " (O)";
}
document.getElementById("player-names").style.display = "none";
board.style.display = "grid";
}
cells.forEach((cell) => {
cell.addEventListener("click", handleClick);
cell.addEventListener("mouseenter", handleCellHover);
cell.addEventListener("mouseleave", handleCellLeave);
});
startButton.addEventListener("click", handleStartButtonClick);
restartButton.addEventListener("click", restartGame);
});