-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIPlayer.cpp
More file actions
279 lines (253 loc) · 8.98 KB
/
Copy pathAIPlayer.cpp
File metadata and controls
279 lines (253 loc) · 8.98 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <stdio.h>
#include "AIPlayer.h"
AIPlayer::AIPlayer (Board* brd, BoardGUI* brdgui, int difficulty) {
b = brd;
bgui = brdgui;
maxDepth = difficulty;
}
bool AIPlayer::isHuman() {
return false;
}
void AIPlayer::saveBoard() {
bSave = *b;
}
bool AIPlayer::isBoardDifferent() {
return b->isDifferent(bSave);
}
int AIPlayer::decideMove() {
//saveBoard(); // uncomment if want to find bugs in board or AI
// numNodes = 0; // uncomment to check pruning's effect (see how many nodes the AI evaluates)
int color = b->getPlayer()? -1 : 1;
int bestMove = -1;
int alpha = -2 * MATE_VALUE;
int beta = 2 * MATE_VALUE;
int numMoves = b->getNumMoves();
// reorder moves
int moveOrder[numMoves];
reorderMoves(moveOrder, numMoves, color);
// Find the sub-tree with best value
// Instead of calling negamax(maxDepth, alpha, beta, color), the first ply moves are search separately
// because after evaluating each move, the piled up GUI events need to be handled.
for (int m = 0; m < numMoves; m++) {
b->makeMove(moveOrder[m]);
// get the value of the sub-tree
int val = -negamax(maxDepth-1, -beta, -alpha, -color);
b->undoMove();
// update the best value and cut off values
if (val > alpha) {
alpha = val;
bestMove = moveOrder[m];
if (alpha >= beta) break;
}
// handle GUI events: if user quit, immediately
if (bgui->getInput() == BoardGUI::INPUT_HOME || GUI::quit) return -1;
}
// uncomment to check for board or AI bugs.
//if (isBoardDifferent()) {
//GUI::quit = true;
//return -1;
//}
return bestMove;
}
int AIPlayer::negamax(int depth, int alpha, int beta, int color) {
//numNodes++; // uncomment to check pruning performance
int numMoves = b->getNumMoves();
//////////////////////////////////////////////////////////////////
// If reaches cut-off depth or a terminal node (end game node),
// evaluate the board at that node and return the board's score.
//////////////////////////////////////////////////////////////////
if (depth <= 0 || numMoves == 0) {
// get board's score for the player to move (color adjusts the sign of board's score)
int score = color * heuristicEval();
// try to win early or lose late by adding or subtracting depth.
if (score > 0) {
score = score + depth;
} else if (score < 0) {
score = score - depth;
}
return score;
}
//////////////////////////////////////////////////////////////////
// If node is not a terminal node,
// evaluate each sub-tree and return the best value
//////////////////////////////////////////////////////////////////
// reorder move so that the first few best moves are searched first
int moveOrder[numMoves];
reorderMoves(moveOrder, numMoves, color);
for (int m = 0; m < numMoves; m++) {
b->makeMove(moveOrder[m]);
int val = -negamax(depth-1, -beta, -alpha, -color);
b->undoMove();
if (val > alpha) {
alpha = val;
if (alpha >= beta) break;
}
}
return alpha;
}
const int AIPlayer::NUM_BEST_MOVES = 6;
void AIPlayer::reorderMoves(int *moveOrder, int numMoves, int color) {
// Find the 1-ply score of each move
int score[numMoves];
for(int m = 0; m < numMoves; m++) {
b->makeMove(m);
score[m] = color * heuristicEval();
b->undoMove();
}
// In case there are less than NUM_BEST_MOVES moves available
int numOrder = (numMoves > NUM_BEST_MOVES)? NUM_BEST_MOVES : numMoves;
int n; //the index in moveOrder
// Put the best moves in the first indexes of moveOrder array by
// first finding the best scores, then turning it to -infinity.
// Repeat this NUM_BEST_MOVES times
for (n = 0; n < numOrder; n++) {
int bestScore = score[0];
int bestIndex = 0;
for (int m = 1; m < numMoves; m++) {
if (score[m] > bestScore) {
bestScore = score[m];
bestIndex = m;
}
}
moveOrder[n] = bestIndex;
score[bestIndex] = -2*MATE_VALUE;
}
// Put the rest of the moves in the moveOrder array
for (int m = 0; m < numMoves; m++) {
if (score[m] != -2*MATE_VALUE) {
moveOrder[n] = m;
n++;
}
}
}
const int AIPlayer::pieceValues[6] = {900, 0, 500, 320, 330, 100};
const int AIPlayer::MATE_VALUE = 9 * pieceValues[Board::BQ]
+ 2 * pieceValues[Board::BR]
+ 2 * pieceValues[Board::BK]
+ 2 * pieceValues[Board::BB];
const int AIPlayer::positionValues[7][64] = {
{ // queen
-20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 5, 5, 5, 0,-10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0,-10,
-10, 0, 5, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20
},
{ //king
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-20,-30,-30,-40,-40,-30,-30,-20,
-10,-20,-20,-20,-20,-20,-20,-10,
20, 20,-10,-10,-10,-10, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20
},
{ //rook
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0
},
{ //knight
-50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 0, 0, 0,-20,-40,
-30, 0, 10, 15, 15, 10, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 10, 15, 15, 10, 5,-30,
-40,-20, 0, 5, 5, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50
},
{ //bishop
-20,-10,-10,-10,-10,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 10, 10, 5, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 5, 0, 0, 0, 0, 5,-10,
-20,-10,-10,-10,-10,-10,-10,-20
},
{ //pawn
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5,-10, 0, 0,-10, -5, 5,
5, 10, 10,-20,-20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
},
{ //king late game
-50,-40,-30,-20,-20,-30,-40,-50,
-30,-20,-10, 0, 0,-10,-20,-30,
-30,-10, 20, 30, 30, 20,-10,-30,
-30,-10, 30, 40, 40, 30,-10,-30,
-30,-10, 30, 40, 40, 30,-10,-30,
-30,-10, 20, 30, 30, 20,-10,-30,
-30,-30, 0, 0, 0, 0,-30,-30,
-50,-30,-30,-30,-30,-30,-30,-50,
}
};
const int AIPlayer::LATE_GAME_MATERIAL = 3500;
int AIPlayer::heuristicEval() {
//////////////////////////////////////////////////////////////////
// End board
//////////////////////////////////////////////////////////////////
if (b->getNumMoves() == 0) {
int winner = b->getWinner();
switch (winner) {
case Board::WHITE : return MATE_VALUE; // white wins
case Board::BLACK : return -MATE_VALUE; // black wins
case Board::BOTH_COLOR : return 0; // draw
}
}
//////////////////////////////////////////////////////////////////
// Not end board
//////////////////////////////////////////////////////////////////
int score = 0; //the score of the board for white
int material = 0;
int piece;
// go through all squares and evaluate each square
// using pieceValues and positionalValues arrays
for (int s = 0; s < Board::NUM_SQUARES; s++) {
piece = b->getPiece(s);
if (piece == Board::EMPTY) { //empty square
continue;
} else if (piece > Board::MAX_BLACK_TYPE) { // square has white piece
//leave the king for last
// because during late game, king has different positional value
if (piece == Board::WK) continue;
piece -= Board::MIN_WHITE_TYPE; //get the type of piece without color
score += pieceValues[piece];
material += pieceValues[piece];
score += positionValues[piece][63 - s]; //flips positionValues
}
else { // square has black piece
if (piece == Board::BK) continue;
score -= pieceValues[piece];
material += pieceValues[piece];
score -= positionValues[piece][s];
}
}
if (material > LATE_GAME_MATERIAL) { // early game
int kingSq = b->getKingSquare(Board::WHITE); //white king
score += positionValues[Board::BK][56 + 2*(kingSq % Board::COLS) - kingSq];
kingSq = b->getKingSquare(Board::BLACK); //black king
score -= positionValues[Board::BK][kingSq];
} else { // late game
int kingSq = b->getKingSquare(Board::WHITE);
score += positionValues[6][56 + 2*(kingSq % Board::COLS) - kingSq];
kingSq = b->getKingSquare(Board::BLACK);
score -= positionValues[6][kingSq];
}
return score;
}