-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
171 lines (156 loc) · 3.87 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const X_CLASS = "x";
const O_CLASS = "o";
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 cellElements = document.querySelectorAll("[data-cell]");
const board = document.getElementById("board");
const winningMessageElement = document.getElementById("winningMessage");
const restartButton = document.getElementById("restartButton");
const winningMessageTextElement = document.querySelector(
"[data-winning-message-text]"
);
let oTurn;
let currentPlayer;
startGame();
restartButton.addEventListener("click", startGame);
function startGame() {
oTurn = false;
currentPlayer = "human";
cellElements.forEach((cell) => {
cell.classList.remove(X_CLASS);
cell.classList.remove(O_CLASS);
cell.removeEventListener("click", handleClick);
cell.addEventListener("click", handleClick);
});
setBoardHoverClass();
winningMessageElement.classList.remove("show");
}
function handleClick(e) {
let cell;
let currentClass = oTurn ? O_CLASS : X_CLASS;
if (currentClass == X_CLASS) {
cell = e.target;
} else {
cell = bestMove();
}
placeMark(cell, currentClass);
if (checkWin(currentClass)) {
endGame(false);
} else if (isDraw()) {
endGame(true);
} else {
swapTurns();
handleClick();
setBoardHoverClass();
}
}
function bestMove() {
let bestScore = -Infinity;
let bestCell;
let availableCells = getAvailableCells();
for (let i = 0; i < availableCells.length; i++) {
const cell = availableCells[i];
cell.classList.add(O_CLASS);
const cellScore = minimax(cell, 0, false);
cell.classList.remove(O_CLASS);
if (cellScore > bestScore) {
bestScore = cellScore;
bestCell = cell;
}
}
return bestCell;
}
let scores = {
X: -1,
O: 1,
tie: 0,
};
function minimax(board, depth, isMaximazing) {
let availableCells = getAvailableCells();
if (checkWin(X_CLASS)) {
return scores.X;
} else if (checkWin(O_CLASS)) {
return scores.O;
} else if (availableCells.length === 0) {
return scores.tie;
}
if (isMaximazing) {
let bestScore = -Infinity;
for (let i = 0; i < availableCells.length; i++) {
const cell = availableCells[i];
cell.classList.add(O_CLASS);
const cellScore = minimax(cell, depth + 1, false);
cell.classList.remove(O_CLASS);
bestScore = Math.max(bestScore, cellScore);
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < availableCells.length; i++) {
const cell = availableCells[i];
cell.classList.add(X_CLASS);
const cellScore = minimax(cell, depth + 1, true);
cell.classList.remove(X_CLASS);
bestScore = Math.min(bestScore, cellScore);
}
return bestScore;
}
}
function getAvailableCells() {
let availableCells = [];
cellElements.forEach((cell) => {
if (
!cell.classList.contains(X_CLASS) &&
!cell.classList.contains(O_CLASS)
) {
availableCells.push(cell);
}
});
return availableCells;
}
function endGame(draw) {
if (draw) {
winningMessageTextElement.innerText = "Draw!";
} else {
winningMessageTextElement.innerText = `${oTurn ? "O's" : "X's"} wins!`;
}
winningMessageElement.classList.add("show");
}
function isDraw() {
return [...cellElements].every((cell) => {
return cell.classList.contains(O_CLASS) || cell.classList.contains(X_CLASS);
});
}
function placeMark(cell, currentClass) {
cell.classList.add(currentClass);
cell.removeEventListener("click", handleClick);
}
function swapTurns() {
oTurn = !oTurn;
currentPlayer = currentPlayer == "human" ? "computer" : "human";
console.log(currentPlayer);
}
function setBoardHoverClass() {
board.classList.remove(X_CLASS);
board.classList.remove(O_CLASS);
if (oTurn) {
board.classList.add(O_CLASS);
} else {
board.classList.add(X_CLASS);
}
}
function checkWin(currentClass) {
return WINNING_COMBINATIONS.some((combination) => {
return combination.every((index) => {
return cellElements[index].classList.contains(currentClass);
});
});
}