-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
233 lines (205 loc) · 6.23 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const cells = document.querySelectorAll(".cell");
const board = Array(9).fill(null);
const turnInfo = document.getElementById("turnInfo");
const opponentSelector = document.getElementById("opponent");
let gameOver = false;
let currentPlayer = null; // Will be set once user picks an opponent
// reference to the Reset button
const resetButton = document.getElementById("resetButton");
resetButton.addEventListener("click", resetGame);
opponentSelector.addEventListener("change", function () {
currentPlayer = "X"; // Reset to start with X
board.fill(null); // Clear the board
cells.forEach((cell) => (cell.textContent = "")); // Clear cell content
if (opponentSelector.value === "ai") {
turnInfo.textContent = "You are playing against Minimax AI. Your move!";
} else if (opponentSelector.value === "joe") {
turnInfo.textContent = "You are playing against Random Joe. Your move!";
} else {
turnInfo.textContent = "You are playing by yourself. Your move!";
}
});
// MAIN LOGIC
cells.forEach((cell) => {
cell.addEventListener("click", function () {
if (!currentPlayer || gameOver) return; // Don't proceed if no opponent is selected
const index = cell.dataset.index;
if (!board[index] && currentPlayer && !gameOver) {
board[index] = currentPlayer;
cell.textContent = currentPlayer;
// Check for a win after the player's move
if (checkWin(board, currentPlayer)) {
console.log("Player X (User) wins!");
highlightWinningCells(getWinCombo(board, currentPlayer));
gameOver = true;
setTimeout(function () {
alert("You won, here's a cookie 🍪");
}, 100);
return; // Stop the game
}
if (opponentSelector.value == "human") {
currentPlayer = currentPlayer === "X" ? "O" : "X";
turnInfo.textContent = `Player ${currentPlayer}'s move!`;
} else if (opponentSelector.value === "ai") {
const aiMove = minimax(board, "X").index;
board[aiMove] = "O";
cells[aiMove].textContent = "O";
// Check for a win after the AI's move
const winningCombo = getWinCombo(board, "O");
if (winningCombo) {
console.log(winningCombo);
console.log("AI (Player O) wins!");
//highlight the winning cells
highlightWinningCells(winningCombo);
gameOver = true;
setTimeout(function () {
alert("No way you lost to me😭, I'm coming for your job 😈");
}, 100);
return; // Stop the game
}
turnInfo.textContent = "Your move!";
} else if (opponentSelector.value == "joe") {
const joeMove = randomJoeMove(board);
board[joeMove] = "O";
cells[joeMove].textContent = "O";
const winningCombo = getWinCombo(board, "O");
if (winningCombo) {
console.log(winningCombo);
console.log("Random Joe (Player O) wins!");
gameOver = true;
setTimeout(function () {
alert("Random Joe wins, Embarassing...");
}, 100);
return; // Stop the game
}
turnInfo.textContent = "Your move!";
}
}
});
});
function randomJoeMove(board) {
let availableMoves = [];
for (let i = 0; i < board.length; i++) {
if (!board[i]) availableMoves.push(i);
}
return availableMoves[Math.floor(Math.random() * availableMoves.length)];
}
function minimax(newBoard, player) {
const availableMoves = getAvailableMoves(newBoard);
if (checkWin(newBoard, "O")) {
return { score: -10 };
} else if (checkWin(newBoard, "X")) {
return { score: 10 };
} else if (availableMoves.length === 0) {
return { score: 0 };
}
const moves = [];
for (let i = 0; i < availableMoves.length; i++) {
const move = {};
move.index = availableMoves[i];
newBoard[availableMoves[i]] = player;
if (player === "X") {
const g = minimax(newBoard, "O");
move.score = g.score;
} else {
const g = minimax(newBoard, "X");
move.score = g.score;
}
newBoard[availableMoves[i]] = null;
moves.push(move);
}
let bestMove;
if (player === "X") {
let bestScore = -Infinity;
for (let i = 0; i < moves.length; i++) {
if (moves[i].score > bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
} else {
let bestScore = Infinity;
for (let i = 0; i < moves.length; i++) {
if (moves[i].score < bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
}
return moves[bestMove];
}
function getAvailableMoves(board) {
const moves = [];
for (let i = 0; i < board.length; i++) {
if (!board[i]) moves.push(i);
}
return moves;
}
function checkWin(board, player) {
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],
];
for (let i = 0; i < winningCombos.length; i++) {
if (
board[winningCombos[i][0]] === player &&
board[winningCombos[i][1]] === player &&
board[winningCombos[i][2]] === player
) {
return true;
}
}
return false;
}
function getWinCombo(board, player) {
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],
];
for (let i = 0; i < winningCombos.length; i++) {
if (
board[winningCombos[i][0]] === player &&
board[winningCombos[i][1]] === player &&
board[winningCombos[i][2]] === player
) {
return winningCombos[i];
}
}
return null;
}
// Function to reset the game
function resetGame() {
currentPlayer = null;
board.fill(null);
cells.forEach((cell) => {
cell.textContent = "";
//new
cell.classList.remove("winning-cell"); // remove the winning highlight
});
turnInfo.textContent = "Choose your opponent to start the game.";
gameOver = false; // Reset the game state
opponentSelector.value = "none";
if (opponentSelector.value === "human") {
// Start with Player X
currentPlayer = "X";
turnInfo.textContent = "Player X's move!";
}
}
// Highlight winning cells by adding a class
function highlightWinningCells(winningCombo) {
winningCombo.forEach((index) => {
cells[index].classList.add("winning-cell");
});
}