-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.js
More file actions
320 lines (245 loc) · 8.07 KB
/
algorithm.js
File metadata and controls
320 lines (245 loc) · 8.07 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { Chess } from 'chess.js';
const WHITE = 'w';
const BLACK = 'b';
let debug = {};
let piece_vals = {
'p' : 100,
'r': 500,
'n': 320,
'b': 330,
'q': 900,
'k': 20000
}
let pawnEvalWhite = [
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, -20, -20, 10, 10, 5,
5, -5, -10, 0, 0, -10, -5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, 5, 10, 25, 25, 10, 5, 5,
10, 10, 20, 30, 30, 20, 10, 10,
50, 50, 50, 50, 50, 50, 50, 50,
0, 0, 0, 0, 0, 0, 0, 0
]
let pawnEvalBlack = pawnEvalWhite.reverse();
let knightEval = [
-50, -40, -30, -30, -30, -30, -40, -50,
-40, -20, 0, 0, 0, 0, -20, -40,
-30, 0, 10, 15, 15, 10, 0, -30,
-30, 5, 15, 20, 20, 15, 5, -30,
-30, 0, 15, 20, 20, 15, 0, -30,
-30, 5, 10, 15, 15, 10, 5, -30,
-40, -20, 0, 5, 5, 0, -20, -40,
-50, -40, -30, -30, -30, -30, -40, -50
]
let bishopEvalWhite = [
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 5, 0, 0, 0, 0, 5, -10,
-10, 10, 10, 10, 10, 10, 10, -10,
-10, 0, 10, 10, 10, 10, 0, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-20, -10, -10, -10, -10, -10, -10, -20
]
let bishopEvalBlack = bishopEvalWhite.reverse();
let rookEvalWhite = [
0, 0, 0, 5, 5, 0, 0, 0,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
5, 10, 10, 10, 10, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
]
let rookEvalBlack = rookEvalWhite.reverse();
let queenEval = [
-20, -10, -10, -5, -5, -10, -10, -20,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 0, 0, 0, 0, -10,
-20, -10, -10, -5, -5, -10, -10, -20
]
let kingEvalWhite = [
20, 30, 10, 0, 0, 10, 30, 20,
20, 20, 0, 0, 0, 0, 20, 20,
-10, -20, -20, -20, -20, -20, -20, -10,
20, -30, -30, -40, -40, -30, -30, -20,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30
]
let kingEvalBlack = kingEvalWhite.reverse();
let kingEvalEndGameWhite = [
50, -30, -30, -30, -30, -30, -30, -50,
-30, -30, 0, 0, 0, 0, -30, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -20, -10, 0, 0, -10, -20, -30,
-50, -40, -30, -20, -20, -30, -40, -50
]
let kingEvalEndGameBlack = kingEvalEndGameWhite.reverse();
// https://github.com/healeycodes/andoma
// https://www.freecodecamp.org/news/simple-chess-ai-step-by-step-1d55a9266977/
function getPieceVal(piece, x, y, endgame) {
if (piece === null || piece === undefined) return 0;
let rank = y - 1;
let idx = (7 - rank) * 8 + x;
let res = null;
if(piece.type === 'p') {
res = piece_vals[piece.type] + (piece.color == WHITE ? pawnEvalWhite[idx] : pawnEvalBlack[idx]);
} else if(piece.type == 'r') {
res = piece_vals[piece.type] + (piece.color == WHITE ? rookEvalWhite[idx] : rookEvalBlack[idx]);
} else if(piece.type == 'n') {
res = piece_vals[piece.type] + knightEval[idx];
} else if(piece.type == 'b') {
res = piece_vals[piece.type] + (piece.color == WHITE ? bishopEvalWhite[idx] : bishopEvalBlack[idx]);
} else if(piece.type == 'q') {
res = piece_vals[piece.type] + queenEval[idx];
} else if(piece.type == 'k') {
if(endgame) {
res = piece_vals[piece.type] + (piece.color == WHITE ? kingEvalEndGameWhite[idx] : kingEvalEndGameBlack[idx]);
} else {
res = piece_vals[piece.type] + (piece.color == WHITE ? kingEvalWhite[idx] : kingEvalBlack[idx]);
}
}
return piece.color == WHITE ? res : -res;
}
function squareToCoords(square) {
let x = square.charCodeAt(0) - 97;
let y = parseInt(square[1], 10);
return {x, y};
}
function moveValue(board, move, endgame) {
if (move.promotion) {
return board.turn() === WHITE ? Infinity : -Infinity;
}
const piece = board.get(move.from);
if (!piece) {
throw new Error("No piece on from-square: " + move.from);
}
const from = squareToCoords(move.from);
const to = squareToCoords(move.to);
const fromValue = getPieceVal(piece, from.x, from.y, endgame);
const toValue = getPieceVal(piece, to.x, to.y, endgame);
let posChange = toValue - fromValue;
let captureValue = 0;
if (move.captured) {
captureValue = evaluateCapture(board, move);
}
let score = posChange + captureValue;
return board.turn() === WHITE ? score : -score;
}
function evaluateCapture(board, move) {
// En passant
if(move.flags && move.flags.includes('e')) {
return piece_vals['p'];
}
let to = board.get(move.to);
let from = board.get(move.from);
if(to === undefined || from === undefined) {
throw new Error("Pieces were expected at: ", to, " and ", from);
}
return piece_vals[to.type] - piece_vals[from.type];
}
function staticEvaluation(board) {
let total_eval = 0
let endgame = checkEndGame(board);
for(let i=0; i<8; i++) {
for(let j=1; j<=8; j++) {
let square = String.fromCharCode(97+i) + j;
let piece = board.get(square);
total_eval += getPieceVal(piece, i, j, endgame);
}
}
return total_eval;
}
function getOrderedMoves(board) {
let endgame = checkEndGame(board);
let moves = board.moves({ verbose: true });
moves.sort((a, b)=>{
return moveValue(board, b, endgame) - moveValue(board, a, endgame);
});
return moves;
}
function minimax(board, depth, alpha, beta, maximizing_player) {
if (board.isCheckmate()) {
return maximizing_player ? -1000000000 : 1000000000;
} else if (board.isGameOver()) {
return 0;
}
if(depth == 0) {
return staticEvaluation(board);
}
if (maximizing_player) {
let value = -Infinity;
for(let move of getOrderedMoves(board)) {
board.move(move);
value = Math.max(value, minimax(board, depth-1, alpha, beta, false));
board.undo();
alpha = Math.max(alpha, value);
if (beta <= alpha)
return value;
}
return value;
} else {
let value = Infinity;
for(let move of getOrderedMoves(board)) {
board.move(move);
value = Math.min(value, minimax(board, depth-1, alpha, beta, true));
board.undo();
beta = Math.min(beta, value);
if(beta <= alpha)
return value;
}
return value;
}
}
function checkEndGame(board) {
/*
according to Michniewski
both sides have no queens or a side that has a queen has no other pieces or one minorpiece maximum (bishop or knight)
*/
// console.log('endgame obj: ', board);
// console.log('in endgame: ', typeof board);
let queens = 0;
let minorChessPieces = 0;
for(let i=0; i<8; i++) {
for(let j=1; j<=8; j++) {
let square = String.fromCharCode(97+i) + j;
let piece = board.get(square);
if(piece) {
if(piece.type == 'q')
queens++;
}
if (piece) {
if(piece.type == 'b' || piece.type == 'n')
minorChessPieces++;
}
}
}
if(queens == 0 || (queens == 2 && minorChessPieces <= 1)) {
return true;
}
return false;
}
export function findBestMove(board, depth, maximizing_player) {
let bestMove = null;
let bestValue = -Infinity;
for(let move of getOrderedMoves(board)) {
board.move(move);
let value = minimax(board, depth-1, -Infinity, Infinity, !maximizing_player);
board.undo();
if(value > bestValue) {
bestValue = value;
bestMove = move;
}
}
return bestMove;
}