-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
986 lines (832 loc) · 37.7 KB
/
main.cpp
File metadata and controls
986 lines (832 loc) · 37.7 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
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
#include <iostream>
#include <vector>
#include <ctime>
// stores the indexes of a row and a col of a piece
struct Square {
int row;
int col;
};
// stores the square from which a piece is moving and a square to which it is moving
struct Move {
Square fromSquare;
Square toSquare;
};
// information about pieces needed for en passant and castle
struct PieceInformation {
bool kingMoved = false;
bool rookMovedA = false;
bool rookMovedH = false;
bool pawnMoved[8] = {false}; // indexes are cols e.g. 0 is a col
int enPassantCol = -1; // index of a col where a pawn just made a double move
};
const char *PIECES[] = {
"k","q","r","b","n","p",
".",
"P","N","B","R","Q","K"
};
// piece value goes from -6 to 6 and this converts it to an index
char const* pieceChar(int pieceValue) {
return PIECES[pieceValue + 6];
}
// for each square it converts the pieces value into a char and prints everything
// into a board format
void printBoard (int board [8][8]) {
for (int i = 0; i < 8; i++) {
std::cout << 8 - i << " ";
for (int j = 0; j < 8; j++) {
std::cout << pieceChar(board[i][j]) << " ";
}
std::cout << '\n';
}
std::cout << " a b c d e f g h" << '\n' << '\n';
}
// gets the player move and stores it inside a Move variable and returns that value
Move getPlayerMove (int board[8][8]) {
Move playerMove;
char temp;
// 1. gets the column of the square from which the player wants to move (the col must be on the board)
// 2. gets the row of the square from which the player wants to move (must be inside the board)
// 3. repeats the function until the player has chosen a valid square (isnt moving an empty space or a black piece)
do {
do {
std::cout << "Col from which you are moving: ";
std::cin >> temp;
if (std::cin.fail()) {std::cin.clear(); std::cin.ignore(10000, '\n'); temp = 'i';};
playerMove.fromSquare.col = int(temp) - int('a');
} while (playerMove.fromSquare.col > 7 || playerMove.fromSquare.col < 0);
do {
std::cout << "Row from which you are moving: ";
std::cin >> playerMove.fromSquare.row;
if (std::cin.fail()) {std::cin.clear(); std::cin.ignore(10000, '\n');};
playerMove.fromSquare.row = 8 - playerMove.fromSquare.row;
} while (playerMove.fromSquare.row > 7 || playerMove.fromSquare.row < 0);
} while (board[playerMove.fromSquare.row][playerMove.fromSquare.col] <= 0);
// does the same thing as the previous cycle but instead it repeats itself until the player
// chooses a square that isnt occupied by a white piece (player can only play white)
do {
do {
std::cout << "Col to which you are moving: ";
std::cin >> temp;
if (std::cin.fail()) {std::cin.clear(); std::cin.ignore(10000, '\n'); temp = 'i';};
playerMove.toSquare.col = int(temp) - int('a');
} while (playerMove.toSquare.col > 7 || playerMove.toSquare.col < 0);
do {
std::cout << "Row to which you are moving: ";
std::cin >> playerMove.toSquare.row;
if (std::cin.fail()) {std::cin.clear(); std::cin.ignore(10000, '\n');};
playerMove.toSquare.row = 8 - playerMove.toSquare.row;
} while (playerMove.toSquare.row > 7 || playerMove.toSquare.row < 0);
} while (board[playerMove.toSquare.row][playerMove.toSquare.col] > 0);
std::cout << '\n';
// returns the move
return playerMove;
}
// displays a move by changing the board
void makeMove (int board[8][8], Move move, PieceInformation &WhitePieceInfo, PieceInformation &BlackPieceInfo) {
WhitePieceInfo.enPassantCol = -1;
BlackPieceInfo.enPassantCol = -1;
int piece = board[move.fromSquare.row][move.fromSquare.col];
char temp;
int promotePiece = -1;
int capturedPiece = board[move.toSquare.row][move.toSquare.col];
// square on the board to which someone is moving will be changed to the piece that is on the square
// from which someone is moving
board[move.toSquare.row][move.toSquare.col] = board[move.fromSquare.row][move.fromSquare.col];
// square from which someone is moving is reset to 0 because it is now empty
board[move.fromSquare.row][move.fromSquare.col] = 0;
// if king has moved then sets the kingMoved to true to prevent castling later on
if (piece == 6) {WhitePieceInfo.kingMoved = true;};
if (piece == -6) {BlackPieceInfo.kingMoved = true;};
// if the rook has moved change the rookMoved to true
if (piece == 4 && move.fromSquare.row == 7 && move.fromSquare.col == 7) {WhitePieceInfo.rookMovedH = true;};
if (piece == 4 && move.fromSquare.row == 7 && move.fromSquare.col == 0) {WhitePieceInfo.rookMovedA = true;};
if (piece == -4 && move.fromSquare.row == 0 && move.fromSquare.col == 7) {BlackPieceInfo.rookMovedH = true;};
if (piece == -4 && move.fromSquare.row == 0 && move.fromSquare.col == 0) {BlackPieceInfo.rookMovedA = true;};
// if someone captured the rook set the rookMoved to true to prevent castling
if (move.toSquare.row == 7 && move.toSquare.col == 7) {WhitePieceInfo.rookMovedH = true;};
if (move.toSquare.row == 7 && move.toSquare.col == 0) {WhitePieceInfo.rookMovedA = true;};
if (move.toSquare.row == 0 && move.toSquare.col == 7) {BlackPieceInfo.rookMovedH = true;};
if (move.toSquare.row == 0 && move.toSquare.col == 0) {BlackPieceInfo.rookMovedA = true;};
// moving the rook when white is castling long and short
if (
move.fromSquare.row == 7 && move.fromSquare.col == 4 &&
move.toSquare.row == 7 && move.toSquare.col == 2 && piece == 6
) {board[7][0] = 0; board[7][3] = 4; WhitePieceInfo.rookMovedA = true;};
if (
move.fromSquare.row == 7 && move.fromSquare.col == 4 &&
move.toSquare.row == 7 && move.toSquare.col == 6 && piece == 6
) {board[7][7] = 0; board[7][5] = 4; WhitePieceInfo.rookMovedH = true;};
// moving the rook when black is castling long and short
if (
move.fromSquare.row == 0 && move.fromSquare.col == 4 &&
move.toSquare.row == 0 && move.toSquare.col == 2 && piece == -6
) {board[0][0] = 0; board[0][3] = -4; BlackPieceInfo.rookMovedA = true;};
if (
move.fromSquare.row == 0 && move.fromSquare.col == 4 &&
move.toSquare.row == 0 && move.toSquare.col == 6 && piece == -6
) {board[0][7] = 0; board[0][5] = -4; BlackPieceInfo.rookMovedH = true;};
// sets the captured pawn from en passant to empty space
if (piece == 1 && move.fromSquare.row == 3 &&
abs(move.fromSquare.col - move.toSquare.col) == 1 &&
capturedPiece == 0) {
board[3][move.toSquare.col] = 0;
}
if (piece == -1 && move.fromSquare.row == 4 &&
abs(move.fromSquare.col - move.toSquare.col) == 1 &&
capturedPiece == 0) {
board[4][move.toSquare.col] = 0;
}
// setting the en passant col if a player did a double move
if (piece == 1 && move.fromSquare.row == 6 && move.toSquare.row == 4) {BlackPieceInfo.enPassantCol = move.toSquare.col;}
if (piece == -1 && move.fromSquare.row == 1 && move.toSquare.row == 3) {WhitePieceInfo.enPassantCol = move.toSquare.col;}
// promotions
if (piece == 1 && move.toSquare.row == 0) {
board[move.toSquare.row][move.toSquare.col] = 5;
}
if (piece == -1 && move.toSquare.row == 7) {
board[move.toSquare.row][move.toSquare.col] = -5;
}
}
// from the square does a reverse move for every piece type to see if there is a piece that can check that square
bool isSquareInCheck (int board[8][8], const int SIDE, Square square) {
int row;
int col;
// tests if a knight is checking that square
int knightOffsets[8][2] = {
{-2,-1},{-2,1},
{-1,-2},{-1,2},
{ 1,-2},{ 1,2},
{ 2,-1},{ 2,1}
};
for (int i = 0; i < 8; i++) {
row = square.row + knightOffsets[i][0];
col = square.col + knightOffsets[i][1];
if (row < 0 || row > 7 || col < 0 || col > 7) {continue;};
if (SIDE == 1 && board[row][col] == -2) {return true;};
if (SIDE == 0 && board[row][col] == 2) {return true;};
}
// tests if a bishop or a queen diagonal is checking that square
int diagonalOffsets[4][2] = {
{-1,-1},{-1, 1},
{ 1,-1},{ 1, 1}
};
for (int i = 0; i < 4; i++) {
row = square.row;
col = square.col;
while (true) {
row += diagonalOffsets[i][0];
col += diagonalOffsets[i][1];
if (row < 0 || row > 7 || col < 0 || col > 7) {break;};
if (board[row][col] != 0) {
if (SIDE == 1 && (board[row][col] == -3 || board[row][col] == -5)) {return true;};
if (SIDE == 0 && (board[row][col] == 3 || board[row][col] == 5)) {return true;};
break;
}
}
}
// tests if a rook or a queen horizontal or vertical move is checking that square
int horVerOffsets[4][2] = {
{-1, 0},{ 1, 0},
{ 0, 1},{ 0,-1}
};
for (int i = 0; i < 4; i++) {
row = square.row;
col = square.col;
while (true) {
row += horVerOffsets[i][0];
col += horVerOffsets[i][1];
if (row < 0 || row > 7 || col < 0 || col > 7) {break;};
if (board[row][col] != 0) {
if (SIDE == 1 && (board[row][col] == -4 || board[row][col] == -5)) {return true;};
if (SIDE == 0 && (board[row][col] == 4 || board[row][col] == 5)) {return true;};
break;
}
}
}
// tests if a king is checking that square
int kingOffsets[8][2] = {
{-1,-1},{-1, 0},{-1, 1},
{ 0,-1},{ 0, 1},
{ 1,-1},{ 1, 0},{ 1, 1}
};
for (int i = 0; i < 8; i++) {
row = square.row + kingOffsets[i][0];
col = square.col + kingOffsets[i][1];
if (row < 0 || row > 7 || col < 0 || col > 7) {continue;};
if (SIDE == 1 && board[row][col] == -6) {return true;};
if (SIDE == 0 && board[row][col] == 6) {return true;};
}
// last case, checking if a pawn is checking that square
row = square.row;
col = square.col;
if (SIDE == 1) {
if (row > 0) {
if (col > 0 && board[row - 1][col - 1] == -1) {return true;};
if (col < 7 && board[row - 1][col + 1] == -1) {return true;};
}
}
if (SIDE == 0) {
if (row < 7) {
if (col > 0 && board[row + 1][col - 1] == 1) {return true;};
if (col < 7 && board[row + 1][col + 1] == 1) {return true;};
}
}
return false;
}
// filters every square to find the friendly king and returns the square of the king
Square findKing (int board[8][8], const int SIDE) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (SIDE == 1 && board[i][j] == 6) {return {i, j};}
if (SIDE == 0 && board[i][j] == -6) {return {i, j};}
}
}
return {-1, -1}; // if there is not a king which should not happen
}
// checks if the king is in check after the move
bool moveLeavesKingInCheck (int board[8][8], const int SIDE, Move move, PieceInformation WhitePieceInfo, PieceInformation BlackPieceInfo) {
int tempBoard[8][8];
// rewriting the board into a temp board
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
tempBoard[i][j] = board[i][j];
}
}
makeMove(tempBoard, move, WhitePieceInfo, BlackPieceInfo);
Square kingSquare = findKing(tempBoard, SIDE);
if (isSquareInCheck(tempBoard, SIDE, kingSquare)) {return true;};
return false;
}
// gets every legal move for a knight and retturns it inside of an array
std::vector <Move> getLegalMovesKnight (int board[8][8],const int SIDE, Square fromSquare) {
// every possible square to which can a knight move ideally represented in offsets
// we add these values to the row and col of the piece and that is a possible move for the knight
int knightOffsets[8][2] = {
{-2,-1},{-2,1},
{-1,-2},{-1,2},
{ 1,-2},{ 1,2},
{ 2,-1},{ 2,1}
};
// vector in which we store the legal moves
std::vector <Move> legalMoves;
Square toSquare;
// repeats this code for each offset
for (int i = 0; i < 8; i++) {
// makes a toSquare based on the legal offsets
toSquare.row = fromSquare.row + knightOffsets[i][0];
toSquare.col = fromSquare.col + knightOffsets[i][1];
// checks if the move is outside of the board
if (
toSquare.row < 0 || toSquare.row > 7 ||
toSquare.col < 0 || toSquare.col > 7
) {continue;};
// for each side checks if the square is occupied by the same colored piece
if (SIDE == 1 && board[toSquare.row][toSquare.col] > 0) {continue;};
if (SIDE == 0 && board[toSquare.row][toSquare.col] < 0) {continue;};
legalMoves.push_back({fromSquare, toSquare});
}
return legalMoves;
}
// goes in one direction (row, col, diagonal) and adds legal moves if the piece can move to that square and ends if it cant move
// forward in that direction
std::vector <Move> getLegalMovesInDirection (int board[8][8], const int SIDE, Square fromSquare, int rowOffset, int colOffset) {
Square toSquare = fromSquare;
std::vector <Move> legalMoves;
// while the piece can move
while (true) {
// going in a direction
toSquare.row += rowOffset;
toSquare.col += colOffset;
// outside of the board
if (
toSquare.row < 0 || toSquare.row > 7 ||
toSquare.col < 0 || toSquare.col > 7
) {break;};
// friendly piece
if (SIDE == 1 && board[toSquare.row][toSquare.col] > 0) {break;};
if (SIDE == 0 && board[toSquare.row][toSquare.col] < 0) {break;};
// empty space
if (board[toSquare.row][toSquare.col] == 0) {legalMoves.push_back({fromSquare, toSquare});};
// enemy piece
if (board[toSquare.row][toSquare.col] != 0) {legalMoves.push_back({fromSquare, toSquare}); break;};
}
return legalMoves;
}
// calls the getLegalMovesInDirection function to get legal moves for every direction a rook can move in
std::vector <Move> getLegalMovesRook (int board[8][8], const int SIDE, Square fromSquare) {
std::vector <Move> legalMoves;
std::vector <Move> moves;
// goes in every 4 directions a rook can move in
moves = getLegalMovesInDirection(board, SIDE, fromSquare, -1, 0);
legalMoves.insert(legalMoves.end(), moves.begin(), moves.end());
moves = getLegalMovesInDirection(board, SIDE, fromSquare, 1, 0);
legalMoves.insert(legalMoves.end(), moves.begin(), moves.end());
moves = getLegalMovesInDirection(board, SIDE, fromSquare, 0, 1);
legalMoves.insert(legalMoves.end(), moves.begin(), moves.end());
moves = getLegalMovesInDirection(board, SIDE, fromSquare, 0, -1);
legalMoves.insert(legalMoves.end(), moves.begin(), moves.end());
return legalMoves;
}
// calls the getLegalMovesInDirection function to get legal moves for every direction a bishop can move in
std::vector <Move> getLegalMovesBishop (int board[8][8], const int SIDE, Square fromSquare) {
std::vector <Move> legalMoves;
std::vector <Move> moves;
// goes in every 4 directions a bishop can move in
moves = getLegalMovesInDirection(board, SIDE, fromSquare, -1, -1);
legalMoves.insert(legalMoves.end(), moves.begin(), moves.end());
moves = getLegalMovesInDirection(board, SIDE, fromSquare, -1, 1);
legalMoves.insert(legalMoves.end(), moves.begin(), moves.end());
moves = getLegalMovesInDirection(board, SIDE, fromSquare, 1, -1);
legalMoves.insert(legalMoves.end(), moves.begin(), moves.end());
moves = getLegalMovesInDirection(board, SIDE, fromSquare, 1, 1);
legalMoves.insert(legalMoves.end(), moves.begin(), moves.end());
return legalMoves;
}
// calls the rook and bishop function because the queen moves as the rook and bishop combined
std::vector <Move> getLegalMovesQueen (int board[8][8], const int SIDE, Square fromSquare) {
std::vector <Move> legalMoves;
std::vector <Move> moves;
// diagonal squares
moves = getLegalMovesBishop(board, SIDE, fromSquare);
legalMoves.insert(legalMoves.end(), moves.begin(), moves.end());
// horizontal and vertical squares
moves = getLegalMovesRook(board, SIDE, fromSquare);
legalMoves.insert(legalMoves.end(), moves.begin(), moves.end());
return legalMoves;
}
// first half does the same thing as the knight and the second one checks if the conditions for castling are true
// and if yes then adds them to the legal moves
std::vector <Move> getLegalMovesKing (int board[8][8], const int SIDE, Square fromSquare, PieceInformation PieceInfo) {
int kingOffsets[8][2] = {
{-1,-1},{-1, 0},{-1, 1},
{ 0,-1},{ 0, 1},
{ 1,-1},{ 1, 0},{ 1, 1}
};
std::vector <Move> legalMoves;
Square toSquare;
// normal moves
for (int i = 0; i < 8; i++) {
toSquare.row = fromSquare.row + kingOffsets[i][0];
toSquare.col = fromSquare.col + kingOffsets[i][1];
// checks if the move is outside of the board
if (
toSquare.row < 0 || toSquare.row > 7 ||
toSquare.col < 0 || toSquare.col > 7
) {continue;};
// for each side checks if the square is occupied by the same colored piece
if (SIDE == 1 && board[toSquare.row][toSquare.col] > 0) {continue;};
if (SIDE == 0 && board[toSquare.row][toSquare.col] < 0) {continue;};
legalMoves.push_back({fromSquare, toSquare});
}
// white side long castle and short castle
if (
SIDE == 1 && PieceInfo.kingMoved == false && PieceInfo.rookMovedA == false &&
board[7][1] == 0 && isSquareInCheck(board, SIDE, {7,1}) == false &&
board[7][2] == 0 && isSquareInCheck(board, SIDE, {7,2}) == false &&
board[7][3] == 0 && isSquareInCheck(board, SIDE, {7,3}) == false &&
isSquareInCheck(board, SIDE, {7, 4}) == false
) {legalMoves.push_back({fromSquare, {7,2}});};
if (
SIDE == 1 && PieceInfo.kingMoved == false && PieceInfo.rookMovedH == false &&
board[7][5] == 0 && isSquareInCheck(board, SIDE, {7,5}) == false &&
board[7][6] == 0 && isSquareInCheck(board, SIDE, {7,6}) == false &&
isSquareInCheck(board, SIDE, {7, 4}) == false
) {legalMoves.push_back({fromSquare, {7,6}});};
// black side long castle and short castle
if (
SIDE == 0 && PieceInfo.kingMoved == false && PieceInfo.rookMovedA == false &&
board[0][1] == 0 && isSquareInCheck(board, SIDE, {0,1}) == false &&
board[0][2] == 0 && isSquareInCheck(board, SIDE, {0,2}) == false &&
board[0][3] == 0 && isSquareInCheck(board, SIDE, {0,3}) == false &&
isSquareInCheck(board, SIDE, {0, 4}) == false
) {legalMoves.push_back({fromSquare, {0,2}});};
if (
SIDE == 0 && PieceInfo.kingMoved == false && PieceInfo.rookMovedH == false &&
board[0][5] == 0 && isSquareInCheck(board, SIDE, {0,5}) == false &&
board[0][6] == 0 && isSquareInCheck(board, SIDE, {0,6}) == false &&
isSquareInCheck(board, SIDE, {0, 4}) == false
) {legalMoves.push_back({fromSquare, {0,6}});};
return legalMoves;
}
// checks every ideal possible move a pawn can make and returns the ones that the pawn can actually make based on the rules
std::vector <Move> getLegalMovesPawn (int board[8][8], const int SIDE, Square fromSquare, PieceInformation PieceInfo) {
int whitePawnOffsets[4][2] = {
{-1,-1},{-1,1}, // captures
{-1,0},{-2,0} // moving forward
};
int blackPawnOffsets[4][2] = {
{ 1,-1},{ 1,1}, // captures
{ 1,0},{ 2,0} // moving forward
};
Square toSquare;
std::vector <Move> legalMoves;
for (int i = 0; i < 4; i++) {
if (SIDE == 1) {
toSquare.row = fromSquare.row + whitePawnOffsets[i][0];
toSquare.col = fromSquare.col + whitePawnOffsets[i][1];
// outside the board
if (toSquare.col < 0 || toSquare.col > 7) {continue;};
if (i == 0) { // capture left
if (board[toSquare.row][toSquare.col] < 0 || (PieceInfo.enPassantCol == toSquare.col && fromSquare.row == 3)) {
legalMoves.push_back({fromSquare, toSquare});
}
}
if (i == 1) { // capture right
if (board[toSquare.row][toSquare.col] < 0 || (PieceInfo.enPassantCol == toSquare.col && fromSquare.row == 3)) {
legalMoves.push_back({fromSquare, toSquare});
}
}
if (i == 2) { // moving one forward
if (board[toSquare.row][toSquare.col] == 0) {
legalMoves.push_back({fromSquare, toSquare});
}
}
if (i == 3) { // moving two forward
if (board[toSquare.row][toSquare.col] == 0 && board[toSquare.row + 1][toSquare.col] == 0 && fromSquare.row == 6) {
legalMoves.push_back({fromSquare, toSquare});
}
}
}
if (SIDE == 0) {
toSquare.row = fromSquare.row + blackPawnOffsets[i][0];
toSquare.col = fromSquare.col + blackPawnOffsets[i][1];
// outside the board
if (toSquare.col < 0 || toSquare.col > 7) {continue;};
if (i == 0) { // capture left
if (board[toSquare.row][toSquare.col] > 0 || (PieceInfo.enPassantCol == toSquare.col && fromSquare.row == 4)) {
legalMoves.push_back({fromSquare, toSquare});
}
}
if (i == 1) { // capture right
if (board[toSquare.row][toSquare.col] > 0 || (PieceInfo.enPassantCol == toSquare.col && fromSquare.row == 4)) {
legalMoves.push_back({fromSquare, toSquare});
}
}
if (i == 2) { // moving one forward
if (board[toSquare.row][toSquare.col] == 0) {
legalMoves.push_back({fromSquare, toSquare});
}
}
if (i == 3) { // moving two forward
if (board[toSquare.row][toSquare.col] == 0 && board[toSquare.row - 1][toSquare.col] == 0 && fromSquare.row == 1) {
legalMoves.push_back({fromSquare, toSquare});
}
}
}
}
return legalMoves;
}
// based on the square of a piece, board and the side this function chooses the right function
// to get all of the legal moves for that piece
std::vector <Move> getLegalMoves (
Square fromSquare, int board[8][8], const int SIDE,
const PieceInformation &WhitePieceInfo, const PieceInformation &BlackPieceInfo
) {
const int PIECE = abs(board[fromSquare.row][fromSquare.col]);
std::vector <Move> legalMoves;
std::vector <Move> tempLegalMoves;
// choosing the right function to get the legal moves
switch (PIECE) {
case 1:
if (SIDE == 1) {
tempLegalMoves = getLegalMovesPawn(board, SIDE, fromSquare, BlackPieceInfo);
}
else if (SIDE == 0) {
tempLegalMoves = getLegalMovesPawn(board, SIDE, fromSquare, WhitePieceInfo);
}
break;
case 2:
tempLegalMoves = getLegalMovesKnight(board, SIDE, fromSquare);
break;
case 3:
tempLegalMoves = getLegalMovesBishop(board, SIDE, fromSquare);
break;
case 4:
tempLegalMoves = getLegalMovesRook(board, SIDE, fromSquare);
break;
case 5:
tempLegalMoves = getLegalMovesQueen(board, SIDE, fromSquare);
break;
case 6:
if (SIDE == 1) {
tempLegalMoves = getLegalMovesKing(board, SIDE, fromSquare, WhitePieceInfo);
}
else if (SIDE == 0) {
tempLegalMoves = getLegalMovesKing(board, SIDE, fromSquare, BlackPieceInfo);
}
break;
}
// only returns the move if after the move the king is not in check
for (int i = 0; i < tempLegalMoves.size(); i++) {
if (moveLeavesKingInCheck(board, SIDE, tempLegalMoves[i], WhitePieceInfo, BlackPieceInfo) == false) {
legalMoves.push_back(tempLegalMoves[i]);
}
}
return legalMoves;
}
// goes for every piece of a side and gets all legal moves for that piece
std::vector <Move> getAllLegalMoves (
int board[8][8], const int SIDE,
const PieceInformation &WhitePieceInfo, const PieceInformation &BlackPieceInfo
) {
std::vector <Move> allLegalMoves;
// gets legal moves for a piece on the board at the square i,j
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (SIDE == 1 && board[i][j] > 0) {
std::vector <Move> moves = getLegalMoves({i, j}, board, SIDE, WhitePieceInfo, BlackPieceInfo);
allLegalMoves.insert(allLegalMoves.end(), moves.begin(), moves.end());
}
if (SIDE == 0 && board[i][j] < 0) {
std::vector <Move> moves = getLegalMoves({i, j}, board, SIDE, WhitePieceInfo, BlackPieceInfo);
allLegalMoves.insert(allLegalMoves.end(), moves.begin(), moves.end());
}
}
}
return allLegalMoves;
}
// compares every legal move to the player move and returns true if it sees a match
bool isPlayerMoveLegal (Move playerMove, std::vector <Move> legalMoves) {
for (int i = 0; i < legalMoves.size(); i++) {
if (
playerMove.fromSquare.row == legalMoves[i].fromSquare.row &&
playerMove.fromSquare.col == legalMoves[i].fromSquare.col &&
playerMove.toSquare.row == legalMoves[i].toSquare.row &&
playerMove.toSquare.col == legalMoves[i].toSquare.col
) {return true;};
}
return false;
}
// returns a score that tells who is winning
int evaluateBoard (int board[8][8]) {
// empty space, pawn, knight, bishop, rook, queen, king
const int pieceValues[] = { 0, 100, 320, 330, 500, 900, 20000 };
int score = 0;
// boards show where a piece is stronger
static const int pawnTable[8][8] = {
{ 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}
};
static const int 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}
};
static const int 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}
};
static const int rookTable[8][8] = {
{ 0, 0, 0, 5, 5, 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}
};
static const int 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}
};
static const int kingTable[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}
};
// for every piece adds the real value of that piece to the score
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
int piece = board[i][j];
if (piece == 0) {continue;};
if (piece > 0) {
switch (piece) {
case 1:
score += (pieceValues[piece] + pawnTable[i][j]);
break;
case 2:
score += (pieceValues[piece] + knightTable[i][j]);
break;
case 3:
score += (pieceValues[piece] + bishopTable[i][j]);
break;
case 4:
score += (pieceValues[piece] + rookTable[i][j]);
break;
case 5:
score += (pieceValues[piece] + queenTable[i][j]);
break;
case 6:
score += (pieceValues[piece] + kingTable[i][j]);
break;
}
}
if (piece < 0) {
switch (abs(piece)) {
case 1:
score -= (pieceValues[-piece] + pawnTable[7 - i][j]);
break;
case 2:
score -= (pieceValues[-piece] + knightTable[7 - i][j]);
break;
case 3:
score -= (pieceValues[-piece] + bishopTable[7 - i][j]);
break;
case 4:
score -= (pieceValues[-piece] + rookTable[7 - i][j]);
break;
case 5:
score -= (pieceValues[-piece] + queenTable[7 - i][j]);
break;
case 6:
score -= (pieceValues[-piece] + kingTable[7 - i][j]);
break;
}
}
}
}
return score;
}
int minimax (
int board[8][8], int depth, int alpha, int beta, bool isMaximizing,
PieceInformation WhitePieceInfo, PieceInformation BlackPieceInfo
) {
// end of the search
if (depth == 0) {return evaluateBoard(board);};
int side = isMaximizing ? 1 : 0;
std::vector <Move> moves = getAllLegalMoves(board, side, WhitePieceInfo, BlackPieceInfo);
if (moves.empty()) {
// checkmate
if (isSquareInCheck(board, side, findKing(board, side))) {return isMaximizing ? -30000 : 30000;};
// stalemate
return 0;
}
// white wants to play the best move
if (isMaximizing) {
int bestScore = -1000000;
for (int i = 0; i < moves.size(); i++) {
int tempBoard[8][8];
// copying information
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 8; k++) {
tempBoard[j][k] = board[j][k];
}
}
PieceInformation tempWhiteInfo = WhitePieceInfo;
PieceInformation tempBlackInfo = BlackPieceInfo;
// simulating the move and looking for the best one
makeMove(tempBoard, moves[i], tempWhiteInfo, tempBlackInfo);
int score = minimax(tempBoard, depth - 1, alpha, beta, false, tempWhiteInfo, tempBlackInfo);
bestScore = std::max(bestScore, score);
alpha = std::max(alpha, bestScore);
if (beta <= alpha) {break;};
}
return bestScore;
}
// black wants to play the best move
else {
int bestScore = 1000000;
for (int i = 0; i < moves.size(); i++) {
int tempBoard[8][8];
// copying information
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 8; k++) {
tempBoard[j][k] = board[j][k];
}
}
PieceInformation tempWhiteInfo = WhitePieceInfo;
PieceInformation tempBlackInfo = BlackPieceInfo;
// simulating the move and looking for the best one
makeMove(tempBoard, moves[i], tempWhiteInfo, tempBlackInfo);
int score = minimax(tempBoard, depth - 1, alpha, beta, true, tempWhiteInfo, tempBlackInfo);
bestScore = std::min(bestScore, score);
alpha = std::min(beta, bestScore);
if (beta <= alpha) {break;};
}
return bestScore;
}
}
Move getBestBotMove(int board[8][8], int depth, PieceInformation WhitePieceInfo, PieceInformation BlackPieceInfo) {
// bot can only play black
std::vector <Move> moves = getAllLegalMoves(board, 0, WhitePieceInfo, BlackPieceInfo);
Move bestMove = moves[0];
int bestScore = 1000000;
for (int i = 0; i < moves.size(); i++) {
int tempBoard[8][8];
// copying information
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 8; k++) {
tempBoard[j][k] = board[j][k];
}
}
PieceInformation tempWhiteInfo = WhitePieceInfo;
PieceInformation tempBlackInfo = BlackPieceInfo;
makeMove(tempBoard, moves[i], tempWhiteInfo, tempBlackInfo);
int score = minimax(tempBoard, depth - 1, -1000000, 1000000, true, tempWhiteInfo, tempBlackInfo);
if (score < bestScore) {
bestScore = score;
bestMove = moves[i];
}
}
return bestMove;
}
int main () {
// positive numbers = white pieces
// negative numbers = black pieces
// 0 = empty space
int board [8][8] = {
{-4,-2,-3,-5,-6,-3,-2,-4},
{-1,-1,-1,-1,-1,-1,-1,-1},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 1, 1, 1, 1, 1, 1, 1, 1},
{ 4, 2, 3, 5, 6, 3, 2, 4},
};
// 1 is white pieces and 0 is black pieces
const int PLAYERSIDE = 1;
const int BOTSIDE = 0;
PieceInformation WhitePieceInfo;
PieceInformation BlackPieceInfo;
Move playerMove;
Move botMove;
std::vector <Move> legalMoves;
bool legalMove;
srand(time(NULL));
printBoard(board);
while (true) {
// gets a players legal move
do {
playerMove = getPlayerMove(board);
legalMoves = getLegalMoves(playerMove.fromSquare, board, PLAYERSIDE, WhitePieceInfo, BlackPieceInfo);
legalMove = isPlayerMoveLegal(playerMove, legalMoves);
if (legalMove == true) {
makeMove(board, playerMove, WhitePieceInfo, BlackPieceInfo);
printBoard(board);
std::cout << "score: " << evaluateBoard(board) << '\n' << '\n';
}
else if (legalMove == false) {
std::cout << "illegal move\n\n";
}
} while (legalMove == false);
// checks for game end
if (
isSquareInCheck(board, BOTSIDE, findKing(board, BOTSIDE)) == true &&
getAllLegalMoves(board, BOTSIDE, WhitePieceInfo, BlackPieceInfo).size() == 0
) {std::cout << "Checkmate, player won."; break;};
if (
isSquareInCheck(board, BOTSIDE, findKing(board, BOTSIDE)) == false &&
getAllLegalMoves(board, BOTSIDE, WhitePieceInfo, BlackPieceInfo).size() == 0
) {std::cout << "Tie, it is a stalemate"; break;};
// gets a random move from the bot
legalMoves = getAllLegalMoves(board, BOTSIDE, WhitePieceInfo, BlackPieceInfo);
botMove = getBestBotMove(board, 5, WhitePieceInfo, BlackPieceInfo);
makeMove(board, botMove, WhitePieceInfo, BlackPieceInfo);
std::cout << "Bot played from col: " << char(int('a') + botMove.fromSquare.col) << '\n';
std::cout << "Bot played from row: " << 8 - botMove.fromSquare.row << '\n';
std::cout << "Bot played to col: " << char(int('a') + botMove.toSquare.col)<< '\n';
std::cout << "Bot played to row: " << 8 - botMove.toSquare.row << '\n' << '\n';
printBoard(board);
std::cout << "score: " << evaluateBoard(board) << '\n' << '\n';
// checks for game end
if (
isSquareInCheck(board, PLAYERSIDE, findKing(board, PLAYERSIDE)) == true &&
getAllLegalMoves(board, PLAYERSIDE, WhitePieceInfo, BlackPieceInfo).size() == 0
) {std::cout << "Checkmate, bot won."; break;};
if (
isSquareInCheck(board, PLAYERSIDE, findKing(board, PLAYERSIDE)) == false &&
getAllLegalMoves(board, PLAYERSIDE, WhitePieceInfo, BlackPieceInfo).size() == 0
) {std::cout << "Tie, it is a stalemate"; break;};
}
return 0;
}