-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
49 lines (47 loc) · 1.27 KB
/
worker.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
function minimax(position, depth, alpha, beta, maximisingPlayer) {
if (depth === 0) {
return -evaluate(position);
}
if (maximisingPlayer) {
let value = -Infinity;
for (let i = 0; i < position.moves().length; i++) {
position.move(position.moves()[i]);
value = Math.max(value, minimax(position, depth - 1, alpha, beta, false));
position.undo();
alpha = Math.max(alpha, value);
if (alpha >= beta) {
return value;
}
}
return value;
} else {
let value = Infinity;
for (let i = 0; i < position.moves().length; i++) {
position.move(game.moves()[i]);
value = Math.min(value, minimax(position, depth - 1, alpha, beta, true));
position.undo();
beta = Math.min(beta, value);
if (alpha >= beta) {
return value;
}
}
return value;
}
}
function evaluate(chess){
var fen = chess.fen();
//console.log(fen);
var i = 0;
var result = 0;
while(fen.charAt(i) !== ' '){
//console.log(fen.charAt(i))
myPoints = points.get(fen.charAt(i));
if (myPoints != null){
result += myPoints;
i++;
continue;
}
i++;
}
return(result)
}