-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEngine.cpp
More file actions
365 lines (319 loc) · 12.9 KB
/
Engine.cpp
File metadata and controls
365 lines (319 loc) · 12.9 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "Engine.h"
Engine::Engine() {
}
Engine::~Engine() {
}
Chessboard Engine::getBestMove(Chessboard board, bool color, int depth) {
MeasuredBoard mb = getInitialMeasure(board);
int16_t alpha = -10000;
int16_t beta = 10000;
if (color == WHITE) {
MeasuredBoard bestBoard(board, alpha);
cout << "Found move scores: ";
for (const MeasuredBoard b : getValidMoves(mb, WHITE)) {
MeasuredBoard current = getEngineScore(b, depth, alpha, beta, WHITE);
cout << (int)current.score << ", ";
if (current.score > bestBoard.score)
bestBoard = current;
}
cout << endl << "Best move score: " << (int)bestBoard.score << endl;
return bestBoard.board;
}
else {
MeasuredBoard bestBoard(board, beta);
cout << "Found move scores: ";
for (const MeasuredBoard b : getValidMoves(mb, BLACK)) {
MeasuredBoard current = getEngineScore(b, depth, alpha, beta, BLACK);
cout << (int)current.score << ", ";
if (current.score < bestBoard.score)
bestBoard = current;
}
cout << endl << "Best move score: " << (int)bestBoard.score << endl;
return bestBoard.board;
}
}
MeasuredBoard Engine::getEngineScore(MeasuredBoard root, uint8_t depth, int16_t alpha, int16_t beta, bool player) {
if (depth == 0)
return root;
if (player == WHITE) {
MeasuredBoard bestBoard(root.board, 10000);
// cout << "Black's possible moves: ";
for (const MeasuredBoard b : getValidMoves(root, BLACK)) {
MeasuredBoard current = getEngineScore(b, depth - 1, alpha, beta, BLACK);
// cout << (int)current.score << ", ";
if (current.score < bestBoard.score) bestBoard.score = current.score;
if (current.score < beta) beta = current.score;
if (beta <= alpha) break;
}
// cout << endl << "Best move score: " << (int)bestBoard.score << endl;
return bestBoard;
}
else {
MeasuredBoard bestBoard(root.board, -10000);
// cout << "White's possible moves: ";
for (const MeasuredBoard b : getValidMoves(root, WHITE)) {
MeasuredBoard current = getEngineScore(b, depth - 1, alpha, beta, WHITE);
// cout << (int)current.score << ", ";
if (current.score > bestBoard.score) bestBoard.score = current.score;
if (current.score > alpha) alpha = current.score;
if (beta <= alpha) break;
}
// cout << endl << "Best move score: " << (int)bestBoard.score << endl;
return bestBoard;
}
}
MeasuredBoard Engine::getInitialMeasure(Chessboard board) const {
MeasuredBoard out;
for (uint8_t y = 0; y < DEFAULT_SIZE; y++) {
for (uint8_t x = 0; x < DEFAULT_SIZE; x++) {
out.score += PIECE_SCORE[getPosition(board, x, y)];
}
}
out.board = board;
return out;
}
unordered_set<MeasuredBoard> Engine::getValidMoves(MeasuredBoard b, bool color) {
unordered_set<MeasuredBoard> moves, current;
uint8_t position;
for (uint8_t y = 0; y < DEFAULT_SIZE; y++) {
for (uint8_t x = 0; x < DEFAULT_SIZE; x++) {
position = getPosition(b.board, x, y);
if (position != EMPTY && getColor(position) == color) {
switch (position & PIECE_MASK) {
case KING:
current = getMovesKing(b, color, x, y);
break;
case QUEEN:
current = getMovesQueen(b, color, x, y);
break;
case ROOK:
current = getMovesRook(b, color, x, y);
break;
case KNIGHT:
current = getMovesKnight(b, color, x, y);
break;
case BISHOP:
current = getMovesBishop(b, color, x, y);
break;
case PAWN:
current = getMovesPawn(b, color, x, y);
break;
default:
cout << "Engine: Error - invalid piece" << endl;
return moves;
}
for (const MeasuredBoard& b : current) {
moves.insert(b);
}
}
}
}
//cout << "Engine: Found " << moves.size() << " valid moves" << endl;
return moves;
}
unordered_set<MeasuredBoard> Engine::getMovesKnight(MeasuredBoard b, bool color, uint8_t x, uint8_t y) {
unordered_set<MeasuredBoard> movesFound;
Move currentMove;
currentMove.fromX = x;
currentMove.fromY = y;
for (uint8_t toX = x - 2; toX <= x + 2; toX+=4) {
if (toX >= DEFAULT_SIZE) continue;
for (uint8_t toY = y - 1; toY <= y + 1; toY+=2) {
if (toY >= DEFAULT_SIZE) continue;
uint8_t position = getPosition(b.board, toX, toY);
if (position != EMPTY && getColor(position) != color) {
MeasuredBoard found(b);
currentMove.toX = toX;
currentMove.toY = toY;
move(found.board, currentMove);
if (!hasCheck(found.board, color)) {
found.score -= PIECE_SCORE[position];
movesFound.insert(found);
}
}
}
}
for (uint8_t toX = x - 1; toX <= x + 1; toX+=2) {
if (toX >= DEFAULT_SIZE) continue;
for (uint8_t toY = y - 2; toY <= y + 2; toY+=4) {
if (toY >= DEFAULT_SIZE) continue;
uint8_t position = getPosition(b.board, toX, toY);
if (position != EMPTY && getColor(position) != color) {
MeasuredBoard found(b);
currentMove.toX = toX;
currentMove.toY = toY;
move(found.board, currentMove);
if (!hasCheck(found.board, color)) {
found.score -= PIECE_SCORE[position];
movesFound.insert(found);
}
}
}
}
return movesFound;
}
unordered_set<MeasuredBoard> Engine::getMovesPawn(MeasuredBoard b, bool color, uint8_t x, uint8_t y) {
unordered_set<MeasuredBoard> movesFound;
MeasuredBoard currentBoard;
Move currentMove(x, y, 0, 0);
int8_t playerOffset = color == WHITE ? 1 : -1;
uint8_t toY = y + playerOffset;
if (toY >= DEFAULT_SIZE) {
return movesFound;
}
// Check front movement
if (getPosition(b.board, x, toY) == EMPTY) {
currentBoard = MeasuredBoard(b);
currentMove.toX = x;
currentMove.toY = toY;
move(currentBoard.board, currentMove);
if (!hasCheck(currentBoard.board, color)) {
movesFound.insert(currentBoard);
}
// Check start double movement
toY += playerOffset;
if (y == PAWN_START_Y[color] && getPosition(b.board, x, toY) == EMPTY) {
currentBoard = MeasuredBoard(b);
currentMove.toX = x;
currentMove.toY = toY;
move(currentBoard.board, currentMove);
if (!hasCheck(currentBoard.board, color)) {
movesFound.insert(currentBoard);
}
}
}
// Check attack movements
currentMove.toY = y + playerOffset;
for (int8_t offsetX = -1; offsetX <= 1; offsetX += 2) {
currentMove.toX = x + offsetX;
uint8_t dest = getPosition(b.board, currentMove.toX, currentMove.toY);
if (dest != EMPTY && getColor(dest) != color) {
currentBoard = MeasuredBoard(b);
move(currentBoard.board, currentMove);
if (!hasCheck(currentBoard.board, color)) {
currentBoard.score -= PIECE_SCORE[dest];
movesFound.insert(currentBoard);
}
}
}
return movesFound;
}
unordered_set<MeasuredBoard> Engine::getMovesRook(MeasuredBoard b, bool color, uint8_t x, uint8_t y) {
unordered_set<MeasuredBoard> movesFound;
MeasuredBoard currentBoard;
Move currentMove(x, y);
bool blocked;
for (int8_t dir = -1; dir <= 1; dir += 2) {
blocked = false;
for (uint8_t toX = x + dir; toX < DEFAULT_SIZE; toX += dir) {
uint8_t position = getPosition(b.board, toX, y);
if (position != EMPTY) {
if (getColor(position) != color)
blocked = true;
else break;
}
currentBoard = MeasuredBoard(b);
currentMove.toX = toX;
currentMove.toY = y;
move(currentBoard.board, currentMove);
if (!hasCheck(currentBoard.board, color)) {
currentBoard.score -= PIECE_SCORE[position];
movesFound.insert(currentBoard);
}
if (blocked) break;
}
for (uint8_t toY = y + dir; toY < DEFAULT_SIZE; toY += dir) {
uint8_t position = getPosition(b.board, x, toY);
if (position != EMPTY) {
if (getColor(position) != color)
blocked = true;
else break;
}
currentBoard = MeasuredBoard(b);
currentMove.toX = x;
currentMove.toY = toY;
move(currentBoard.board, currentMove);
if (!hasCheck(currentBoard.board, color)) {
currentBoard.score -= PIECE_SCORE[position];
movesFound.insert(currentBoard);
}
if (blocked) break;
}
}
return movesFound;
}
unordered_set<MeasuredBoard> Engine::getMovesBishop(MeasuredBoard b, bool color, uint8_t x, uint8_t y) {
unordered_set<MeasuredBoard> movesFound;
MeasuredBoard currentBoard;
Move currentMove(x, y);
for (int8_t dirX = -1; dirX <= 1; dirX += 2) {
for (int8_t dirY = -1; dirY <= 1; dirY += 2) {
currentMove.toX = x + dirX;
currentMove.toY = y + dirY;
while (currentMove.toX < DEFAULT_SIZE && currentMove.toY < DEFAULT_SIZE) {
uint8_t position = getPosition(b.board, currentMove.toX, currentMove.toY);
if (position != EMPTY) {
if (getColor(position) != color) {
currentBoard = MeasuredBoard(b);
move(currentBoard.board, currentMove);
if (!hasCheck(currentBoard.board, color)) {
currentBoard.score -= PIECE_SCORE[position];
movesFound.insert(currentBoard);
}
}
break;
}
currentBoard = MeasuredBoard(b);
move(currentBoard.board, currentMove);
if (!hasCheck(currentBoard.board, color)) {
currentBoard.score -= PIECE_SCORE[position];
movesFound.insert(currentBoard);
}
currentMove.toX += dirX;
currentMove.toY += dirY;
}
}
}
return movesFound;
}
unordered_set<MeasuredBoard> Engine::getMovesQueen(MeasuredBoard b, bool color, uint8_t x, uint8_t y) {
unordered_set<MeasuredBoard> movesFound, temp;
movesFound = getMovesRook(b, color, x, y);
temp = getMovesBishop(b, color, x, y);
for (const MeasuredBoard found : temp) {
movesFound.insert(found);
}
return movesFound;
}
unordered_set<MeasuredBoard> Engine::getMovesKing(MeasuredBoard b, bool color, uint8_t x, uint8_t y) {
unordered_set<MeasuredBoard> movesFound;
MeasuredBoard currentBoard;
Move currentMove(x, y);
for (int8_t dirX = -1; dirX <= 1; dirX++) {
currentMove.toX = x + dirX;
if (currentMove.toX >= DEFAULT_SIZE) {
continue;
}
for (int8_t dirY = -1; dirY <= 1; dirY++) {
currentMove.toY = y + dirY;
if (currentMove.toY >= DEFAULT_SIZE) {
continue;
}
if (currentMove.toX == currentMove.fromX &&
currentMove.toY == currentMove.fromY) {
continue;
}
uint8_t position = getPosition(b.board, currentMove.toX, currentMove.toY);
if (position != EMPTY && getColor(position) == color) {
continue;
}
currentBoard = MeasuredBoard(b);
move(currentBoard.board, currentMove);
if (!hasCheck(currentBoard.board, color)) {
currentBoard.score -= PIECE_SCORE[position];
movesFound.insert(currentBoard);
}
}
}
return movesFound;
}