-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
1018 lines (932 loc) · 40.1 KB
/
Game.cpp
File metadata and controls
1018 lines (932 loc) · 40.1 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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Game.h"
#include <string>
#include "Constants.h"
#include <regex>
#include <iostream>
#include <cmath>
using namespace std;
// Placeholder for move legality checking (not implemented yet)
bool Game::is_move_legal() {
return true;
}
// Constructor: Initialize the game with a bitboard array
// board[0-5]: White pieces (Rook, Knight, Bishop, Queen, King, Pawn)
// board[6-11]: Black pieces (Rook, Knight, Bishop, Queen, King, Pawn)
Game::Game(const unsigned long long board[12]) {
for (int i=0;i<12;i++) {
this->board[i]=board[i];
}
}
// Main move function: Parse and execute algebraic notation moves
void Game::move(const string& algebraic_notation) {
// Convert move to lowercase for uniform parsing
// Regex patterns for algebraic notation:
// Pawn: "e4", "exd4" (capture), "e8=q" (promotion)
// Piece: "Nf3", "Raxd5" (disambiguated capture)
const regex pawn_move_regex("^(?:([a-h])x)?([a-h])(?:([1-7]|8|1(?:=([RNQB]))))$");
const regex piece_move_regex("^([RNKQB])([a-h])?([1-8])?(x)?([a-h][1-8])$");
smatch match;
// ========== PAWN MOVEMENT IMPLEMENTATION ==========
if (regex_match(algebraic_notation, match, pawn_move_regex)) {
const char file_to = match[2].str()[0]; // Destination file (e.g., 'd' in "exd4")
const int rank_to = stoi(&match[3].str()[0])-1; // Destination rank (0-indexed)
// ===== PAWN CAPTURE (e.g., "exd4") =====
if (match[1].matched) {
const char file_from = match[1].str()[0]; // Starting file from capture notation
// Validate diagonal movement (one file left or right)
if (file_from-1==file_to || file_from+1==file_to) {
// Calculate source rank: White pawns move up (+1), Black pawns move down (-1)
// pow(-1, Black_to_move) gives: -1 for white, +1 for black
const int rank_from = rank_to - static_cast<int>(pow(-1, Black_to_move));
// Prevent backward movement: white must increase rank, black must decrease
if ((!Black_to_move && rank_to <= rank_from) || (Black_to_move && rank_to >= rank_from)) {
cerr<<"Pawns cannot move backwards"<<endl;
exit(1);
}
// Check if destination square has an opponent piece
// OR all opponent pieces together (indices adjusted by Black_to_move)
// If white's turn: check board[6-11], if black's turn: check board[0-5]
if (((board[6-6*Black_to_move]|board[7-6*Black_to_move]|board[8-6*Black_to_move]|board[9-6*Black_to_move]
|board[10-6*Black_to_move]|board[11-6*Black_to_move]) & SQUARES[rank_to][(file_to - 'a')])== SQUARES[rank_to][(file_to - 'a')]) {
// Verify that our pawn exists at the expected source square
// board[5] = white pawns, board[11] = black pawns
if ((board[5+6*Black_to_move] & SQUARES[rank_from][(file_from - 'a')])== SQUARES[rank_from][(file_from - 'a')]) {
// Remove pawn from source square (bitwise AND with NOT)
board[5+6*Black_to_move] &= ~SQUARES[rank_from][(file_from - 'a')];
// Place pawn on destination square (bitwise OR)
board[5+6*Black_to_move] |= SQUARES[rank_to][(file_to - 'a')];
// Remove the captured opponent piece from all piece bitboards
for (int i=6;i<12;i++) {
if ((board[i-6*Black_to_move] & SQUARES[rank_to][(file_to - 'a')])== SQUARES[rank_to][(file_to - 'a')]) {
board[i-6*Black_to_move] &= ~SQUARES[rank_to][(file_to - 'a')];
break;
}
}
}
else {
cerr<<"Invalid capture move"<<endl;
exit(1);
}
}
else {
cerr<<"Invalid capture move"<<endl;
exit(1);
}
}
else {
cerr<<"invalid move: "<<algebraic_notation<<endl;
exit(1);
}
}
// ===== REGULAR PAWN MOVE (non-capture, e.g., "e4") =====
else{
// Calculate potential source squares for single and double moves
const int rank_from_single = rank_to - static_cast<int>(pow(-1, Black_to_move));
const int rank_from_double = rank_to - 2*static_cast<int>(pow(-1, Black_to_move));
// Sanity check: prevent edge case invalid moves
if ((!Black_to_move && rank_to <= 0) || (Black_to_move && rank_to >= 7)) {
cerr<<"Invalid pawn move"<<endl;
exit(1);
}
// Try single square forward move
if ((board[5+6*Black_to_move] & SQUARES[rank_from_single][(file_to - 'a')])== SQUARES[rank_from_single][(file_to - 'a')]) {
// Check if destination square is empty (no pieces blocking)
unsigned long long all_pieces = 0;
for (unsigned long long i : board) {
all_pieces |= i;
}
if (all_pieces & SQUARES[rank_to][(file_to - 'a')]) {
cerr<<"Destination square is occupied"<<endl;
exit(1);
}
// Remove pawn from source square
board[5+6*Black_to_move] &= ~SQUARES[rank_from_single][(file_to - 'a')];
// Place pawn on destination square
board[5+6*Black_to_move] |= SQUARES[rank_to][(file_to - 'a')];
}
// Try double square forward move (from starting position)
// rank_to==3 means white moved to rank 4, rank_to==4 means black moved to rank 5
else if ((rank_to==3||rank_to==4) && (board[5+6*Black_to_move] & SQUARES[rank_from_double][(file_to - 'a')])== SQUARES[rank_from_double][(file_to - 'a')]) {
// Check if both destination and intermediate squares are empty
unsigned long long all_pieces = 0;
for (unsigned long long i : board) {
all_pieces |= i;
}
// Check destination square
if (all_pieces & SQUARES[rank_to][(file_to - 'a')]) {
cerr<<"Destination square is occupied"<<endl;
exit(1);
}
// Check intermediate square (the square pawn passes through)
if (all_pieces & SQUARES[rank_from_single][(file_to - 'a')]) {
cerr<<"Path is blocked for double pawn move"<<endl;
exit(1);
}
// Remove pawn from starting square (rank 2 for white, rank 7 for black)
board[5+6*Black_to_move] &= ~SQUARES[rank_from_double][(file_to - 'a')];
// Place pawn on destination square
board[5+6*Black_to_move] |= SQUARES[rank_to][(file_to - 'a')];
}
else {
cerr<<"Invalid move"<<endl;
exit(1);
}
}
// ===== PAWN PROMOTION (e.g., "e8=q") =====
if (match[4].matched) {
// Convert pawn to promoted piece on the destination square
switch (match[4].str()[0]) {
case 'R': // Promote to Rook
board[0+6*Black_to_move] |= SQUARES[rank_to][(file_to - 'a')];
board[5+6*Black_to_move] &= ~SQUARES[rank_to][(file_to - 'a')];
break;
case 'N': // Promote to Knight
board[1+6*Black_to_move] |= SQUARES[rank_to][(file_to - 'a')];
board[5+6*Black_to_move] &= ~SQUARES[rank_to][(file_to - 'a')];
break;
case 'B': // Promote to Bishop
board[2+6*Black_to_move] |= SQUARES[rank_to][(file_to - 'a')];
board[5+6*Black_to_move] &= ~SQUARES[rank_to][(file_to - 'a')];
break;
case 'Q': // Promote to Queen
board[3+6*Black_to_move] |= SQUARES[rank_to][(file_to - 'a')];
board[5+6*Black_to_move] &= ~SQUARES[rank_to][(file_to - 'a')];
break;
default:
cerr<<"Invalid promotion piece: "<<match[4].str()[0]<<endl;
exit(1);
}
}
// Switch turns after successful pawn move
Black_to_move=!Black_to_move;
}
// ========== PIECE MOVEMENT (Rook, Knight, Bishop, Queen, King) ==========
else if (regex_match(algebraic_notation, match, piece_move_regex)) {
// Debug: print regex match groups
for (int i=0;i<match.size();i++) {
cout<<match[i].str()<<":"<<i<<" ";
}
cout<<endl;
// Route to appropriate piece handler based on piece type
switch (match[1].str()[0]) {
case 'R': // Rook move
move_rook(match);
break;
case 'N': // Knight move
move_knight(match);
break;
case 'B': // Bishop move
move_bishop(match);
break;
case 'Q': // Queen move
move_queen(match);
break;
case 'K': // King move
move_king(match);
break;
default:
cerr<<"Invalid piece: "<<match[1].str()[0]<<endl;
exit(2);
break;
}
}
else {
cerr<<"Invalid move notation: "<<algebraic_notation<<endl;
exit(3);
}
}
// ========== ROOK MOVEMENT HANDLER ==========
// Handles moves like "Re4", "Raxd5" (capture), "R1e4" (rank disambiguation), "Rae4" (file disambiguation)
void Game::move_rook(const smatch& match) {
char file_from=-1,rank_from=-1; // Initialize to -1 to track if disambiguation is needed
const char file_to=match[5].str()[0]-'a'; // Destination file (0-7 for a-h)
const char rank_to=match[5].str()[1]-'1'; // Destination rank (0-7 for 1-8)
// Parse disambiguation: which rook is moving?
// match[2]: file disambiguation (e.g., "Raxd5" means rook from 'a' file)
// match[3]: rank disambiguation (e.g., "R1xd5" means rook from rank 1)
if (match[2].matched) {
file_from=match[3].str()[0]-'a';
}
if (match[3].matched) {
rank_from=match[4].str()[0]-'1';
}
// No disambiguation provided: find which rook can reach the destination
if (file_from==-1 && rank_from==-1) {
// First, try to find a rook on the same file (vertical move)
for (char i=0;i<8;i++) {
if ((board[0+6*Black_to_move] & SQUARES[i][file_to])== SQUARES[i][file_to]) {
cout<<" Found rook on same file"<<endl;
rank_from=i;
file_from=file_to;
break;
}
}
// If no rook found on same file, try same rank (horizontal move)
if (rank_from==-1) {
for (char i=0;i<8;i++) {
if ((board[0+6*Black_to_move] & SQUARES[rank_to][i])== SQUARES[rank_to][i]) {
file_from=i;
rank_from=rank_to;
break;
}
}
}
}
// Rank is known, find file (search along the rank)
else if (file_from==-1) {
for (char i=0;i<8;i++) {
if (board[0+6*Black_to_move] & SQUARES[rank_from][i]) {
file_from=i;
break;
}
}
}
// File is known, find rank (search along the file)
else if (rank_from==-1) {
for (char i=0;i<8;i++) {
if (board[0+6*Black_to_move] & SQUARES[i][file_from]) {
rank_from=i;
break;
}
}
}
// Execute vertical move (same file, different ranks)
if (file_from==file_to) {
// Check if path is clear (no pieces blocking)
// Combine all pieces into one bitboard for obstacle detection
unsigned long long all_pieces = 0;
for (unsigned long long i : board) {
all_pieces |= i;
}
// Check each square between source and destination
char start = rank_from < rank_to ? rank_from + 1 : rank_to + 1;
char end = rank_from < rank_to ? rank_to : rank_from;
for (char rank = start; rank < end; rank++) {
if (all_pieces & SQUARES[rank][file_from]) {
cerr<<"VPath is blocked for rook move"<<endl;
exit(5);
}
}
// Validate capture vs non-capture
// Check if destination has an opponent piece
unsigned long long opponent_pieces = 0;
for (int i=0;i<6;i++) {
opponent_pieces |= board[i+6-6*Black_to_move];
}
bool has_opponent = (opponent_pieces & SQUARES[rank_to][file_to]) == SQUARES[rank_to][file_to];
if (match[4].matched && !has_opponent) {
cerr<<"No opponent piece to capture at destination"<<endl;
exit(6);
}
if (!match[4].matched && has_opponent) {
cerr<<"Must use capture notation (x) when capturing"<<endl;
exit(6);
}
if (!match[4].matched && (all_pieces & SQUARES[rank_to][file_to])) {
cerr<<"Destination square is occupied"<<endl;
exit(6);
}
// Remove rook from source square
board[0+6*Black_to_move] &= ~SQUARES[rank_from][file_from];
// Place rook on destination square
board[0+6*Black_to_move] |= SQUARES[rank_to][file_to];
// If capture, remove opponent piece
if (match[4].matched) {
// Loop through all opponent piece types (indices 0-5 for one color, 6-11 for other)
for (int i=0;i<6;i++) {
if ((board[i+6-6*Black_to_move] & SQUARES[rank_to][file_to])== SQUARES[rank_to][file_to]) {
board[i+6-6*Black_to_move] &= ~SQUARES[rank_to][file_to];
break;
}
}
}
}
// Execute horizontal move (same rank, different files)
else if (rank_from==rank_to) {
// Check if path is clear (no pieces blocking)
// Combine all pieces into one bitboard for obstacle detection
unsigned long long all_pieces = 0;
for (unsigned long long i : board) {
all_pieces |= i;
}
// Check each square between source and destination
char start = file_from < file_to ? file_from + 1 : file_to + 1;
char end = file_from < file_to ? file_to : file_from;
for (char file = start; file < end; file++) {
if (all_pieces & SQUARES[rank_from][file]) {
cerr<<"Path is blocked for rook move"<<endl;
exit(5);
}
}
// Validate capture vs non-capture
// Check if destination has an opponent piece
unsigned long long opponent_pieces = 0;
for (int i=0;i<6;i++) {
opponent_pieces |= board[i+6-6*Black_to_move];
}
bool has_opponent = (opponent_pieces & SQUARES[rank_to][file_to]) == SQUARES[rank_to][file_to];
if (match[2].matched && !has_opponent) {
cerr<<"No opponent piece to capture at destination"<<endl;
exit(6);
}
if (!match[2].matched && has_opponent) {
cerr<<"Must use capture notation (x) when capturing"<<endl;
exit(6);
}
if (!match[2].matched && (all_pieces & SQUARES[rank_to][file_to])) {
cerr<<"Destination square is occupied"<<endl;
exit(6);
}
// Remove rook from source square
board[0+6*Black_to_move] &= ~SQUARES[rank_from][file_from];
// Place rook on destination square
board[0+6*Black_to_move] |= SQUARES[rank_to][file_to];
// If capture, remove opponent piece
if (match[2].matched) {
// Loop through all opponent piece types (indices 0-5 for one color, 6-11 for other)
for (int i=0;i<6;i++) {
if ((board[i+6-6*Black_to_move] & SQUARES[rank_to][file_to])== SQUARES[rank_to][file_to]) {
board[i+6-6*Black_to_move] &= ~SQUARES[rank_to][file_to];
break;
}
}
}
}
else {
// Rooks can only move horizontally or vertically
cerr<<"Invalid rook move"<<endl;
exit(4);
}
// Switch turns after successful rook move
Black_to_move=!Black_to_move;
}
// ========== KNIGHT MOVEMENT HANDLER ==========
// Handles moves like "Nf3", "Nxd5" (capture), "Nce4" (file disambiguation), "N3e4" (rank disambiguation)
void Game::move_knight(smatch& match) {
char file_from=-1, rank_from=-1; // Initialize to -1 to track if disambiguation is needed
const char file_to=match[5].str()[0]-'a'; // Destination file (0-7 for a-h)
const char rank_to=match[5].str()[1]-'1'; // Destination rank (0-7 for 1-8)
// Parse disambiguation: which knight is moving?
// match[2]: file disambiguation (e.g., "Ncxd5" means knight from 'c' file)
// match[3]: rank disambiguation (e.g., "N3xd5" means knight from rank 3)
if (!match[2].str().empty()) {
file_from=match[2].str()[0]-'a';
}
if (!match[3].str().empty()) {
rank_from=match[3].str()[0]-'1';
}
// All possible knight moves (L-shaped): 2+1 or 1+2 in perpendicular directions
// There are 8 possible L-shaped moves from any square
const int knight_moves[8][2] = {
{2, 1}, // 2 up, 1 right
{2, -1}, // 2 up, 1 left
{-2, 1}, // 2 down, 1 right
{-2, -1}, // 2 down, 1 left
{1, 2}, // 1 up, 2 right
{1, -2}, // 1 up, 2 left
{-1, 2}, // 1 down, 2 right
{-1, -2} // 1 down, 2 left
};
// No disambiguation provided: find which knight can reach the destination
if (file_from==-1 && rank_from==-1) {
// Check all 8 possible source squares that a knight could move from
for (auto knight_move : knight_moves) {
char test_rank = rank_to - knight_move[0];
char test_file = file_to - knight_move[1];
// Check if source square is on the board
if (test_rank >= 0 && test_rank < 8 && test_file >= 0 && test_file < 8) {
// Check if our knight is at this position
if ((board[1+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
rank_from = test_rank;
file_from = test_file;
break;
}
}
}
}
// File is known, find rank
else if (rank_from==-1) {
// Check all 8 possible knight moves from file_from to file_to
for (int i=0; i<8; i++) {
char test_rank = rank_to - knight_moves[i][0];
char test_file = file_to - knight_moves[i][1];
// Check if this matches our known file and is on the board
if (test_file == file_from && test_rank >= 0 && test_rank < 8) {
// Check if our knight is at this position
if ((board[1+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
rank_from = test_rank;
break;
}
}
}
}
// Rank is known, find file
else if (file_from==-1) {
// Check all 8 possible knight moves from rank_from to rank_to
for (auto knight_move : knight_moves) {
char test_rank = rank_to - knight_move[0];
char test_file = file_to - knight_move[1];
// Check if this matches our known rank and is on the board
if (test_rank == rank_from && test_file >= 0 && test_file < 8) {
// Check if our knight is at this position
if ((board[1+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
file_from = test_file;
break;
}
}
}
}
// Validate that move is a valid knight move (L-shaped)
char rank_diff = abs(rank_to - rank_from);
char file_diff = abs(file_to - file_from);
if (!((rank_diff == 2 && file_diff == 1) || (rank_diff == 1 && file_diff == 2))) {
cerr<<"Knights move in an L-shape (2+1 or 1+2 squares)"<<endl;
exit(4);
}
// Knights jump over pieces, so no path checking needed!
// Just check the destination square
// Combine all pieces for checking destination
unsigned long long all_pieces = 0;
for (unsigned long long i : board) {
all_pieces |= i;
}
// Validate capture vs non-capture
unsigned long long opponent_pieces = 0;
for (int i=0; i<6; i++) {
opponent_pieces |= board[i+6-6*Black_to_move];
}
bool has_opponent = (opponent_pieces & SQUARES[rank_to][file_to]) == SQUARES[rank_to][file_to];
if (match[4].matched && !has_opponent) {
cerr<<"No opponent piece to capture at destination"<<endl;
exit(6);
}
if (!match[4].matched && has_opponent) {
cerr<<"Must use capture notation (x) when capturing"<<endl;
exit(6);
}
if (!match[4].matched && (all_pieces & SQUARES[rank_to][file_to])) {
cerr<<"Destination square is occupied"<<endl;
exit(6);
}
// Remove knight from source square
board[1+6*Black_to_move] &= ~SQUARES[rank_from][file_from];
// Place knight on destination square
board[1+6*Black_to_move] |= SQUARES[rank_to][file_to];
// If capture, remove opponent piece
if (match[4].matched) {
// Loop through all opponent piece types
for (int i=0; i<6; i++) {
if ((board[i+6-6*Black_to_move] & SQUARES[rank_to][file_to]) == SQUARES[rank_to][file_to]) {
board[i+6-6*Black_to_move] &= ~SQUARES[rank_to][file_to];
break;
}
}
}
// Switch turns after successful knight move
Black_to_move=!Black_to_move;
}
// ========== BISHOP MOVEMENT HANDLER ==========
// Handles moves like "Be4", "Bxd5" (capture), "Bce4" (file disambiguation), "B3e4" (rank disambiguation)
void Game::move_bishop(smatch& match) {
char file_from=-1, rank_from=-1; // Initialize to -1 to track if disambiguation is needed
const char file_to=match[5].str()[0]-'a'; // Destination file (0-7 for a-h)
const char rank_to=match[5].str()[1]-'1'; // Destination rank (0-7 for 1-8)
// Parse disambiguation: which bishop is moving?
// match[2]: file disambiguation (e.g., "Bcxd5" means bishop from 'c' file)
// match[3]: rank disambiguation (e.g., "B3xd5" means bishop from rank 3)
if (!match[2].str().empty()) {
file_from=match[2].str()[0]-'a';
}
if (!match[3].str().empty()) {
rank_from=match[3].str()[0]-'1';
}
// No disambiguation provided: find which bishop can reach the destination diagonally
if (file_from==-1 && rank_from==-1) {
// Search all four diagonal directions from the destination
// Direction 1: Up-Right diagonal (decreasing rank, increasing file)
for (char i=1; i<8; i++) {
char test_rank = rank_to - i;
char test_file = file_to - i;
if (test_rank < 0 || test_file < 0) break;
if ((board[2+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
rank_from = test_rank;
file_from = test_file;
break;
}
}
// Direction 2: Up-Left diagonal (decreasing rank, decreasing file)
if (rank_from == -1) {
for (char i=1; i<8; i++) {
char test_rank = rank_to - i;
char test_file = file_to + i;
if (test_rank < 0 || test_file > 7) break;
if ((board[2+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
rank_from = test_rank;
file_from = test_file;
break;
}
}
}
// Direction 3: Down-Right diagonal (increasing rank, increasing file)
if (rank_from == -1) {
for (char i=1; i<8; i++) {
char test_rank = rank_to + i;
char test_file = file_to - i;
if (test_rank > 7 || test_file < 0) break;
if ((board[2+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
rank_from = test_rank;
file_from = test_file;
break;
}
}
}
// Direction 4: Down-Left diagonal (increasing rank, decreasing file)
if (rank_from == -1) {
for (char i=1; i<8; i++) {
char test_rank = rank_to + i;
char test_file = file_to + i;
if (test_rank > 7 || test_file > 7) break;
if ((board[2+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
rank_from = test_rank;
file_from = test_file;
break;
}
}
}
}
// File is known, find rank by searching along diagonals through that file
else if (rank_from==-1) {
// Search diagonals that pass through file_from and reach file_to
for (char i=0; i<8; i++) {
// Check if this square is on a diagonal to the destination
if (abs(i - rank_to) == abs(file_from - file_to)) {
if ((board[2+6*Black_to_move] & SQUARES[i][file_from]) == SQUARES[i][file_from]) {
rank_from = i;
break;
}
}
}
}
// Rank is known, find file by searching along diagonals through that rank
else if (file_from==-1) {
// Search diagonals that pass through rank_from and reach rank_to
for (char i=0; i<8; i++) {
// Check if this square is on a diagonal to the destination
if (abs(rank_from - rank_to) == abs(i - file_to)) {
if ((board[2+6*Black_to_move] & SQUARES[rank_from][i]) == SQUARES[rank_from][i]) {
file_from = i;
break;
}
}
}
}
// Validate that move is diagonal
if (abs(rank_from - rank_to) != abs(file_from - file_to)) {
cerr<<"Bishops can only move diagonally"<<endl;
exit(4);
}
// Check if path is clear (no pieces blocking the diagonal)
unsigned long long all_pieces = 0;
for (int i = 0; i < 12; i++) {
all_pieces |= board[i];
}
// Determine direction of movement
char rank_dir = (rank_to > rank_from) ? 1 : -1;
char file_dir = (file_to > file_from) ? 1 : -1;
// Check each square along the diagonal between source and destination
char check_rank = rank_from + rank_dir;
char check_file = file_from + file_dir;
while (check_rank != rank_to || check_file != file_to) {
if (all_pieces & SQUARES[check_rank][check_file]) {
cerr<<"Path is blocked for bishop move"<<endl;
exit(5);
}
check_rank += rank_dir;
check_file += file_dir;
}
// Validate capture vs non-capture
// Check if destination has an opponent piece
unsigned long long opponent_pieces = 0;
for (int i=0; i<6; i++) {
opponent_pieces |= board[i+6-6*Black_to_move];
}
bool has_opponent = (opponent_pieces & SQUARES[rank_to][file_to]) == SQUARES[rank_to][file_to];
if (match[4].matched && !has_opponent) {
cerr<<"No opponent piece to capture at destination"<<endl;
exit(6);
}
if (!match[4].matched && has_opponent) {
cerr<<"Must use capture notation (x) when capturing"<<endl;
exit(6);
}
if (!match[4].matched && (all_pieces & SQUARES[rank_to][file_to])) {
cerr<<"Destination square is occupied"<<endl;
exit(6);
}
// Remove bishop from source square
board[2+6*Black_to_move] &= ~SQUARES[rank_from][file_from];
// Place bishop on destination square
board[2+6*Black_to_move] |= SQUARES[rank_to][file_to];
// If capture, remove opponent piece
if (match[4].matched) {
// Loop through all opponent piece types (indices 0-5 for one color, 6-11 for other)
for (int i=0; i<6; i++) {
if ((board[i+6-6*Black_to_move] & SQUARES[rank_to][file_to]) == SQUARES[rank_to][file_to]) {
board[i+6-6*Black_to_move] &= ~SQUARES[rank_to][file_to];
break;
}
}
}
// Switch turns after successful bishop move
Black_to_move=!Black_to_move;
}
// ========== QUEEN MOVEMENT HANDLER ==========
// Handles moves like "Qe4", "Qxd5" (capture), "Qce4" (file disambiguation), "Q3e4" (rank disambiguation)
// Queens move like rooks (horizontal/vertical) OR bishops (diagonal)
void Game::move_queen(smatch& match) {
char file_from=-1, rank_from=-1; // Initialize to -1 to track if disambiguation is needed
const char file_to=match[5].str()[0]-'a'; // Destination file (0-7 for a-h)
const char rank_to=match[5].str()[1]-'1'; // Destination rank (0-7 for 1-8)
// Parse disambiguation: which queen is moving?
// match[2]: file disambiguation (e.g., "Qcxd5" means queen from 'c' file)
// match[3]: rank disambiguation (e.g., "Q3xd5" means queen from rank 3)
if (!match[2].str().empty()) {
file_from=match[2].str()[0]-'a';
}
if (!match[3].str().empty()) {
rank_from=match[3].str()[0]-'1';
}
// No disambiguation provided: find which queen can reach the destination
if (file_from==-1 && rank_from==-1) {
// First, try vertical move (same file)
for (char i=0;i<8;i++) {
if ((board[3+6*Black_to_move] & SQUARES[i][file_to]) == SQUARES[i][file_to]) {
rank_from=i;
file_from=file_to;
break;
}
}
// Try horizontal move (same rank)
if (rank_from == -1) {
for (char i=0;i<8;i++) {
if ((board[3+6*Black_to_move] & SQUARES[rank_to][i]) == SQUARES[rank_to][i]) {
file_from=i;
rank_from=rank_to;
break;
}
}
}
// Try diagonal moves - check all 4 diagonal directions
if (rank_from == -1) {
// Direction 1: Up-Right diagonal
for (char i=1; i<8; i++) {
char test_rank = rank_to - i;
char test_file = file_to - i;
if (test_rank < 0 || test_file < 0) break;
if ((board[3+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
rank_from = test_rank;
file_from = test_file;
break;
}
}
}
if (rank_from == -1) {
// Direction 2: Up-Left diagonal
for (char i=1; i<8; i++) {
char test_rank = rank_to - i;
char test_file = file_to + i;
if (test_rank < 0 || test_file > 7) break;
if ((board[3+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
rank_from = test_rank;
file_from = test_file;
break;
}
}
}
if (rank_from == -1) {
// Direction 3: Down-Right diagonal
for (char i=1; i<8; i++) {
char test_rank = rank_to + i;
char test_file = file_to - i;
if (test_rank > 7 || test_file < 0) break;
if ((board[3+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
rank_from = test_rank;
file_from = test_file;
break;
}
}
}
if (rank_from == -1) {
// Direction 4: Down-Left diagonal
for (char i=1; i<8; i++) {
char test_rank = rank_to + i;
char test_file = file_to + i;
if (test_rank > 7 || test_file > 7) break;
if ((board[3+6*Black_to_move] & SQUARES[test_rank][test_file]) == SQUARES[test_rank][test_file]) {
rank_from = test_rank;
file_from = test_file;
break;
}
}
}
}
// File is known, find rank
else if (rank_from==-1) {
// Try vertical move first (same file)
for (char i=0;i<8;i++) {
if ((board[3+6*Black_to_move] & SQUARES[i][file_from]) == SQUARES[i][file_from]) {
rank_from=i;
break;
}
}
// If not found, try diagonals through this file
if (rank_from==-1) {
for (char i=0; i<8; i++) {
if (abs(i - rank_to) == abs(file_from - file_to)) {
if ((board[3+6*Black_to_move] & SQUARES[i][file_from]) == SQUARES[i][file_from]) {
rank_from = i;
break;
}
}
}
}
}
// Rank is known, find file
else if (file_from==-1) {
// Try horizontal move first (same rank)
for (char i=0;i<8;i++) {
if ((board[3+6*Black_to_move] & SQUARES[rank_from][i]) == SQUARES[rank_from][i]) {
file_from=i;
break;
}
}
// If not found, try diagonals through this rank
if (file_from==-1) {
for (char i=0; i<8; i++) {
if (abs(rank_from - rank_to) == abs(i - file_to)) {
if ((board[3+6*Black_to_move] & SQUARES[rank_from][i]) == SQUARES[rank_from][i]) {
file_from = i;
break;
}
}
}
}
}
// Determine move type: horizontal, vertical, or diagonal
bool is_horizontal = (rank_from == rank_to && file_from != file_to);
bool is_vertical = (file_from == file_to && rank_from != rank_to);
bool is_diagonal = (abs(rank_from - rank_to) == abs(file_from - file_to) && rank_from != rank_to);
if (!is_horizontal && !is_vertical && !is_diagonal) {
cerr<<"Queens can only move horizontally, vertically, or diagonally"<<endl;
exit(4);
}
// Check if path is clear (no pieces blocking)
unsigned long long all_pieces = 0;
for (int i = 0; i < 12; i++) {
all_pieces |= board[i];
}
// Check path based on move type
if (is_vertical) {
// Check vertical path
char start = rank_from < rank_to ? rank_from + 1 : rank_to + 1;
char end = rank_from < rank_to ? rank_to : rank_from;
for (char rank = start; rank < end; rank++) {
if (all_pieces & SQUARES[rank][file_from]) {
cerr<<"Path is blocked for queen move"<<endl;
exit(5);
}
}
}
else if (is_horizontal) {
// Check horizontal path
char start = file_from < file_to ? file_from + 1 : file_to + 1;
char end = file_from < file_to ? file_to : file_from;
for (char file = start; file < end; file++) {
if (all_pieces & SQUARES[rank_from][file]) {
cerr<<"Path is blocked for queen move"<<endl;
exit(5);
}
}
}
else if (is_diagonal) {
// Check diagonal path
char rank_dir = (rank_to > rank_from) ? 1 : -1;
char file_dir = (file_to > file_from) ? 1 : -1;
char check_rank = rank_from + rank_dir;
char check_file = file_from + file_dir;
while (check_rank != rank_to || check_file != file_to) {
if (all_pieces & SQUARES[check_rank][check_file]) {
cerr<<"Path is blocked for queen move"<<endl;
exit(5);
}
check_rank += rank_dir;
check_file += file_dir;
}
}
// Validate capture vs non-capture
unsigned long long opponent_pieces = 0;
for (int i=0; i<6; i++) {
opponent_pieces |= board[i+6-6*Black_to_move];
}
bool has_opponent = (opponent_pieces & SQUARES[rank_to][file_to]) == SQUARES[rank_to][file_to];
if (match[4].matched && !has_opponent) {
cerr<<"No opponent piece to capture at destination"<<endl;
exit(6);
}
if (!match[4].matched && has_opponent) {
cerr<<"Must use capture notation (x) when capturing"<<endl;
exit(6);
}
if (!match[4].matched && (all_pieces & SQUARES[rank_to][file_to])) {
cerr<<"Destination square is occupied"<<endl;
exit(6);
}
// Remove queen from source square
board[3+6*Black_to_move] &= ~SQUARES[rank_from][file_from];
// Place queen on destination square
board[3+6*Black_to_move] |= SQUARES[rank_to][file_to];
// If capture, remove opponent piece
if (match[4].matched) {
// Loop through all opponent piece types
for (int i=0; i<6; i++) {
if ((board[i+6-6*Black_to_move] & SQUARES[rank_to][file_to]) == SQUARES[rank_to][file_to]) {
board[i+6-6*Black_to_move] &= ~SQUARES[rank_to][file_to];
break;
}
}
}
// Switch turns after successful queen move
Black_to_move=!Black_to_move;
}
// ========== KING MOVEMENT HANDLER ==========
// Handles moves like "Ke4", "Kxd5" (capture)
// Kings move exactly one square in any direction (horizontal, vertical, or diagonal)
void Game::move_king(smatch& match) {
const char file_to=match[5].str()[0]-'a'; // Destination file (0-7 for a-h)
const char rank_to=match[5].str()[1]-'1'; // Destination rank (0-7 for 1-8)
// Find the king's current position
// board[4] = white king, board[10] = black king
char file_from=-1, rank_from=-1;
for (char rank=0; rank<8; rank++) {
for (char file=0; file<8; file++) {
if ((board[4+6*Black_to_move] & SQUARES[rank][file]) == SQUARES[rank][file]) {
rank_from = rank;
file_from = file;
break;
}
}
if (rank_from != -1) break;
}
if (rank_from == -1 || file_from == -1) {
cerr<<"King not found on board"<<endl;
exit(7);
}
// Validate that move is exactly one square in any direction
char rank_diff = abs(rank_to - rank_from);
char file_diff = abs(file_to - file_from);
if (rank_diff > 1 || file_diff > 1 || (rank_diff == 0 && file_diff == 0)) {
cerr<<"Kings can only move one square in any direction"<<endl;
exit(4);
}
// Combine all pieces for checking destination
unsigned long long all_pieces = 0;
for (unsigned long long i : board) {
all_pieces |= i;
}
// Validate capture vs non-capture
unsigned long long opponent_pieces = 0;
for (int i=0; i<6; i++) {
opponent_pieces |= board[i+6-6*Black_to_move];
}
bool has_opponent = (opponent_pieces & SQUARES[rank_to][file_to]) == SQUARES[rank_to][file_to];
if (match[4].matched && !has_opponent) {
cerr<<"No opponent piece to capture at destination"<<endl;
exit(6);
}
if (!match[4].matched && has_opponent) {
cerr<<"Must use capture notation (x) when capturing"<<endl;
exit(6);
}
if (!match[4].matched && (all_pieces & SQUARES[rank_to][file_to])) {
cerr<<"Destination square is occupied"<<endl;
exit(6);
}
// Check if destination is attacked by opponent (not allowing king to move into check)
// For now, we just prevent moving onto a square occupied by our own pieces
// Remove king from source square