-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.cpp
1501 lines (1314 loc) · 53.9 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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#define _HUGE_ENUF 1e+300
#define INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF))
using namespace std;
// Forward declaration of class so that
// I can do a function declaration for create_piece_from_char so I can
// use it in hasKingsideCastlingRights
class Piece;
string coordinate_to_algebraic(int coordinate);
Piece* create_piece_from_char(char piece_char, int position);
struct BoardState {
string board;
char active_color;
string castling_rights;
string en_passant;
int halfmove;
int fullmove;
};
bool isSideInCheck(const string& board, bool isWhite);
int game_over(const BoardState& boardState);
int findKing(const string& board, bool isWhite) {
string kingAliasesWhite = "KWUS";
string kingAliasesBlack = "kwus";
// Choose the appropriate king aliases based on the color
const string& kingAliases = isWhite ? kingAliasesWhite : kingAliasesBlack;
for (size_t i = 0; i < board.size(); ++i) {
char piece = board[i];
if (kingAliases.find(piece) != string::npos) {
return static_cast<int>(i);
}
}
// Return -1 if no king found
return -1;
}
void print_board(const string& board) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
int index = row * 8 + col;
cout << board[index] << " ";
}
cout << endl;
}
}
pair<int, char> unpackPromotionIndex(int toIndex) {
char promotionPiece = '\0'; // Default to no promotion
if (toIndex >= 64 && toIndex <= 127) {
int col = (toIndex - 64) / 4;
int pieceType = (toIndex - 64) % 4;
// Convert index back to a normal in-bounds index
toIndex = (toIndex < 96) ? col : 56 + col; // Row 0 for white, row 7 for black
// Set the promotion piece
switch (pieceType) {
case 0: promotionPiece = 'q'; break;
case 1: promotionPiece = 'n'; break;
case 2: promotionPiece = 'r'; break;
case 3: promotionPiece = 'b'; break;
}
}
return {toIndex, promotionPiece};
}
string movePiece(string board, int fromIndex, int toIndex, char promote_to = '\0') {
int fromRow = fromIndex / 8;
int fromCol = fromIndex % 8;
int toRow = toIndex / 8;
int toCol = toIndex % 8;
string kingAliases = "SWUswu"; // Kings and their castling rights
string newBoard = board; // Create a copy of the board to make changes
// Revoke en passant eligibility on all pawns
for (int i = 0; i < 64; ++i) {
if (newBoard[i] == 'E') newBoard[i] = 'P';
else if (newBoard[i] == 'e') newBoard[i] = 'p';
}
// Check if there's a piece at the fromPosition
char piece = newBoard[fromIndex];
if (piece == '.') {
cerr << "Tried to move a piece that doesn't exist." << endl;
return board; // No piece to move, return the original board
}
// Handle pawn-specific logic (promotion, en passant)
if (piece == 'P' || piece == 'p') {
// Check for pawn advancing two squares (for en passant)
if (abs(toRow - fromRow) == 2) {
piece = (piece == 'P') ? 'E' : 'e'; // Make it en passant eligible
}
// Handle en passant capture
else if (toCol != fromCol && newBoard[toIndex] == '.') {
int capturedPawnIndex = (piece == 'P') ? (toIndex + 8) : (toIndex - 8);
newBoard[capturedPawnIndex] = '.'; // Remove captured pawn
}
// Handle pawn promotion
else if ((toRow == 0 || toRow == 7) && promote_to != '\0') {
piece = promote_to; // Promote to the specified piece
}
}
// Handle rook moving (to revoke castling rights)
else if ((piece == 'R' || piece == 'r') && (fromIndex == 0 || fromIndex == 7 || fromIndex == 56 || fromIndex == 63)) {
int kingPos = isupper(piece) ? findKing(newBoard, true) : findKing(newBoard, false);
if (kingPos == (isupper(piece) ? 60 : 4)) { // Revoke castling rights for the respective king
bool isQueenside = (fromCol == 0);
char& kingRights = newBoard[kingPos];
if (isQueenside) {
kingRights = (kingRights == 'W' || kingRights == 'w') ? (isupper(piece) ? 'S' : 's') : (isupper(piece) ? 'K' : 'k');
} else {
kingRights = (kingRights == 'W' || kingRights == 'w') ? (isupper(piece) ? 'U' : 'u') : (isupper(piece) ? 'K' : 'k');
}
}
}
// Handle king moving (castling rights and castling move)
else if (kingAliases.find(piece) != string::npos) {
piece = isupper(piece) ? 'K' : 'k'; // Revoke all castling rights
// Handle castling
if (abs(fromCol - toCol) == 2) { // If the king moved two squares (castling)
int rookFrom, rookTo;
if (toCol == 2) { // Queenside castling
rookFrom = isupper(piece) ? 56 : 0;
rookTo = isupper(piece) ? 59 : 3;
} else { // Kingside castling
rookFrom = isupper(piece) ? 63 : 7;
rookTo = isupper(piece) ? 61 : 5;
}
newBoard[rookTo] = newBoard[rookFrom]; // Move the rook
newBoard[rookFrom] = '.'; // Clear old rook position
}
}
// Update the board with the move
newBoard[fromIndex] = '.';
newBoard[toIndex] = piece;
return newBoard;
}
string simulateMove(const string& board, int fromPosition, int toPosition) {
auto [realToIndex, promotionPiece] = unpackPromotionIndex(toPosition);
string newBoard = movePiece(board, fromPosition, realToIndex, promotionPiece);
return newBoard;
}
class Piece {
public:
Piece(bool isWhite, int position) : isWhite(isWhite), position(position) {}
virtual set<int> validMoves(const string& board) const {
set<int> validMoves;
set<int> inVisionPositions = this->inVisionPositions(board);
for (int newPosition : inVisionPositions) {
// Check if this move puts us in check
string hypotheticalBoard = simulateMove(board, position, newPosition);
if (isSideInCheck(hypotheticalBoard, isWhite))
{
// If so, don't add it to the list of valid moves.
continue;
}
int newRow = newPosition / 8;
int newCol = newPosition % 8;
if (board[newRow * 8 + newCol] == '.') { // If it's empty, add it.
validMoves.insert(newPosition);
} else if (!isWhite && isupper(board[newRow * 8 + newCol])) { // If it's an enemy, add it.
validMoves.insert(newPosition);
} else if (isWhite && islower(board[newRow * 8 + newCol])) { // If it's an enemy, add it.
validMoves.insert(newPosition);
}
}
return validMoves;
}
int getPosition() const {
return position;
}
bool getIsWhite() const {
return isWhite;
}
virtual set<int> inVisionPositions(const string& board) const {
// Returns a set of squares that are occupied by allies
// that are defended by this piece. Used for checking legal
// king moves.
return set<int>();
}
private:
bool isWhite;
int position;
};
class Rook : public Piece {
public:
Rook(bool isWhite, int position) : Piece(isWhite, position) {}
set<int> inVisionPositions(const string& board) const override {
set<int> inVisionPositions;
int row = getPosition() / 8;
int col = getPosition() % 8;
int directions[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
for (const auto& dir : directions) {
int dr = dir[0];
int dc = dir[1];
int newRow = row + dr;
int newCol = col + dc;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
int newPosition = newRow * 8 + newCol;
char piece = board[newPosition];
inVisionPositions.insert(newPosition);
if (piece != '.') {
break; // Stop if there's a piece in the way
}
newRow += dr;
newCol += dc;
}
}
return inVisionPositions;
}
};
class Pawn : public Piece {
public:
// Constructor
Pawn(bool isWhite, int position) : Piece(isWhite, position) {}
// set<int> inVisionPositions(const string& board) const override;
// Implementing the in_vision_positions function
set<int> inVisionPositions(const string& board) const override{
set<int> inVisionPositions;
int row = getPosition() / 8;
int col = getPosition() % 8;
// Determine the direction based on the pawn's color
int direction = (getIsWhite()) ? -1 : 1;
// Check diagonal captures
for (int offset : {-1, 1}) {
int newCol = col + offset;
int newRow = row + direction;
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
int newPosition = newRow * 8 + newCol;
inVisionPositions.insert(newPosition);
}
}
return inVisionPositions;
}
set<int> validMoves(const string& board) const override{
set<int> validMoves;
int row = getPosition() / 8;
int col = getPosition() % 8;
// Determine the direction based on the pawn's color
int direction = (getIsWhite()) ? -1 : 1;
// Check one square forward
int newRow = row + direction;
// Only add forward moves if the square is empty
if (board[newRow * 8 + col] == '.') {
//Checks for queening.
//We don't need to know what color we are because only
//white pawns can ever be on row 0 and black pawns on row 7
switch (newRow){
case 0:
for(int i = 0; i < 4; i++){
validMoves.insert(96 + (col*4) + i); //Add all 4 queening moves
}
break;
case 7:
for(int i = 0; i < 4; i++){
validMoves.insert(64 + (col*4) + i); //Add all 4 queening moves
}
break;
default:
validMoves.insert(newRow * 8 + col);
}
// Check two squares forward (only on the first move)
if ((row == 6 && getIsWhite()) || (row == 1 && !getIsWhite())) {
int newRow2 = row + 2 * direction;
if (board[newRow2 * 8 + col] == '.') {
validMoves.insert(newRow2 * 8 + col);
}
}
}
// Check diagonal captures
for (int offset : {-1, 1}) {
int newCol = col + offset;
if (0 <= newRow && newRow < 8 && 0 <= newCol && newCol < 8) { //If we're in bounds
char piece = board[newRow * 8 + newCol];
if ((getIsWhite() && islower(piece)) || (!getIsWhite() && isupper(piece))) { //If the piece is enemy
//If we can capture onto the end rank, then we must promote.
switch (newRow){
case 0:
for(int i = 0; i < 4; i++){
validMoves.insert(64 + (newCol*4) + i); //Add all 4 queening moves
}
break;
case 7:
for(int i = 0; i < 4; i++){
validMoves.insert(96 + (newCol*4) + i); //Add all 4 queening moves
}
break;
default: //The row is not a queening square
validMoves.insert(newRow * 8 + newCol);
}
}
}
}
// Check for en passant captures
int enPassantRow = (getIsWhite()) ? 3 : 4; // Row where en passant captures are possible
if (row == enPassantRow) {
for (int offset : {-1, 1}) {
int newCol = col + offset;
if (0 <= newCol && newCol < 8) {
char piece = board[row * 8 + newCol];
if ((getIsWhite() && piece == 'e') || (!getIsWhite() && piece == 'E')) {
validMoves.insert((row + direction) * 8 + newCol);
}
}
}
}
// Make sure the move doesn't put us in check.
set<int> legal_moves;
for (int newPosition : validMoves) {
string hypotheticalBoard = simulateMove(board, getPosition(), newPosition);
if (isSideInCheck(hypotheticalBoard, getIsWhite()))
{
// If so, don't add it to the list of valid moves.
continue;
}
legal_moves.insert(newPosition);
}
return legal_moves;
}
void setEnPassantEligibility(bool eligibility){
this->enPassantEligible = eligibility;
}
private:
bool enPassantEligible = false;
};
class Knight : public Piece {
public:
// Constructor
Knight(bool isWhite, int position) : Piece(isWhite, position) {}
// Implementing the in_vision_positions function
set<int> inVisionPositions(const string& board) const override {
set<int> inVisionPositions;
int row = getPosition() / 8;
int col = getPosition() % 8;
// Define the possible knight moves
vector<pair<int, int>> knightMoves = {
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
{1, -2}, {1, 2}, {2, -1}, {2, 1}
};
for (const auto& move : knightMoves) {
int dr = move.first;
int dc = move.second;
int newRow = row + dr;
int newCol = col + dc;
bool isInBounds = (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8);
if (isInBounds) {
int newPosition = newRow * 8 + newCol;
inVisionPositions.insert(newPosition);
}
}
return inVisionPositions;
}
};
class Bishop : public Piece {
public:
// Constructor
Bishop(bool isWhite, int position) : Piece(isWhite, position) {}
// Implementing the in_vision_positions function for Bishop
set<int> inVisionPositions(const string& board) const override {
set<int> inVisionPositions;
int row = getPosition() / 8;
int col = getPosition() % 8;
// Define the four diagonal directions
vector<pair<int, int>> directions = {
{-1, -1}, {-1, 1}, {1, -1}, {1, 1}
};
for (const auto& direction : directions) {
int dr = direction.first;
int dc = direction.second;
int newRow = row + dr;
int newCol = col + dc;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
int newPosition = newRow * 8 + newCol;
char piece = board[newPosition];
inVisionPositions.insert(newPosition);
if (piece != '.') {
break; // Stop if there's a piece in the way
}
newRow += dr;
newCol += dc;
}
}
return inVisionPositions;
}
// Add any other member variables or functions you need
};
class Queen : public Piece {
public:
// Constructor
Queen(bool isWhite, int position) : Piece(isWhite, position) {}
// Implementing the in_vision_positions function for Queen
set<int> inVisionPositions(const string& board) const override {
set<int> inVisionPositions;
int row = getPosition() / 8;
int col = getPosition() % 8;
// Define the eight possible directions (horizontal, vertical, and diagonal)
vector<pair<int, int>> directions = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
for (const auto& dir : directions) {
int dr = dir.first;
int dc = dir.second;
int newRow = row + dr;
int newCol = col + dc;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
int newPosition = newRow * 8 + newCol;
char piece = board[newPosition];
inVisionPositions.insert(newPosition);
if (piece != '.') {
break;
}
newRow += dr;
newCol += dc;
}
}
return inVisionPositions;
}
// Implement the getPosition function if needed
// int getPosition() const { return position; }
// Add any other member variables or functions you need
};
class King : public Piece {
public:
// Constructor
King(bool isWhite, int position) : Piece(isWhite, position) {}
// Implementing the in_vision_positions function for King
set<int> inVisionPositions(const string& board) const override {
set<int> inVisionPositions;
int row = getPosition() / 8;
int col = getPosition() % 8;
// Define the possible king moves
vector<pair<int, int>> kingMoves = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
for (const auto& move : kingMoves) {
int dr = move.first;
int dc = move.second;
int newRow = row + dr;
int newCol = col + dc;
bool isInBounds = (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8);
if (isInBounds) {
int newPosition = newRow * 8 + newCol;
inVisionPositions.insert(newPosition);
}
}
return inVisionPositions;
}
void setKingsideCastlingRights(bool hasRights){
this->hasKingsideCastlingRights = hasRights;
}
void setQueensideCastlingRights(bool hasRights){
this->hasQueensideCastlingRights = hasRights;
}
bool getKingsideCastlingRights() const{
return this->hasKingsideCastlingRights;
}
bool getQueensideCastlingRights() const{
return this->hasQueensideCastlingRights;
}
// Check if kingside castling is valid
bool canCastleKingside(const string& board) const{
if (!getKingsideCastlingRights() || isSideInCheck(board, getIsWhite())) {
return false;
}
//Check to make sure the rook is actually there, too
int kingsideRookPosition = getIsWhite() ? 63 : 7;
char rookChar = getIsWhite() ? 'R' : 'r';
if(board[kingsideRookPosition] != rookChar){
return false;
}
// Check if the squares between the king and kingside rook are empty
for (int col = getPosition() + 1; col < kingsideRookPosition; ++col) {
if (board[col] != '.') {
return false;
}
}
// Check if none of the enemy piece's inVisionPositions attack the squares involved in kingside castling
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
char piece = board[row * 8 + col];
if ((!getIsWhite() && isupper(piece)) || (getIsWhite() && islower(piece))) {
Piece* enemyPiece = create_piece_from_char(piece, row * 8 + col);
set<int> enemyPieceVision = enemyPiece->inVisionPositions(board);
// Check if enemyPieceVision includes any of the squares involved in kingside castling
if (enemyPieceVision.find(getPosition()) != enemyPieceVision.end() ||
enemyPieceVision.find(getPosition() + 1) != enemyPieceVision.end() ||
enemyPieceVision.find(getPosition() + 2) != enemyPieceVision.end()) {
delete enemyPiece; // Don't forget to delete the created piece object
return false;
}
delete enemyPiece; // Don't forget to delete the created piece object
}
}
}
return true;
}
// Check if queenside castling is valid
bool canCastleQueenside(const string& board) const{
if (!getQueensideCastlingRights() || isSideInCheck(board, getIsWhite())) {
return false;
}
//Check to make sure the rook is actually there, too
int queensideRookPosition = getIsWhite() ? 56 : 0;
char rookChar = getIsWhite() ? 'R' : 'r';
if(board[queensideRookPosition] != rookChar){
return false;
}
// Check if the squares between the king and queenside rook are empty
for (int col = getPosition() - 1; col > queensideRookPosition; --col) {
if (board[col] != '.') {
return false;
}
}
// Check if none of the enemy piece's inVisionPositions attack the squares involved in queenside castling
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
char piece = board[row * 8 + col];
if ((!getIsWhite() && isupper(piece)) || (getIsWhite() && islower(piece))) {
Piece* enemyPiece = create_piece_from_char(piece, row * 8 + col);
set<int> enemyPieceVision = enemyPiece->inVisionPositions(board);
// Check if enemyPieceVision includes any of the squares involved in queenside castling
if (enemyPieceVision.find(getPosition()) != enemyPieceVision.end() ||
enemyPieceVision.find(getPosition() - 1) != enemyPieceVision.end() ||
enemyPieceVision.find(getPosition() - 2) != enemyPieceVision.end()) {
delete enemyPiece; // Don't forget to delete the created piece object
return false;
}
delete enemyPiece; // Don't forget to delete the created piece object
}
}
}
return true;
}
set<int> validMoves(const string& board) const override{
set<int> validMoves;
// Get all the positions the King can "see"
set<int> visionPositions = this->inVisionPositions(board);
for (int newPosition : visionPositions) {
string hypotheticalBoard = simulateMove(board, getPosition(), newPosition);
if (isSideInCheck(hypotheticalBoard, getIsWhite()))
{
// If so, don't add it to the list of valid moves.
continue;
}
char piece = board[newPosition];
if (piece == '.' || (getIsWhite() && islower(piece)) || (!getIsWhite() && isupper(piece))) {
// Empty square or enemy piece
validMoves.insert(newPosition);
}
}
int kingsideRookPosition = getIsWhite() ? 63 : 7;
int queensideRookPosition = getIsWhite() ? 56 : 0;
// Check for castling kingside
if (canCastleKingside(board)) {
validMoves.insert(kingsideRookPosition-1);
}
// Check for castling queenside
if (canCastleQueenside(board)) {
validMoves.insert(queensideRookPosition+2);
}
return validMoves;
}
private:
bool hasQueensideCastlingRights = true;
bool hasKingsideCastlingRights = true;
};
Piece* create_piece_from_char(char piece_char, int position) {
switch (piece_char) {
case 'P':
return new Pawn(true, position);
case 'p':
return new Pawn(false, position);
case 'E':
{
Pawn* pawn = new Pawn(true, position);
pawn->setEnPassantEligibility(true);
return pawn;
}
case 'e':
{
Pawn* pawn = new Pawn(false, position);
pawn->setEnPassantEligibility(true);
return pawn;
}
case 'K':
{
King* king = new King(true, position);
king->setQueensideCastlingRights(false);
king->setKingsideCastlingRights(false);
return king;
}
case 'k':
{
King* king = new King(false, position);
king->setQueensideCastlingRights(false);
king->setKingsideCastlingRights(false);
return king;
}
case 'S':
{
King* king = new King(true, position);
king->setQueensideCastlingRights(false);
king->setKingsideCastlingRights(true);
return king;
}
case 's':
{
King* king = new King(false, position);
king->setQueensideCastlingRights(false);
king->setKingsideCastlingRights(true);
return king;
}
case 'U':
{
King* king = new King(true, position);
king->setQueensideCastlingRights(true);
king->setKingsideCastlingRights(false);
return king;
}
case 'u':
{
King* king = new King(false, position);
king->setKingsideCastlingRights(true);
king->setQueensideCastlingRights(false);
return king;
}
case 'W':
{
King* king = new King(true, position);
king->setQueensideCastlingRights(true);
king->setKingsideCastlingRights(true);
return king;
}
case 'w':
{
King* king = new King(false, position);
king->setQueensideCastlingRights(true);
king->setKingsideCastlingRights(true);
return king;
}
case 'R':
return new Rook(true, position);
case 'r':
return new Rook(false, position);
case 'N':
return new Knight(true, position);
case 'n':
return new Knight(false, position);
case 'B':
return new Bishop(true, position);
case 'b':
return new Bishop(false, position);
case 'Q':
return new Queen(true, position);
case 'q':
return new Queen(false, position);
default:
return nullptr; // Return nullptr for empty squares ('.')
}
}
bool isSideInCheck(const string& board, bool isWhite) {
// cout << "is side in check called " << endl;
int kingPosition = findKing(board, isWhite);
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
char pieceChar = board[row * 8 + col];
if ((isWhite && islower(pieceChar)) || (!isWhite && isupper(pieceChar))) {
// Create a Piece object for the enemy piece
Piece* enemyPiece = create_piece_from_char(pieceChar, row * 8 + col);
// Calculate the vision of the enemy piece
set<int> enemyPieceVision = enemyPiece->inVisionPositions(board);
if (enemyPieceVision.find(kingPosition) != enemyPieceVision.end()) {
delete enemyPiece; // Clean up the dynamically allocated object
return true; // Side is in check
}
delete enemyPiece; // Clean up the dynamically allocated object
}
}
}
return false; // Side is not in check
}
// Function to print vision boards for each piece on the board
void printVisionBoards(const string& board) {
for (int i = 0; i < 64; ++i) {
char piece_char = board[i];
if (piece_char == '.') {
continue; // Skip empty squares
}
// Create a piece object from the character on the board
Piece* piece = create_piece_from_char(piece_char, i);
if (piece) {
cout << "=============================" << endl;
print_board(board);
cout << endl << "Vision board for piece at position " << piece->getPosition() << ":\n";
// Calculate and print the vision board
set<int> visionPositions = piece->inVisionPositions(board);
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
int position = row * 8 + col;
char square = (visionPositions.find(position) != visionPositions.end()) ? 'x' : '.';
if(row * 8 + col == i){
square = piece_char;
}
cout << square << ' ';
}
cout << endl;
}
delete piece; // Clean up the dynamically allocated piece
}
}
}
void printValidMovesBoards(const string& board, bool isWhite) {
for (int i = 0; i < 64; ++i) {
char pieceChar = board[i];
if ((isWhite && isupper(pieceChar)) || (!isWhite && islower(pieceChar))) {
// Create the corresponding piece
Piece* piece = create_piece_from_char(pieceChar, i);
// Get valid moves for the piece
set<int> validMoves = piece->validMoves(board);
set<int>::iterator itr;
// Displaying set elements
for (itr = validMoves.begin();
itr != validMoves.end(); itr++)
{
cout << *itr << " ";
}
// // Create a board with X for valid moves
// string validMovesBoard = board;
// for (int move : validMoves) {
// validMovesBoard[move] = 'X';
// }
// // Print the board
// cout << "Valid moves for piece at position " << i << ":" << endl;
// for (int row = 0; row < 8; ++row) {
// for (int col = 0; col < 8; ++col) {
// cout << validMovesBoard[row * 8 + col];
// }
// cout << endl;
// }
// cout << endl;
// Clean up
delete piece;
}
cout << endl;
}
}
/* Return a board evaluation (double) from a BoardState struct */
double evaluateBoard(const BoardState& boardState) {
// Call game_over with the BoardState directly
int game_result = game_over(boardState);
// Check if the game is over (checkmate or stalemate)
if (game_result == 1) { // white checkmate
return 50000;
}
else if(game_result == 2){ // draw (stalemate, fifty-move rule, etc.)
return 0;
}
else if (game_result == 3) { // black checkmate
return -50000;
}
map<char, double> pieceValues = {
{ '.', 0.0 },
{ 'Q', 900 },
{ 'q', -900},
{ 'R', 500 },
{ 'r', -500},
{ 'B', 330 },
{ 'b', -330},
{ 'N', 320 },
{ 'n', -320},
{ 'K', 20000},
{ 'k', -20000},
{ 'P', 100 },
{ 'p', -100},
{ 'E', 100 },
{ 'e', -100},
{ 'S', 20000},
{ 's', -20000},
{ 'W', 20000},
{ 'w', -20000},
{ 'U', 20000},
{ 'u', -20000}
};
const double pawnTable[8][8] = {
{0.0, 0.0, 0.0, 0.0, 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, 0.0, 0.0, 0.0, 0.0}
};
const double knightTable[8][8] = {
{-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}
};
const double bishopTable[8][8] = {
{-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}
};
const double rookTable[8][8] = {
{ 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},
};
const double queenTable[8][8] = {
{-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}
};
const double kingMidGameTable[8][8] = {
{-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, 0, 0, 0, 0, 20, 20},
{ 20, 30, 10, 0, 0, 10, 30, 20}
};
const double kingEndGameTable[8][8] = {
{-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}
};
double eval = 0.0;
for (size_t row = 0; row < 8; row++) {
for (size_t col = 0; col < 8; col++) {
//First add up the value of pieces
char piece = boardState.board[row * 8 + col];
//Now weight the pieces based on the squares theyre on
switch(piece){
case 'P':
case 'E':
eval += pieceValues[piece] + pawnTable[row][col];
break;
case 'p':
case 'e':
eval += pieceValues[piece] - pawnTable[7 - row][col];
break;