-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
210 lines (194 loc) · 4.22 KB
/
index.mjs
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
import readline from "readline";
import { animateWin, animateDraw, animateLose } from "./animations.mjs";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const board = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "],
];
let currentPlayer = "X";
function printBoard() {
console.clear();
for (let i = 0; i < 3; i++) {
console.log(board[i].join(" | "));
if (i < 2) {
console.log("---------");
}
}
console.log("\n");
}
function checkWin(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 switchPlayer() {
currentPlayer = currentPlayer === "X" ? "O" : "X";
}
function computerMove() {
let bestScore = -Infinity;
let move;
// Check if computer can win in the next move
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === " ") {
board[i][j] = "O";
if (checkWin("O")) {
return;
}
board[i][j] = " ";
}
}
}
// If no immediate win, use minimax to find the best move
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === " ") {
board[i][j] = "O";
let score = minimax(board, 0, false);
board[i][j] = " ";
if (score > bestScore) {
bestScore = score;
move = { row: i, col: j };
}
}
}
}
board[move.row][move.col] = "O";
}
const scores = {
X: -1,
O: 1,
tie: 0,
};
function minimax(board, depth, isMaximizing) {
if (checkWin("X")) {
return scores.X - depth;
}
if (checkWin("O")) {
return scores.O + depth;
}
if (isBoardFull()) {
return scores.tie;
}
if (isMaximizing) {
let bestScore = -Infinity;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === " ") {
board[i][j] = "O";
let score = minimax(board, depth + 1, false);
board[i][j] = " ";
bestScore = Math.max(score, bestScore);
}
}
}
return bestScore;
} else {
let bestScore = Infinity;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === " ") {
board[i][j] = "X";
let score = minimax(board, depth + 1, true);
board[i][j] = " ";
bestScore = Math.min(score, bestScore);
}
}
}
return bestScore;
}
}
function playGame() {
printBoard();
if (currentPlayer === "X") {
rl.question(
`Player X, enter row (0-2) and column (0-2) separated by a space: `,
(input) => {
const [row, col] = input.split(" ").map(Number);
if (
row >= 0 &&
row < 3 &&
col >= 0 &&
col < 3 &&
board[row][col] === " "
) {
board[row][col] = currentPlayer;
if (checkWin(currentPlayer)) {
printBoard();
animateWin();
rl.close();
} else if (isBoardFull()) {
printBoard();
animateDraw();
rl.close();
} else {
switchPlayer();
playGame();
}
} else {
console.log("Invalid move. Try again.");
playGame();
}
}
);
} else {
setTimeout(() => {
computerMove();
if (checkWin("O")) {
printBoard();
animateLose();
rl.close();
} else if (isBoardFull()) {
printBoard();
animateDraw();
rl.close();
} else {
switchPlayer();
playGame();
}
}, 1000);
}
}
playGame();