-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.js
53 lines (39 loc) · 1.24 KB
/
search.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
function minMax(depth) {
var bestMove;
// if red, best is -INFINTE
// if black, best is INFINITE
var bestScore = -INFINITE * (GameBoard.turn * -1);
var posScore;
var moves = generateMoves();
for (var i = 0; i < moves.length; i++) {
makeMove(moves[i]);
posScore = searchScores(depth);
//posScore = evalPosition();
if (!GameBoard.turn && posScore < bestScore || GameBoard.turn && posScore > bestScore) {
//if (posScore < bestScore) {
bestScore = posScore;
bestMove = moves[i];
}
takeMove();
}
return bestMove;
}
function searchScores(depth) {
var bestScore = -INFINITE * (GameBoard.turn * -1);
var posScore;
var moves = generateMoves();
for (var i = 0; i < moves.length; i++) {
makeMove(moves[i]);
if (depth > 0) {
posScore = searchScores(depth-1);
}
else {
posScore = evalPosition();
}
if (!GameBoard.turn && posScore < bestScore || GameBoard.turn && posScore > bestScore) {
bestScore = posScore;
}
takeMove();
}
return bestScore;
}