-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpce.cpp
1463 lines (1375 loc) · 45.7 KB
/
pce.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
/*
> PCE - Play eChess Engine
> Code by SinisterIcy
> Version ALPHA
> Licensed under the GNU General Public License v3.0
> Github: https://github.com/playeChess/PCE
*/
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include <cstdlib>
/**
* @brief PlayeChessEngine is the namespace for the PCE engine who is made for
* the eChess project (playechess.com)
*/
namespace PlayeChessEngine {
/**
* @brief Move is a class that represents a move (start square and end square)
*/
class Move {
private:
/**
* @brief The x coordinate of the start square
*/
int start_square_x;
/**
* @brief The y coordinate of the start square
*/
int start_square_y;
/**
* @brief The x coordinate of the end square
*/
int end_square_x;
/**
* @brief The y coordinate of the end square
*/
int end_square_y;
/**
* @brief If the move is a capture
*/
bool is_capture = false;
/**
* @brief If the move is valid
*/
bool is_valid = false;
public:
/**
* @brief Construct a new Move object
*
* @param start_square_x The x coordinate of the start square
* @param start_square_y The y coordinate of the start square
* @param end_square_x The x coordinate of the end square
* @param end_square_y The y coordinate of the end square
*/
Move(int start_square_x, int start_square_y, int end_square_x,
int end_square_y) {
this->start_square_x = start_square_x;
this->start_square_y = start_square_y;
this->end_square_x = end_square_x;
this->end_square_y = end_square_y;
};
/**
* @brief Shows the move in a readable format
*
* @return Formatted move (std::string)
*/
std::string show() {
std::string files = "abcdefgh";
return std::string(1, files[start_square_y]) + std::to_string(start_square_x + 1) + " -> " + std::string(1, files[end_square_y]) + std::to_string(end_square_x + 1);
}
/**
* @brief Get the start coords
*
* @return The start coords (std::vector<int>)
*/
std::vector<int> get_start_coords() {
return {this->start_square_x, this->start_square_y};
}
/**
* @brief Get the end coords
*
* @return The end coords (std::vector<int>)
*/
std::vector<int> get_end_coords() {
return {this->end_square_x, this->end_square_y};
}
/**
* @brief Set if the move is a capture
*
* @param is_capture If the move is a capture (bool)
*/
void set_capture(bool is_capture) {
this->is_capture = is_capture;
}
/**
* @brief Get if the move is a capture
*
* @return If the move is a capture
*/
bool get_capture() {
return this->is_capture;
}
/**
* @brief Set if the move is valid
*
* @param is_valid If the move is valid
*/
void set_valid(bool is_valid) {
this->is_valid = is_valid;
}
/**
* @brief Get if the move is valid
*
* @return If the move is valid
*/
bool get_valid() {
return this->is_valid;
}
/**
* @brief Checks if the move is in a vector of moves
*
* @param moves The vector of moves
* @return True if the move is in the vector, false if not (bool)
*/
bool am_in(std::vector<Move> moves) {
for (int i = 0; i < moves.size(); i++) {
if (moves[i].get_end_coords()[0] == this->get_end_coords()[0] && moves[i].get_end_coords()[1] == this->get_end_coords()[1])
return true;
}
return false;
}
};
/**
* @brief Namespace for things related to the board
*/
namespace board {
/**
* @brief Namespace for things related to the pieces
*/
namespace pieces {
/**
* @brief The piece type enum (p = pawn, r = rook, n = knight, b = bishop, q =
* queen, k = king)
*/
enum piece_type { p, r, n, b, q, k };
/**
* @brief The piece class (abstract)
*/
class Piece {
protected:
/**
* @brief The type of the piece
*/
piece_type type;
/**
* @brief The coordinates of the piece
*/
int coords[2] = {0, 0};
public:
/**
* @brief The color of the piece (true = white, false = black)
*/
bool is_white;
/**
* @brief If the piece has moved (used for castling)
*/
bool has_moved = false;
/**
* @brief Construct a new Piece object
*
* @param type The type of the piece (refer to the piece_type enum)
* @param is_white The color of the piece (true = white, false = black)
* @param x The x coordinate of the piece (0-7)
* @param y The y coordinate of the piece (0-7)
*/
Piece(piece_type type, bool is_white, int x, int y) {
this->type = type;
this->is_white = is_white;
this->coords[0] = x;
this->coords[1] = y;
}
/**
* @brief Destroy the Piece object
*/
virtual ~Piece() {}
/**
* @brief Validates the move of the piece (abstract)
*
* @return If the move is valid (bool)
*/
virtual bool validation_function(std::array<std::array<Piece *, 8>, 8> board, int x_final, int y_final) {
return false;
}
/**
* @brief Updates the coordinates of the piece
*
* @param x The new x coordinate of the piece (0-7)
* @param y The new y coordinate of the piece (0-7)
*/
void update_coords(int x, int y) {
this->coords[0] = x;
this->coords[1] = y;
}
/**
* @brief Get the type object
*
* @return piece_type
*/
piece_type get_type() { return this->type; }
/**
* @brief Validates the move of the piece (checks if the landing square is
* valid)
*
* @param board The board
* @param x_final The x coordinate of the landing square
* @param y_final The y coordinate of the landing square
* @return If the landing square is valid (bool)
*/
bool validate_validation(std::array<std::array<Piece *, 8>, 8> board, int x_final, int y_final) {
if (board[x_final][y_final] == nullptr)
return true;
if (board[x_final][y_final]->is_white == this->is_white)
return false;
return true;
}
/**
* @brief Checks the path from the piece to the landing square
*
* @param x_final The final x coordinate
* @param y_final The final y coordinate
* @param board The board
* @return Whether the path is clear (bool)
*/
bool check_path(int x_final, int y_final, std::array<std::array<Piece *, 8>, 8> board) {
int x_diff = x_final - this->coords[0];
int y_diff = y_final - this->coords[1];
if (x_diff == 0) {
if (y_diff > 0) {
for (int i = 1; i < y_diff; i++) {
if (board[this->coords[0]][this->coords[1] + i] != nullptr)
return false;
}
} else {
for (int i = 1; i < abs(y_diff); i++) {
if (board[this->coords[0]][this->coords[1] - i] != nullptr)
return false;
}
}
} else if (y_diff == 0) {
if (x_diff > 0) {
for (int i = 1; i < x_diff; i++) {
if (board[this->coords[0] + i][this->coords[1]] != nullptr)
return false;
}
} else {
for (int i = 1; i < abs(x_diff); i++) {
if (board[this->coords[0] - i][this->coords[1]] != nullptr)
return false;
}
}
} else if (abs(x_diff) == abs(y_diff)) {
if (x_diff > 0 && y_diff > 0) {
for (int i = 1; i < x_diff; i++) {
if (board[this->coords[0] + i][this->coords[1] + i] != nullptr)
return false;
}
} else if (x_diff < 0 && y_diff < 0) {
for (int i = 1; i < abs(x_diff); i++) {
if (board[this->coords[0] - i][this->coords[1] - i] != nullptr)
return false;
}
} else if (x_diff > 0 && y_diff < 0) {
for (int i = 1; i < x_diff; i++) {
if (board[this->coords[0] + i][this->coords[1] - i] != nullptr)
return false;
}
} else if (x_diff < 0 && y_diff > 0) {
for (int i = 1; i < abs(x_diff); i++) {
if (board[this->coords[0] - i][this->coords[1] + i] != nullptr)
return false;
}
}
}
return true;
}
/**
* @brief Shows the piece in a readable format
*
* @return The formatted piece (std::string)
*/
std::string show() {
if (this->is_white) {
std::string piece_names[6] = {"P", "R", "N", "B", "Q", "K"};
return piece_names[this->type];
}
std::string piece_names[6] = {"p", "r", "n", "b", "q", "k"};
return piece_names[this->type];
}
};
/**
* @brief The pawn piece (inherits from Piece)
*
*/
class Pawn : public Piece {
private:
/**
* @brief If the pawn has moved
*
*/
bool has_moved = false;
public:
/**
* @brief Construct a new Pawn object
*
* @param is_white Whether the pawn is white or black (true = white, false =
* black)
* @param x The x coordinate of the pawn
* @param y The y coordinate of the pawn
*/
Pawn(bool is_white, int x, int y) : Piece(p, is_white, x, y){}
/**
* @brief Validates the move of the pawn (combines the other validation
* functions)
*
* @param board The board
* @param x_final The x coordinate of the landing square
* @param y_final The y coordinate of the landing square
* @return If the move is valid (bool)
*/
bool validation_function(std::array<std::array<Piece *, 8>, 8> board, int x_final, int y_final) {
// TODO Add pawn takes
int x_diff = x_final - this->coords[0];
int y_diff = y_final - this->coords[1];
if (y_diff == 0 && x_diff != 0) {
if (this->is_white) {
if (x_diff == 1) {
if (board[x_final][y_final] == nullptr)
return true;
} else if (x_diff == 2 && !this->has_moved && this->coords[0] == 1) {
if (board[x_final][y_final] == nullptr && board[x_final][y_final - 1] == nullptr)
return true;
}
} else {
if (x_diff == -1) {
if (board[x_final][y_final] == nullptr)
return true;
} else if (x_diff == -2 && !has_moved && this->coords[0] == 6) {
if (board[x_final][y_final] == nullptr && board[x_final][y_final + 1] == nullptr)
return true;
}
}
} else if (abs(y_diff) == 1 && abs(x_diff) == 1) {
if (this->is_white) {
if (x_diff == 1) {
if (board[x_final][y_final] != nullptr && !board[x_final][y_final]->is_white)
return true;
}
} else {
if (x_diff == -1) {
if (board[x_final][y_final] != nullptr && board[x_final][y_final]->is_white)
return true;
}
}
}
return false;
}
};
/**
* @brief The rook piece (inherits from Piece)
*
*/
class Rook : public Piece {
private:
/**
* @brief If the rook has moved
*
*/
bool has_moved = false;
public:
/**
* @brief Construct a new Rook object
*
* @param is_white Whether the rook is white or black (true = white, false =
* black)
* @param x The x coordinate of the rook
* @param y The y coordinate of the rook
*/
Rook(bool is_white, int x, int y) : Piece(r, is_white, x, y) {}
/**
* @brief Validates the move of the rook (combines the other validation
* functions)
*
* @param board The board
* @param x_final The x coordinate of the landing square
* @param y_final The y coordinate of the landing square
* @return If the move is valid (bool)
*/
bool validation_function(std::array<std::array<Piece *, 8>, 8> board, int x_final, int y_final) {
int x_diff = x_final - this->coords[0];
int y_diff = y_final - this->coords[1];
if (x_diff * y_diff == 0 && x_diff != y_diff) {
if (validate_validation(board, x_final, y_final))
return this->check_path(x_final, y_final, board);
}
return false;
}
};
/**
* @brief The knight piece (inherits from Piece)
*
*/
class Knight : public Piece {
public:
/**
* @brief Construct a new Knight object
*
* @param is_white Whether the knight is white or black (true = white, false =
* black)
* @param x The x coordinate of the knight
* @param y The y coordinate of the knight
*/
Knight(bool is_white, int x, int y) : Piece(n, is_white, x, y) {}
/**
* @brief Validates the move of the knight (combines the other validation
* functions)
*
* @param board The board
* @param x_final The x coordinate of the landing square
* @param y_final The y coordinate of the landing square
* @return If the move is valid (bool)
*/
bool validation_function(std::array<std::array<Piece *, 8>, 8> board, int x_final, int y_final) {
int x_diff = x_final - this->coords[0];
int y_diff = y_final - this->coords[1];
if ((abs(x_diff) == 2 && abs(y_diff) == 1) || (abs(x_diff) == 1 && abs(y_diff) == 2))
return validate_validation(board, x_final, y_final);
return false;
}
};
/**
* @brief The bishop piece (inherits from Piece)
*
*/
class Bishop : public Piece {
public:
/**
* @brief Construct a new Bishop object
*
* @param is_white Whether the bishop is white or black (true = white, false =
* black)
* @param x The x coordinate of the bishop
* @param y The y coordinate of the bishop
*/
Bishop(bool is_white, int x, int y) : Piece(b, is_white, x, y) {}
/**
* @brief Validates the move of the bishop (combines the other validation
* functions)
*
* @param board The board
* @param x_final The x coordinate of the landing square
* @param y_final The y coordinate of the landing square
* @return If the move is valid (bool)
*/
bool validation_function(std::array<std::array<Piece *, 8>, 8> board, int x_final, int y_final) {
int x_diff = x_final - this->coords[0];
int y_diff = y_final - this->coords[1];
if (abs(x_diff) == abs(y_diff) && x_diff != 0) {
if (validate_validation(board, x_final, y_final))
return this->check_path(x_final, y_final, board);
}
return false;
}
};
/**
* @brief The queen piece (inherits from Piece)
*
*/
class Queen : public Piece {
public:
/**
* @brief Construct a new Queen object
*
* @param is_white Whether the queen is white or black (true = white, false =
* black)
* @param x The x coordinate of the queen
* @param y The y coordinate of the queen
*/
Queen(bool is_white, int x, int y) : Piece(q, is_white, x, y) {}
/**
* @brief Validates the move of the queen (combines the other validation
* functions)
*
* @param board The board
* @param x_final The x coordinate of the landing square
* @param y_final The y coordinate of the landing square
* @return If the move is valid (bool)
*/
bool validation_function(std::array<std::array<Piece *, 8>, 8> board, int x_final, int y_final) {
int x_diff = x_final - this->coords[0];
int y_diff = y_final - this->coords[1];
if ((abs(x_diff) == abs(y_diff) && x_diff != 0) || ((x_diff == 0 || y_diff == 0) && x_diff != y_diff)) {
if (validate_validation(board, x_final, y_final))
return this->check_path(x_final, y_final, board);
}
return false;
}
};
/**
* @brief The king piece (inherits from Piece)
*
*/
class King : public Piece {
private:
/**
* @brief Whether the king has moved or not
*
*/
bool has_moved = false;
public:
/**
* @brief Construct a new King object
*
* @param is_white Whether the king is white or black (true = white, false =
* black)
* @param x The x coordinate of the king
* @param y The y coordinate of the king
*/
King(bool is_white, int x, int y) : Piece(k, is_white, x, y) {}
/**
* @brief Validates the move of the king (combines the other validation
* functions)
*
* @param board The board
* @param x_final The x coordinate of the landing square
* @param y_final The y coordinate of the landing square
* @return If the move is valid (bool)
*/
bool validation_function(std::array<std::array<Piece *, 8>, 8> board, int x_final, int y_final) {
int x_diff = x_final - this->coords[0];
int y_diff = y_final - this->coords[1];
if (abs(x_diff) <= 1 && abs(y_diff) <= 1 && (x_diff != 0 || y_diff != 0)) {
// std::cout << "King validation " << this->coords[0] << " " << this->coords[1] << " - " << x_final << " " << y_final << std::endl;
return validate_validation(board, x_final, y_final);
}
return false;
}
};
} // namespace pieces
/**
* @brief The board class
*
*/
class Board {
private:
/**
* @brief The board
*
*/
std::array<std::array<pieces::Piece *, 8>, 8> board = {{
{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}
}};
/**
* @brief The moves played to get to this position
*
*/
std::vector<Move> moves;
/**
* @brief Whether it is white's turn or not
*
*/
bool white_turn = true;
public:
// TODO Add the funcitonnality for "KQkq - 0 1"
/**
* @brief Construct a new Board object
*
* @param fen The fen string
*/
Board(std::string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") {
size_t fen_end = fen.find(" ");
fen = fen.substr(0, fen_end);
this->white_turn = fen[fen_end + 1] == 'w';
this->load_fen(fen);
};
/**
* @brief Set the moves vector
*
* @param moves The new value of the moves vector
*/
void set_moves(std::vector<Move> moves) {
this->moves = moves;
}
/**
* @brief Set the white_turn value
*
* @param white_turn The new value of white_turn
*/
void set_white_turn(bool white_turn) {
this->white_turn = white_turn;
}
/**
* @brief Checks if the board is equal to another board
*
* @param other The other board
* @return Whether the boards are equal (bool)
*/
bool operator==(Board &other) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (this->board[i][j] == nullptr && other.board[i][j] == nullptr) {
continue;
} if (this->board[i][j] == nullptr || other.board[i][j] == nullptr) {
return false;
}
if(this->board[i][j]->get_type() == other.board[i][j]->get_type() && this->board[i][j]->is_white == other.board[i][j]->is_white)
continue;
return false;
}
}
if(this->can_castle(true, true) != other.can_castle(true, true) || this->can_castle(true, false) != other.can_castle(true, false) || this->can_castle(false, true) != other.can_castle(false, true) || this->can_castle(false, false) != other.can_castle(false, false))
return false;
if(this->get_en_passant(this->moves, this->white_turn) != other.get_en_passant(other.moves, other.white_turn))
return false;
return true;
}
/**
* @brief Checks if the board is not equal to another board
*
* @param other The other board
* @return Whether the boards are not equal (bool)
*/
bool operator!=(Board &other) {
return !(*this == other);
}
/**
* @brief Reverses the fen string (for loading the board)
*
* @param fen The fen string
* @return The reversed fen (std::string)
*/
std::string reverse_fen(std::string fen) {
std::vector<std::string> fen_split;
std::string rt = "";
std::string buffer = "";
for (auto c : fen) {
if (c == '/') {
fen_split.push_back(buffer + '/');
buffer = "";
} else {
buffer += c;
}
}
fen_split.push_back(buffer + '/');
for (int i = fen_split.size() - 1; i >= 0; i--) {
rt += fen_split[i];
}
return rt.substr(0, rt.size() - 1);
}
/**
* @brief Loads the board from a fen string
*
* @param fen The fen string
*/
void load_fen(std::string fen) {
fen = reverse_fen(fen);
int x = 0;
int y = 0;
for (auto c : fen) {
if (c == '/') {
y = 0;
x++;
} else if (c == '8') {
continue;
} else if (c >= '0' && c <= '9') {
y += c - '0';
} else {
bool is_white = c >= 'A' && c <= 'Z';
switch (c) {
case 'p':
case 'P':
this->board[x][y] = new pieces::Pawn(is_white, x, y);
break;
case 'r':
case 'R':
this->board[x][y] = new pieces::Rook(is_white, x, y);
break;
case 'n':
case 'N':
this->board[x][y] = new pieces::Knight(is_white, x, y);
break;
case 'b':
case 'B':
this->board[x][y] = new pieces::Bishop(is_white, x, y);
break;
case 'q':
case 'Q':
this->board[x][y] = new pieces::Queen(is_white, x, y);
break;
case 'k':
case 'K':
this->board[x][y] = new pieces::King(is_white, x, y);
break;
}
y++;
}
}
}
/**
* @brief Prints the board (for console only)
*
* @param moves The moves to highlight
* @param board The board to print
*/
void print_board(std::vector<std::vector<int>> moves = {}, std::array<std::array<pieces::Piece *, 8>, 8> board = {}) {
if (board == std::array<std::array<pieces::Piece *, 8>, 8>{})
board = this->board;
std::cout << " #-----------------#" << std::endl;
for (int i = 0; i < 8; i++) {
std::cout << 8 - i << " | ";
for (int j = 0; j < 8; j++) {
bool skip = false;
for (auto move : moves) {
if (move[0] == 7 - i && move[1] == j) {
std::cout << "* ";
skip = true;
break;
}
}
if (skip)
continue;
if (board[7 - i][j] != nullptr)
std::cout << board[7 - i][j]->show() << " ";
else
std::cout << " ";
}
std::cout << "|" << std::endl;
}
std::cout << " #-----------------#" << std::endl;
std::cout << " a b c d e f g h" << std::endl;
}
/**
* @brief Gets the board
*
* @return The board (std::array<std::array<pieces::Piece*, 8>, 8>)
*/
std::array<std::array<pieces::Piece *, 8>, 8> get_board() {
return this->board;
}
/**
* @brief Gets a piece from the board
*
* @param x The x coordinate
* @param y The y coordinate
* @return The piece (pieces::Piece*)
*/
pieces::Piece *get_piece(int x, int y) { return this->board[x][y]; }
/**
* @brief Gets the moves for a piece
*
* @param x The x coordinate
* @param y The y coordinate
* @param from_premove If the function is called from premove_check (to
* prevent infinite recursion)
* @return The moves (std::vector<PlayeChessEngine::Move>)
*/
std::vector<PlayeChessEngine::Move> get_moves(int x, int y, bool from_premove = false) {
std::vector<PlayeChessEngine::Move> moves;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (this->board[x][y] == nullptr)
continue;
if (this->board[x][y]->validation_function(this->board, i, j)) {
PlayeChessEngine::Move move = PlayeChessEngine::Move(x, y, i, j);
// std::cout << "Move of " << this->board[x][y]->show() << " from " << x << ", " << y << " to " << i << ", " << j << std::endl;
if (from_premove) {
moves.push_back(move);
continue;
}
if (!this->premove_check(move, this->board[x][y]->is_white)) {
moves.push_back(move);
}
}
}
}
return moves;
}
/**
* @brief Gets all the moves for a color
*
* @param white If the color is white
* @param from_premove If the function is called from premove_check (to
* prevent infinite recursion)
* @return The moves (std::vector<PlayeChessEngine::Move>)
*/
std::vector<PlayeChessEngine::Move> get_all_moves(std::array<std::array<pieces::Piece *, 8>, 8> brd, bool white, bool from_premove = false) {
std::vector<PlayeChessEngine::Move> moves;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (brd[i][j] == nullptr)
continue;
if (brd[i][j]->is_white != white)
continue;
for (auto move : this->get_moves(i, j, from_premove)) {
moves.push_back(move);
}
}
}
return moves;
}
/**
* @brief Checks if a vector of int is in a vector of vector of int
*
* @param vec The vector of vector of int
* @param val The vector of int
* @return Wether the vector of int is in the vector of vector of int (bool)
*/
bool in(std::vector<std::vector<int>> vec, std::vector<int> val) {
for (auto v : vec) {
if (v == val)
return true;
}
return false;
}
/**
* @brief Get the all landing moves of a color
*
* @param brd The board
* @param white Wether the color is white
* @param from_premove If the function call is from premove_check (to prevent infinite recursion)
* @return All landing moves (std::vector<std::vector<int>>)
*/
std::vector<std::vector<int>> get_all_landing_moves(std::array<std::array<pieces::Piece *, 8>, 8> brd, bool white, bool from_premove = false) {
std::vector<std::vector<int>> moves;
for(auto move : this->get_all_moves(brd, white, from_premove)) {
moves.push_back(move.get_end_coords());
}
return moves;
}
/**
* @brief Checks if a color is in check
*
* @param white If the color is white
* @return If the color is in check (bool)
*/
bool is_check(bool white) {
for (auto move : this->get_all_moves(this->board, !white, true)) {
if (this->board[move.get_end_coords()[0]][move.get_end_coords()[1]] == nullptr)
continue;
if (this->board[move.get_end_coords()[0]][move.get_end_coords()[1]] ->get_type() == pieces::piece_type::k)
return true;
}
return false;
}
/**
* @brief Transfer a piece from a position to another
*
* @param board The board to transfer the piece on
* @param start_x The x coordinate of the start position
* @param start_y The y coordinate of the start position
* @param end_x The x coordinate of the end position
* @param end_y The y coordinate of the end position
* @return Board with the piece positon updated
* (std::array<std::array<pieces::Piece*, 8>, 8>)
*/
std::array<std::array<pieces::Piece *, 8>, 8> transfer(std::array<std::array<pieces::Piece *, 8>, 8> board, int start_x, int start_y, int end_x, int end_y) {
std::swap(board[start_x][start_y], board[end_x][end_y]);
board[start_x][start_y] = nullptr;
return board;
}
/**
* @brief Plays one move in the future to check if it puts the king in check
*
* @param move The move to check
* @param white If the color is white
* @return If the move is legal (bool)
*/
bool premove_check(PlayeChessEngine::Move move, bool white) {
if (this->board[move.get_start_coords()[0]][move.get_start_coords()[1]] == nullptr)
throw std::invalid_argument("No piece at start coords");
std::array<std::array<pieces::Piece *, 8>, 8> backup = this->board;
this->board = this->transfer(this->board, move.get_start_coords()[0], move.get_start_coords()[1], move.get_end_coords()[0], move.get_end_coords()[1]);
bool check = this->is_check(white);
this->board = backup;
return check;
}
/**
* @brief Plays a move
*
* @param move The move to play
* @param white If the color is white
* @return If the move was played (bool)
*/
PlayeChessEngine::Move move(std::vector<PlayeChessEngine::Move> moves, PlayeChessEngine::Move move, bool white) {
if (this->board[move.get_start_coords()[0]][move.get_start_coords()[1]] == nullptr) {
move.set_valid(false);
return move;
} if(this->board[move.get_start_coords()[0]][move.get_start_coords()[1]]->is_white != white) {
move.set_valid(false);
return move;
} if (move.am_in(this->get_moves(move.get_start_coords()[0], move.get_start_coords()[1]))) {
if(this->board[move.get_end_coords()[0]][move.get_end_coords()[1]] != nullptr)
move.set_capture(true);
this->board = this->transfer(this->board, move.get_start_coords()[0], move.get_start_coords()[1], move.get_end_coords()[0], move.get_end_coords()[1]);
this->board[move.get_end_coords()[0]][move.get_end_coords()[1]]->update_coords(move.get_end_coords()[0], move.get_end_coords()[1]);
delete this->board[move.get_start_coords()[0]][move.get_start_coords()[1]];
move.set_valid(true);
return move;
}
std::array<int, 2> start_coords = this->get_en_passant(moves, white);
if(start_coords == std::array<int, 2>{move.get_start_coords()[0], move.get_start_coords()[1]}) {
int side = moves.back().get_end_coords()[1] - start_coords[1];
int offset = (white ? 1 : -1);
if(move.get_end_coords()[0] == start_coords[0] + offset && move.get_end_coords()[1] == start_coords[1] + side) {
this->en_passant(start_coords, std::array<int, 2>{start_coords[0] + offset, start_coords[1] + side}, white);
move.set_valid(true);
move.set_capture(true);
return move;
}
}
move.set_valid(false);
return move;
}
/**
* @brief Gets the status of the game
*
* @param white
* @return 0 if the game is not over, 1 if it is checkmate, 2 if it is
* stalemate (int)