-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
118 lines (105 loc) · 2.78 KB
/
engine.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
/*
* My Chess Engine Attempt
* TODO:
* - Parallelize!
* - Improve the Evaluation function with heuristics
* - Introduce an opening book and a tablebase for endgames
* - Move ordering to speed up alpha-beta
*
*/
let points = new Map();
points.set('P', 1);
points.set('N', 3);
points.set('B', 3);
points.set('R', 5);
points.set('Q', 9);
points.set('K', 100);
points.set('p', -1);
points.set('n', -3);
points.set('b', -3);
points.set('r', -5);
points.set('q', -9);
points.set('k', -100);
depth = 3
/*
* Evaluates a position based only on material.
* Returns: Int - The value of the new position.
*/
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)
}
function makeBestMove(game) {
let bestMove = minimaxRoot(game, depth, true);
return bestMove
}
const minimaxRoot = function(game, depth, maximisingPlayer) {
var bestMove = -Infinity;
var bestMoveFound;
var t0 = performance.now()
// Can we parallelize this loop?
for (var i = 0; i < game.moves().length; i++) {
game.move(game.moves()[i]);
var value = minimax(
game,
depth - 1,
-Infinity,
Infinity,
!maximisingPlayer
);
game.undo();
if (value >= bestMove) {
bestMove = value;
bestMoveFound = game.moves()[i];
}
}
console.log("bestMoveFound: " + bestMoveFound)
var t1 = performance.now()
console.log("Time taken: " + (t1 - t0) + ", moves evaluated: " + game.moves().length)
return bestMoveFound;
};
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;
}
}
// TO-DO
// Evaluate position on more criteria (e.g. position of pieces on board)