-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (53 loc) · 1.59 KB
/
index.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
function NegamaxAlphaBeta (config) {
const conf = Object.assign({
generateMoves: (gameState) => [],
makeMove: (gameState, move) => true,
unmakeMove: null,
evaluate: (gameState) => 0,
evaluateTerminal: (gameState) => null
}, config);
this._generateMoves = conf.generateMoves;
this._makeMove = conf.makeMove;
this._unmakeMove = conf.unmakeMove;
this._evaluate = conf.evaluate;
this._evaluateTerminal = conf.evaluateTerminal;
this.nodeCount = 0;
}
NegamaxAlphaBeta.prototype.search = function (gameState, depth, alpha = -Infinity, beta = Infinity, side = 1, isRoot = true) {
if (isRoot) this.nodeCount = 1;
else ++this.nodeCount;
const terminalEvaluation = this._evaluateTerminal(gameState);
if (terminalEvaluation != null) {
return terminalEvaluation;
}
if (depth === 0) {
return this._evaluate(gameState);
}
let bestScore = -Infinity, bestMove = null;
const moves = this._generateMoves(gameState);
for (let i = 0, len = moves.length; i < len; ++i) {
const move = moves[i];
const sideChanged = this._makeMove(gameState, move) ? -1 : 1;
const score = sideChanged * this.search(
gameState,
depth - 1,
sideChanged * alpha,
sideChanged * beta,
sideChanged * side,
false
);
this._unmakeMove(gameState, move);
if (score > bestScore) {
bestScore = score;
}
if (bestScore > alpha) {
alpha = bestScore;
bestMove = move;
}
if (alpha >= beta) {
break;
}
}
return isRoot ? {score: bestScore, bestMove: bestMove} : bestScore;
};
module.exports = NegamaxAlphaBeta;