-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
569 lines (506 loc) · 14.7 KB
/
Board.cpp
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include "Board.h"
#include <iostream>
#include <string>
#include <array>
#include <chrono>
using std::string;
using std::cout;
using std::endl;
using std::vector;
Board::Board() : whiteTurn(true) {
board = new Piece * [64];
for (int i = 0; i < 64; i++) {
board[i] = NULL;
}
PreComputeMoveData();
}
Board::~Board() {
for (int i = 0; i < 64; i++) {
delete board[i];
}
delete board;
for (int i = 0; i < 64; i++) {
delete numToTheEdge[i];
}
delete numToTheEdge;
}
void Board::printBoard() {
cout << endl;
int pos = 0;
while (pos < 64) {
for (int i = 0; i < 8; i++) {
//cout << pos << endl;
if (board[pos] == NULL) {
//empty square, print a _
cout << " _";
}
else {
cout << " " << board[pos]->pieceName;
}
pos++;
}
//end the line
cout << endl;
}
cout << endl;
}
bool Board::setPositionFEN(std::string FENSTring) {
unsigned int posOnBoard = 0;
Piece** newBoard = new Piece* [64];
for (int i = 0; i < 64; i++) {
delete board[i];
}
delete board;
for (unsigned int i = 0; i < FENSTring.length(); i++) {
unsigned char current = FENSTring.at(i);
if (current == '/') {
//skip to next rank
while (posOnBoard % 8 != 0) {
newBoard[posOnBoard] = NULL;
posOnBoard++;
}
}
else if (isdigit(current)) {
int amountToSkip = current - 48;
for (int i = 0; i < amountToSkip; i++) {
newBoard[posOnBoard] = NULL;
posOnBoard++;
}
}
else {
int colour = Piece::Black;
if (isupper(current)) {
current = tolower(current);
colour = Piece::White;
}
int pieceType = 0;
//find what piece it is
switch (current) {
case 'k':
//king
pieceType = Piece::King;
break;
case 'p':
//pawn
pieceType = Piece::Pawn;
break;
case 'n':
//knight
pieceType = Piece::Knight;
break;
case 'b':
//bishop
pieceType = Piece::Bishop;
break;
case 'r':
//rook
pieceType = Piece::Rook;
break;
case 'q':
//queen
pieceType = Piece::Queen;
break;
}
newBoard[posOnBoard] = new Piece(pieceType | colour);
//cout << newBoard[posOnBoard]->getPieceName() << posOnBoard << "Hello?" << endl;
posOnBoard++;
}
}
board = newBoard;
return true;
}
bool Board::ManualMove(int from, int to) {
//returns false if it is an illegal move
vector<Move> moves;
moves = GenerateLegalMoves();
for (Move cmove : moves) {
if (cmove.from == from && cmove.to == to) {
MakeMove(cmove);
return true;
}
}
//was not in the legal moves, therfore illegal
return false;
}
void Board::MakeMove(Move move) {
moveHistory.push_back(MoveInHistory(move.from, move.to, board[move.to]));
board[move.to] = board[move.from];
board[move.from] = NULL;
whiteTurn = !whiteTurn;
}
void Board::UndoMove() {
MoveInHistory move = moveHistory.back();
board[move.from] = board[move.to];
board[move.to] = move.pOnTo;
moveHistory.pop_back();
whiteTurn = !whiteTurn;
}
vector<Move> Board::GenerateLegalMoves() {
vector<Move> allMoves;
allMoves = generateAllMoves();
filterMoves(allMoves);
/*
for (Move cmove : legalMoves) {
cout << cmove.from << " to " << cmove.to << endl;
}
*/
//cout << "number of legal moves: " << allMoves.size() << endl;
return allMoves;
}
vector<Move> Board::generateAllMoves() {
//cout << "Starting generation" << endl;
vector<Move> moves;
for (int fpos = 0; fpos < 64; fpos++) {
//if you are on a piece that is also the colour that it is to move
if (board[fpos] != NULL && (board[fpos]->isWhite == whiteTurn)) {
//if it is a sliding piece, the array that it will read off of will not b emtpy
int pieceNum = board[fpos]->identity % 8;
if (pieceNum >= 4) {
//sliding piece
generateSlidingMoves(moves, fpos);
}
else if (pieceNum == Piece::King) {
generateKingMoves(moves, fpos);
}
else if (pieceNum == Piece::Knight) {
generateKnightMoves(moves, fpos);
}
else if (pieceNum == Piece::Pawn) {
if (board[fpos]->isWhite) {
generateWhitePawnMoves(moves, fpos);
}
else {
generateBlackPawnMoves(moves, fpos);
}
}
}
}
//cout << "finished genration" << endl;
return moves;
}
void Board::generateSlidingMoves(vector<Move>& legalMoves, int fpos) {
int pieceDirectionsi = board[fpos]->identity % 8 - 4;
bool colour = board[fpos]->isWhite;
for (int i = 0; i < sizeof(Piece::HowPiecesCanMove[pieceDirectionsi]) / sizeof(Piece::HowPiecesCanMove[pieceDirectionsi][0]); i++) {
int direction = Piece::HowPiecesCanMove[pieceDirectionsi][i];
if (direction != 10) {
//go in the direction until the edge
int pos = fpos;
for (int distance = numToTheEdge[fpos][direction]; distance > 0; distance--) {
//slide until you hit self or enemy
pos += directionOffsets[direction];
//if you are on a friendly piece
if (board[pos] != NULL && (board[pos]->isWhite == colour)) {
//on friendly, impossible move
break;
}
//legal move, add to moves
legalMoves.push_back(Move(fpos, pos));
//see if you are on a opponents pice, if so this move is legal but you cant go any farther
if (board[pos] != NULL && (board[pos]->isWhite != colour)) {
//on opponent, dont go farther
break;
}
}
}
}
}
void Board::generateKingMoves(vector<Move>& legalMoves, int fpos) {
for (int direction = 0; direction < 8; direction++) {
//if there is spae in that direction on the board
if (numToTheEdge[fpos][direction] >= 1) {
//if its empty or its an opponent
int npos = fpos + directionOffsets[direction];
if (board[npos] == NULL || board[fpos]->isWhite != board[npos]->isWhite) {
//legal move
legalMoves.push_back(Move(fpos, npos));
}
}
}
}
void Board::generateKnightMoves(std::vector<Move>& legalMoves, int fpos) {
for (int direction = 0; direction < 8; direction++) {
//if that square is possible
if (numToTheEdge[fpos][knightNumToTheEdgeDirections[direction][0]] >= 2 && numToTheEdge[fpos][knightNumToTheEdgeDirections[direction][1]] >= 1) {
//possible square, see if it is empty or an opponent
int npos = fpos + knightOffsets[direction];
if (board[npos] == NULL || board[fpos]->isWhite != board[npos]->isWhite) {
legalMoves.push_back(Move(fpos, npos));
}
}
}
}
void Board::generateWhitePawnMoves(std::vector<Move>& legalMoves, int fpos) {
//single
int npos = fpos + directionOffsets[1];
if (numToTheEdge[fpos][1] >= 1 && board[npos] == NULL) {
legalMoves.push_back(Move(fpos, npos));
//see if you can go twice
npos += directionOffsets[1];
bool onStartingRank = numToTheEdge[fpos][6] == 1;
if (onStartingRank && board[npos] == NULL) {
legalMoves.push_back(Move(fpos, npos));
}
}
npos = fpos + directionOffsets[0];
//diagonal captue moves
if (numToTheEdge[fpos][0] >= 1 && board[npos] != NULL && !board[npos]->isWhite) {
legalMoves.push_back(Move(fpos, npos));
}
npos = fpos + directionOffsets[2];
if (numToTheEdge[fpos][2] >= 1 && board[npos] != NULL && !board[npos]->isWhite) {
legalMoves.push_back(Move(fpos, npos));
}
}
void Board::generateBlackPawnMoves(std::vector<Move>& legalMoves, int fpos) {
//single
int npos = fpos + directionOffsets[6];
if (numToTheEdge[fpos][6] >= 1 && board[npos] == NULL) {
legalMoves.push_back(Move(fpos, npos));
//see if you can go twice
npos += directionOffsets[6];
bool onStartingRank = numToTheEdge[fpos][1] == 1;
if (onStartingRank && board[npos] == NULL) {
legalMoves.push_back(Move(fpos, npos));
}
}
npos = fpos + directionOffsets[5];
//diagonal captue moves
if (numToTheEdge[fpos][5] >= 1 && board[npos] != NULL && board[npos]->isWhite) {
legalMoves.push_back(Move(fpos, npos));
}
npos = fpos + directionOffsets[7];
if (numToTheEdge[fpos][7] >= 1 && board[npos] != NULL && board[npos]->isWhite) {
legalMoves.push_back(Move(fpos, npos));
}
}
void Board::filterMoves(std::vector<Move>& moves) {
//make each move, generate the legal moves, then filter illegal moves
//make move
auto i = moves.begin();
while (i != moves.end()) {
MakeMove(*i);
//see if it would be an illegal move
bool valid = !kingInCheck(!whiteTurn);
//unmake move
UndoMove();
if (valid) {
++i;
}
else {
i = moves.erase(i);
}
}
}
bool Board::kingInCheck(bool white) {
//find king
int kingpos = 0;
while (kingpos < 64) {
if (board[kingpos] != NULL && board[kingpos]->identity % 8 == Piece::King && board[kingpos]->isWhite == white) {
break;
}
kingpos++;
}
//if in check by a knight
for (int direction = 0; direction < 8; direction++) {
//if that square is possible
if (numToTheEdge[kingpos][knightNumToTheEdgeDirections[direction][0]] >= 2 && numToTheEdge[kingpos][knightNumToTheEdgeDirections[direction][1]] >= 1) {
//possible square, see if it is empty or an opponent
int npos = kingpos + knightOffsets[direction];
if (board[npos] != NULL && white != board[npos]->isWhite && board[npos]->identity % 8 == Piece::Knight) {
//in check by knight
return true;
}
}
}
int npos;
//pawn check
if (white) {
//up left
npos = kingpos + directionOffsets[0];
if (board[npos] != NULL && board[npos]->identity == Piece::Pawn + Piece::Black) {
return true;
}
//up right
npos = kingpos + directionOffsets[2];
if (board[npos] != NULL && board[npos]->identity == Piece::Pawn + Piece::Black) {
return true;
}
}
else {
//down left
npos = kingpos + directionOffsets[5];
if (board[npos] != NULL && board[npos]->identity == Piece::Pawn + Piece::White) {
return true;
}
//down right
npos = kingpos + directionOffsets[7];
if (board[npos] != NULL && board[npos]->identity == Piece::Pawn + Piece::White) {
return true;
}
}
//check for sliding pieces
for (int direction = 0; direction < 8; direction++) {
//go in the direction until the edge
int pos = kingpos;
for (int distance = numToTheEdge[kingpos][direction]; distance > 0; distance--) {
//slide until you hit self or enemy
pos += directionOffsets[direction];
//if you are on a friendly piece
if (board[pos] != NULL && (board[pos]->isWhite == white)) {
//on friendly, not in check from here
break;
}
//see if you are on a opponents pice, if so this move is legal but you cant go any farther
if (board[pos] != NULL && (board[pos]->isWhite != white)) {
//on opponent, see if it sees you
//if it is a queen
if (board[pos]->identity % 8 == Piece::Queen) {
return true;
}
//check other types
if (direction == 0 || direction == 2 || direction == 5 || direction == 7) {
//on a diagonal move, if bishop - in check
if (board[pos]->identity % 8 == Piece::Bishop) {
return true;
}
}
else {
//on a straight move, if rook - in check
if (board[pos]->identity % 8 == Piece::Rook) {
return true;
}
}
break;
}
}
}
return false;
}
int Board::Evaluate(bool captures) {
//go until there are no captures remaining
int movesMade = 0;
if (captures) {
movesMade = moveUntilNoCaptures();
}
int white = 0;
int black = 0;
for (int pos = 0; pos < 64; pos++) {
if (board[pos] != NULL) {
//add into eval
if (board[pos]->isWhite) {
white += board[pos]->value * 100 + whitePostionBonuses[board[pos]->identity % 8 - 1][pos];
}
else {
black += board[pos]->value * 100 + blackPositionBonuses[board[pos]->identity % 8 - 1][pos];
}
}
}
//eval has been made, return back to the position that was given
for (int i = 0; i < movesMade; i++) {
UndoMove();
}
//printBoard();
if (!captures) {
return white - black;
}
//this is the first call
//higher is better ofr white, so give the one that is better for the player whos turn it is
int nocaptures = Evaluate(false);
int thisEval = white - black;
if (whiteTurn) {
return thisEval > nocaptures ? thisEval : nocaptures;
}
else {
return thisEval > nocaptures ? nocaptures : thisEval;
}
//cout << "this pos is at " << white - black << endl;
//cout << "white is " << white << endl;
//cout << "black is " << black << endl;
//cout << "had made " << movesMade << " moves to do all captures before evaluating materially" << endl;
}
int Board::moveUntilNoCaptures() {
int movesMade = 0;
bool capturing = true;
while (capturing) {
capturing = false;
vector<Move> moves = GenerateLegalMoves();
for (Move cmove : moves) {
if (board[cmove.to] != NULL) {
MakeMove(cmove);
movesMade++;
capturing = true;
break;
}
}
//no captures possible in this postion on this turn
}
return movesMade;
}
Move Board::findBestMove(int depth) {
auto start = std::chrono::high_resolution_clock::now();
postionsEvaluated = 0;
vector<Move> moves = GenerateLegalMoves();
std::size_t bestMovei = 0;
int bestMoveEval = -10000000;
//search for the best eval for this move
int movesLookedAt = 1;
for (std::size_t i = 0; i < moves.size(); ++i) {
//for each move, find the best move
MakeMove(moves[i]);
int thisEval = findBestMoveEval(depth - 1, -100000, 100000) * -1;
cout << "done " << movesLookedAt << " moves" << endl;
if (thisEval > bestMoveEval) {
bestMoveEval = thisEval;
bestMovei = i;
}
UndoMove();
movesLookedAt++;
}
cout << "searched " << postionsEvaluated << " postions" << endl;
auto end = std::chrono::high_resolution_clock::now();
auto timeTaken = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
cout << "what" << endl;
cout << "took " << timeTaken.count() << " milliseconds to find the best move" << endl;
return moves[bestMovei];
}
int Board::findBestMoveEval(int depth, int alpha, int beta) {
if (depth == 0) {
//base case
postionsEvaluated++;
int eval = Evaluate();
if (whiteTurn){
return eval;
}
return eval * -1;
}
vector<Move> moves = GenerateLegalMoves();
//check to se if you are mated
if (moves.empty()) {
if (kingInCheck(whiteTurn)) {
return -100000000;
}
return 0;
}
int bestMoveEval = -1000000000;
//search for the best eval for this move
for (std::size_t i = 0; i < moves.size(); ++i) {
//for each move, find the best move
MakeMove(moves[i]);
int thisEval = findBestMoveEval(depth - 1, -beta, -alpha) * -1;
if (thisEval > bestMoveEval) {
bestMoveEval = thisEval;
alpha = thisEval;
}
UndoMove();
if (thisEval >= beta) {
return beta;//move was too good opponent will avoid it
}
}
return bestMoveEval;
}
int Board::convert(string str) {
int file = str.at(0) - 'a';
int rank = str.at(1) - '0';
return file + (8 - rank) * 8;
}