-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (104 loc) · 2.18 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
function checkWinner(player) {
for (let i = 0; i < 3; i++) {
if (
board[i][0] === player &&
board[i][1] === player &&
board[i][2] === player
) {
return true;
}
if (
board[0][i] === player &&
board[1][i] === player &&
board[2][i] === player
) {
return true;
}
}
if (
board[0][0] === player &&
board[1][1] === player &&
board[2][2] === player
) {
return true;
}
if (
board[0][2] === player &&
board[1][1] === player &&
board[2][0] === player
) {
return true;
}
return false;
}
function isBoardFull() {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === "") {
return false;
}
}
}
return true;
}
function markWinningCells(player) {
for (let i = 0; i < 3; i++) {
if (
board[i][0] === player &&
board[i][1] === player &&
board[i][2] === player
) {
markCellAsWinning(i, 0);
markCellAsWinning(i, 1);
markCellAsWinning(i, 2);
return;
}
if (
board[0][i] === player &&
board[1][i] === player &&
board[2][i] === player
) {
markCellAsWinning(0, i);
markCellAsWinning(1, i);
markCellAsWinning(2, i);
return;
}
}
if (
board[0][0] === player &&
board[1][1] === player &&
board[2][2] === player
) {
markCellAsWinning(0, 0);
markCellAsWinning(1, 1);
markCellAsWinning(2, 2);
}
if (
board[0][2] === player &&
board[1][1] === player &&
board[2][0] === player
) {
markCellAsWinning(0, 2);
markCellAsWinning(1, 1);
markCellAsWinning(2, 0);
}
}
function markCellAsWinning(row, col) {
const cell = document.getElementById("board").children[row * 3 + col];
cell.classList.add("winning-cell");
}
function resetGame() {
board = [
["", "", ""],
["", "", ""],
["", "", ""],
];
currentPlayer = "X";
gameOver = false;
document.getElementById("message").innerHTML = "";
const cells = document.getElementsByClassName("cell");
for (let i = 0; i < cells.length; i++) {
cells[i].innerHTML = "";
cells[i].classList.remove("winning-cell"); // Remove winning effect
}
}