-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
101 lines (82 loc) · 2.09 KB
/
Copy pathscript.js
File metadata and controls
101 lines (82 loc) · 2.09 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
function startGame() {
const boxes = document.querySelectorAll(".box");
const scoreXp = document.querySelector(".score-one");
const scoreOp = document.querySelector(".score-two");
let currentSymbol = true;
let gameGoing = true;
let scoreX = 0;
let scoreO = 0;
const winningCombos = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
function placeSymbol() {
boxes.forEach((box) => box.addEventListener("click", handleBoxClick));
}
function handleBoxClick(e) {
const box = e.target;
if (!gameGoing || box.textContent !== "") return;
box.textContent = currentSymbol ? "X" : "O";
box.classList.add(currentSymbol ? "blue" : "red");
currentSymbol = !currentSymbol;
checkWinner();
}
function checkWinner() {
const board = Array.from(boxes).map((box) => box.textContent);
for (let [a, b, c] of winningCombos) {
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
handleWin(board[a]);
return;
}
}
if (board.every((cell) => cell !== "")) {
handleDraw();
}
}
function handleWin(player) {
alert(`${player} is the Winner!`);
gameGoing = false;
if (player === "X") scoreX++;
else scoreO++;
updateScoreDisplay();
}
function handleDraw() {
alert("It's a draw. Board reset.");
resetGameBoard();
}
function resetGameBoard() {
gameGoing = true;
currentSymbol = true;
boxes.forEach((box) => {
box.textContent = "";
box.classList.remove("blue", "red");
});
}
function resetScores() {
scoreX = 0;
scoreO = 0;
updateScoreDisplay();
}
function updateScoreDisplay() {
scoreXp.textContent = `X = ${scoreX}`;
scoreOp.textContent = `O = ${scoreO}`;
}
function addResetListeners() {
document
.getElementById("reset-board")
.addEventListener("click", resetGameBoard);
document
.getElementById("reset-score")
.addEventListener("click", resetScores);
}
// Init
placeSymbol();
addResetListeners();
}
startGame();