-
Notifications
You must be signed in to change notification settings - Fork 8
/
ai.js
138 lines (120 loc) · 4.5 KB
/
ai.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var Board = require('./board');
var evalCnt = 0;
// ===========================================================
// Constructor
// ===========================================================
function AI(board){
this.board = board;
}
// ===========================================================
// Instance Methods
// ===========================================================
AI.prototype.allPossiblePlacements = function(color) {
var allPlacments = [];
var pieces = this.board.piecesNotInPlayByColor(color);
var colorNotation = Board.notationFromColor(color);
for(var i = 0; i < pieces.length; i++){
var piece = pieces[i];
var pieceTypeNotation = Board.notationFromType(piece.type, piece.typeId);
var placements = this.board.legalPiecePlacements(piece);
for(var j = 0; j < placements.length; j++){
var notation = colorNotation + ':' + 'p' + ':' + pieceTypeNotation + ':' + placements[j];
if(this.board.validateQueenPlaced(notation)){
allPlacments.push(notation);
}
}
}
return allPlacments;
}
AI.prototype.allPossibleMovements = function(color) {
var allMovements = [];
var pieces = this.board.piecesInPlayByColor(color);
var colorNotation = Board.notationFromColor(color);
for(var i = 0; i < pieces.length; i++){
var piece = pieces[i];
var pieceTypeNotation = Board.notationFromType(piece.type, piece.typeId);
var placements = this.board.legalPieceMovements(piece);
for(var j = 0; j < placements.length; j++){
var notation = colorNotation + ':' + 'm' + ':' + pieceTypeNotation + ':' + placements[j];
if(this.board.validateQueenPlaced(notation) && this.board.validateConnected(notation)){
allMovements.push(notation);
}
this.board.errors = [];
}
}
return allMovements;
}
AI.prototype.allPossibleMoves = function(color) {
return AI.shuffle(this.allPossiblePlacements(color).concat(this.allPossibleMovements(color)));
}
AI.prototype.surroundingQueenCnt = function(color){
var queen = this.board.findPiece(color, 'QUEEN', '1');
return queen.isInPlay ? this.board.surroundingPieces(queen.location, true).length : 0;
}
AI.prototype.evaluatePosition = function() {
var minQueenSurroundCnt = this.surroundingQueenCnt(this.minimizingColor);
var maxQueenSurroundCnt = this.surroundingQueenCnt(this.maximizingColor);
if (minQueenSurroundCnt === 6) minQueenSurroundCnt = Number.POSITIVE_INFINITY;
if (maxQueenSurroundCnt === 6) maxQueenSurroundCnt = Number.NEGATIVE_INFINITY;
var score = minQueenSurroundCnt - maxQueenSurroundCnt;
return score;
}
AI.prototype.bestMove = function(depth) {
evalCnt = 0;
this.maximizingColor = this.board.whoseTurn();
this.minimizingColor = AI.toggleColor(this.maximizingColor);
this.board.quietMode = true;
var move = this.minimax(depth, true, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY);
this.board.quietMode = false;
console.log('eval count: ' + evalCnt);
return move;
}
AI.prototype.minimax = function(depth, maximizing, alpha, beta, notation) {
if(notation) {
this.board.moves.push(notation);
this.board.movement(notation);
}
if(depth === 0){
evalCnt += 1;
var score = this.evaluatePosition(!maximizing);
this.board.revertLast();
return { score: score, notation: notation }
}
var color = this.board.whoseTurn();
var allPossibleMoves = this.allPossibleMoves(color);
var bestPath = maximizing ? { score: Number.NEGATIVE_INFINITY } : { score: Number.POSITIVE_INFINITY } ;
for(var i = 0; i < allPossibleMoves.length; i++){
var candidate = allPossibleMoves[i];
var results = this.minimax(depth - 1, !maximizing, alpha, beta, candidate);
if(maximizing){
if (results.score > bestPath.score){
bestPath = { score: results.score, notation: candidate }
}
alpha = Math.max(alpha, bestPath.score);
}else {
if (results.score < bestPath.score) {
bestPath = { score: results.score, notation: candidate }
}
beta = Math.min(beta, bestPath.score);
}
if(beta <= alpha){
break;
}
}
if(notation){
this.board.revertLast();
}
return bestPath;
}
// ===========================================================
// Class Methods
// ===========================================================
AI.toggleColor = function(color){
return color === 'BLACK' ? 'WHITE' : 'BLACK';
}
// Source - http://jsfromhell.com/array/shuffle
AI.shuffle = function(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
module.exports = AI;