-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.mjs
205 lines (178 loc) · 4.57 KB
/
Bot.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
import { PIECES } from "./constants.mjs";
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
export class Bot1 {
constructor(piece) {
this.piece = piece;
this.name = "Talgat";
}
move(board) {
console.log(
"%c Talgat - board -> ",
"background: #222; color: royalblue",
board
);
// let i = getRandomInt(0, 9);
// while (board[i] !== 0) {
// i = getRandomInt(0, 9);
// }
// return i;
let bestScore = -10000;
let bestMove;
const newBoard = [...board];
const first = isFirst(newBoard);
if (first) {
return getRandomInt(0, 9);
}
for (let i = 0; i < 9; i++) {
if (newBoard[i] === 0) {
newBoard[i] = PIECES.X;
const score = minimax(newBoard, 0, false);
newBoard[i] = 0;
if (score > bestScore) {
bestScore = score;
bestMove = i;
}
}
}
console.log(
"%c Talgat - bestMove -> ",
"background: #222; color: royalblue",
bestMove);
return bestMove;
}
}
function isFirst(board) {
for (const el of board) {
if (el !== 0) return false;
}
return true
}
export class Bot2 {
constructor(piece) {
this.piece = piece;
this.name = "Abat";
}
move(board) {
console.log(
"%c Abat - board -> ",
"background: #222; color: royalblue",
board
);
let i = getRandomInt(0, 9);
while (board[i] !== 0) {
i = getRandomInt(0, 9);
}
console.log("%c Abat - i -> ", "background: #222; color: royalblue", i);
return i;
// let bestScore = Number.MAX_VALUE;
// let bestMove;
// const newBoard = [...board];
// for (let i = 0; i < 9; i++) {
// if (newBoard[i] === 0) {
// newBoard[i] = PIECES.O;
// const score = minimax(newBoard, 0, true);
// newBoard[i] = 0;
// if (score < bestScore) {
// bestScore = score;
// bestMove = i;
// }
// }
// }
// return bestMove;
}
}
function minimax(board, depth, isMax) {
let score = checkWinner(board);
// If Maximizer has won the game
// return his/her evaluated score
if (score === PIECES.X)
return score;
// If Minimizer has won the game
// return his/her evaluated score
if (score === PIECES.O)
return score;
if (score === "tie")
return 0;
// If this maximizer's move
if (isMax) {
let best = -10000;
for (let i = 0; i < 9; i++) {
if (board[i] === 0) {
board[i] = PIECES.X;
best = Math.max(best, minimax(board,
depth + 1, false));
board[i] = 0;
}
}
return best;
}
// If this minimizer's move
else {
let best = 10000;
for (let i = 0; i < 9; i++) {
if (board[i] === 0) {
board[i] = PIECES.O;
best = Math.min(best, minimax(board,
depth + 1, true));
board[i] = 0;
}
}
return best;
}
}
function checkWinner(board) {
let winner = null;
const COL = 3;
const ROW = 3;
// Horizontal
// Horizontal
if (board[0] === board[1] && board[1] === board[2] && board[0] !== 0) {
if (board[0] === PIECES.X) winner = PIECES.X;
else winner = PIECES.O;
}
if (board[3] === board[4] && board[4] === board[5] && board[3] !== 0) {
if (board[3] === PIECES.X) winner = PIECES.X;
else winner = PIECES.O;
}
if (board[6] === board[7] && board[7] === board[8] && board[6] !== 0) {
if (board[6] === PIECES.X) winner = PIECES.X;
else winner = PIECES.O;
}
// Vertical
if (board[0] === board[3] && board[3] === board[6] && board[0] !== 0) {
if (board[0] === PIECES.X) winner = PIECES.X;
else winner = PIECES.O;
}
if (board[1] === board[4] && board[4] === board[7] && board[1] !== 0) {
if (board[1] === PIECES.X) winner = PIECES.X;
else winner = PIECES.O;
}
if (board[2] === board[5] && board[5] === board[8] && board[2] !== 0) {
if (board[2] === PIECES.X) winner = PIECES.X;
else winner = PIECES.O;
}
// Diagonal
if (board[0] === board[4] && board[4] === board[8] && board[0] !== 0) {
if (board[0] === PIECES.X) winner = PIECES.X;
else winner = PIECES.O;
}
if (board[2] === board[4] && board[4] === board[6] && board[2] !== 0) {
if (board[2] === PIECES.X) winner = PIECES.X;
else winner = PIECES.O;
}
let openSpots = 0;
for (let i = 0; i < 9; i++) {
if (board[i] === 0) {
openSpots++;
}
}
if (winner === null && openSpots === 0) {
return "tie";
} else {
return winner;
}
}