lumifox_chess 0.1.0

A high-performance, no_std-capable chess engine library (bitboards and move generation).
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
/*
 * A high-performance chess library licensed under the LGPLv3.
 * Copyright (C) 2025 Clifton Toaster Reid
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <https://opensource.org/license/lgpl-3-0>.
 */

use crate::{
  constants::{A1, A8, D1, D8, F1, F8, H1, H8},
  legal::attack::is_square_attacked,
  model::piecemove::{PieceMove, PromotionType},
};

use super::bitboard::BitBoard;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PieceType {
  Pawn,
  Knight,
  Bishop,
  Rook,
  Queen,
  King,
}

#[derive(Clone, Copy, Debug)]
pub struct GameBoard {
  // Boards for each piece type
  pub pawns: BitBoard,
  pub knights: BitBoard,
  pub bishops: BitBoard,
  pub rooks: BitBoard,
  pub queens: BitBoard,
  pub kings: BitBoard,

  // Now for additional metadata
  pub colour: BitBoard, // BitBoard indicating which pieces are white (1) or black (0)
  pub castling: u8,
  pub en_passant: PieceMove,
  pub playing: bool, // true if it's white's turn to play
}

impl Default for GameBoard {
  fn default() -> Self {
    GameBoard {
      pawns: BitBoard::EMPTY,
      knights: BitBoard::EMPTY,
      bishops: BitBoard::EMPTY,
      rooks: BitBoard::EMPTY,
      queens: BitBoard::EMPTY,
      kings: BitBoard::EMPTY,
      colour: BitBoard::EMPTY,
      castling: 0,
      en_passant: PieceMove::NULL,
      playing: true,
    }
  }
}

impl GameBoard {
  pub fn new() -> Self {
    GameBoard::default()
  }

  pub fn reset(&mut self) {
    *self = GameBoard::new();
  }

  pub fn combined(&self) -> BitBoard {
    self.pawns | self.knights | self.bishops | self.rooks | self.queens | self.kings
  }

  pub fn combined_coloured(&self, desired: bool) -> BitBoard {
    self.combined() & (self.colour ^ desired)
  }

  pub fn casling_right_white(&self) -> (bool, bool) {
    (
      (self.castling & 0b0001) != 0, // White kingside
      (self.castling & 0b0010) != 0, // White queenside
    )
  }

  pub fn casling_right_black(&self) -> (bool, bool) {
    (
      (self.castling & 0b0100) != 0, // Black kingside
      (self.castling & 0b1000) != 0, // Black queenside
    )
  }

  pub(crate) fn find_king(&self, is_white: bool) -> Option<u8> {
    let king_board = if is_white {
      self.kings & self.colour
    } else {
      self.kings & !self.colour
    };

    if king_board.raw() != BitBoard::EMPTY.raw() {
      Some(king_board.raw().trailing_zeros() as u8)
    } else {
      None
    }
  }
  /// Check that all squares between `from` and `to` are empty (exclusive).
  fn is_path_clear(&self, from: u8, to: u8) -> bool {
    let from_rank = (from / 8) as i8;
    let from_file = (from % 8) as i8;
    let to_rank = (to / 8) as i8;
    let to_file = (to % 8) as i8;
    let dr = (to_rank - from_rank).signum();
    let df = (to_file - from_file).signum();
    let mut r = from_rank + dr;
    let mut f = from_file + df;
    while r != to_rank || f != to_file {
      let sq = (r * 8 + f) as u8;
      if self.combined().get_bit(sq).unwrap_or(false) {
        return false;
      }
      r += dr;
      f += df;
    }
    true
  }

  pub fn is_move_legal(&self, piece_move: &PieceMove) -> bool {
    // 1. Is the piece being moved the correct color for the current turn?
    if !self.is_correct_turn_piece(piece_move) {
      #[cfg(feature = "std")]
      eprintln!("Failed: is_correct_turn_piece");
      return false;
    }

    // 2. Is the move type allowed for the piece?
    if !self.is_piece_move_valid(piece_move) {
      #[cfg(feature = "std")]
      eprintln!("Failed: is_piece_move_valid");
      return false;
    }

    // 3. Is the destination square occupied by a friendly piece?
    if !self.is_destination_valid(piece_move) {
      #[cfg(feature = "std")]
      eprintln!("Failed: is_destination_valid");
      return false;
    }

    // 4. Check special moves (castling, en passant, promotion)
    if !self.are_special_moves_valid(piece_move) {
      #[cfg(feature = "std")]
      eprintln!("Failed: are_special_moves_valid");
      return false;
    }

    // 5. Is the move not leaving the moving side's king in check?
    if !self.does_not_leave_king_in_check(piece_move) {
      #[cfg(feature = "std")]
      eprintln!("Failed: does_not_leave_king_in_check");
      return false;
    }

    true
  }

  /// Check if the piece being moved belongs to the player whose turn it is
  fn is_correct_turn_piece(&self, piece_move: &PieceMove) -> bool {
    self
      .colour
      .get_bit(piece_move.from_square())
      .is_some_and(|f| f == self.playing)
  }

  /// Check if the destination square is valid (not occupied by friendly piece, not capturing king)
  fn is_destination_valid(&self, piece_move: &PieceMove) -> bool {
    let to = piece_move.to_square();

    // Cannot move to square occupied by friendly piece
    if let Some(_) = self.get_piece(to)
      && self.colour.get_bit(to).is_some_and(|f| f == self.playing)
    {
      return false;
    }

    // Rule 9: It's illegal to capture the opponent's king
    if let Some(PieceType::King) = self.get_piece(to) {
      return false;
    }

    true
  }

  /// Check if the piece move follows the movement rules for that piece type
  fn is_piece_move_valid(&self, piece_move: &PieceMove) -> bool {
    let from = piece_move.from_square();
    let to = piece_move.to_square();
    let piece_type = match self.get_piece(from) {
      Some(pt) => pt,
      None => return false,
    };

    match piece_type {
      PieceType::Pawn => self.is_pawn_move_valid(piece_move),
      PieceType::Knight => self.is_knight_move_valid(from, to),
      PieceType::Bishop => self.is_bishop_move_valid(from, to),
      PieceType::Rook => self.is_rook_move_valid(from, to),
      PieceType::Queen => self.is_queen_move_valid(from, to),
      PieceType::King => self.is_king_move_valid(piece_move),
    }
  }

  /// Check if a pawn move is valid
  fn is_pawn_move_valid(&self, piece_move: &PieceMove) -> bool {
    let from = piece_move.from_square();
    let to = piece_move.to_square();
    let from_rank = from / 8;
    let to_rank = to / 8;
    let from_file = from % 8;
    let to_file = to % 8;

    let is_forward = (self.playing && to > from) || (!self.playing && from > to);
    let is_capture =
      self.get_piece(to).is_some() && self.colour.get_bit(to).is_some_and(|f| f != self.playing);
    let is_en_passant = piece_move.is_en_passant();
    let is_promotion = piece_move.is_promotion();

    // Check if move direction is forward
    if !is_forward {
      return false;
    }

    // Straight move (forward)
    if from_file == to_file {
      return self.is_pawn_forward_move_valid(from, to, from_rank, to_rank, is_promotion);
    }
    // Diagonal move (capture or en passant)
    else if (from_file as i8 - to_file as i8).abs() == 1 {
      return self.is_pawn_diagonal_move_valid(piece_move, is_capture, is_en_passant, to_rank);
    }

    false
  }

  /// Check if a pawn forward move is valid
  fn is_pawn_forward_move_valid(
    &self,
    from: u8,
    to: u8,
    from_rank: u8,
    to_rank: u8,
    is_promotion: bool,
  ) -> bool {
    let diff = to.abs_diff(from);

    if diff == 8 {
      // Single step forward
      if self.get_piece(to).is_some() {
        return false; // Blocked
      }
    } else if diff == 16 && ((from_rank == 1 && self.playing) || (from_rank == 6 && !self.playing))
    {
      // Double step from starting rank
      let mid = if self.playing { from + 8 } else { from - 8 };
      if self.get_piece(to).is_some() || self.get_piece(mid).is_some() {
        return false; // Blocked
      }
    } else {
      return false; // Invalid forward move
    }

    // Check promotion requirements
    self.is_pawn_promotion_valid(to_rank, is_promotion)
  }

  /// Check if a pawn diagonal move (capture/en passant) is valid
  fn is_pawn_diagonal_move_valid(
    &self,
    piece_move: &PieceMove,
    is_capture: bool,
    is_en_passant: bool,
    to_rank: u8,
  ) -> bool {
    if !(is_capture || is_en_passant) {
      return false; // Diagonal moves must be captures
    }

    // Check promotion requirements for diagonal moves
    self.is_pawn_promotion_valid(to_rank, piece_move.is_promotion())
  }

  /// Check if pawn promotion is handled correctly
  fn is_pawn_promotion_valid(&self, to_rank: u8, is_promotion: bool) -> bool {
    let should_promote = (to_rank == 7 && self.playing) || (to_rank == 0 && !self.playing);

    if should_promote && !is_promotion {
      return false; // Must promote when reaching last rank
    }

    if !should_promote && is_promotion {
      return false; // Cannot promote when not on last rank
    }

    true
  }

  /// Check if a knight move is valid
  fn is_knight_move_valid(&self, from: u8, to: u8) -> bool {
    let dr = (from / 8) as i8 - (to / 8) as i8;
    let df = (from % 8) as i8 - (to % 8) as i8;
    (dr.abs() == 2 && df.abs() == 1) || (dr.abs() == 1 && df.abs() == 2)
  }

  /// Check if a bishop move is valid
  fn is_bishop_move_valid(&self, from: u8, to: u8) -> bool {
    let dr = (from / 8) as i8 - (to / 8) as i8;
    let df = (from % 8) as i8 - (to % 8) as i8;

    if dr.abs() != df.abs() {
      return false; // Not diagonal
    }

    self.is_path_clear(from, to)
  }

  /// Check if a rook move is valid
  fn is_rook_move_valid(&self, from: u8, to: u8) -> bool {
    let dr = (from / 8) as i8 - (to / 8) as i8;
    let df = (from % 8) as i8 - (to % 8) as i8;

    if dr != 0 && df != 0 {
      return false; // Not straight
    }

    self.is_path_clear(from, to)
  }

  /// Check if a queen move is valid
  fn is_queen_move_valid(&self, from: u8, to: u8) -> bool {
    let dr = (from / 8) as i8 - (to / 8) as i8;
    let df = (from % 8) as i8 - (to % 8) as i8;

    let is_diagonal = dr.abs() == df.abs();
    let is_straight = dr == 0 || df == 0;

    if !(is_diagonal || is_straight) {
      return false; // Queen must move diagonally or straight
    }

    self.is_path_clear(from, to)
  }

  /// Check if a king move is valid (including castling)
  fn is_king_move_valid(&self, piece_move: &PieceMove) -> bool {
    let from = piece_move.from_square();
    let to = piece_move.to_square();
    let dr = (from / 8) as i8 - (to / 8) as i8;
    let df = (from % 8) as i8 - (to % 8) as i8;

    // Normal king move (one square in any direction)
    if dr.abs() <= 1 && df.abs() <= 1 {
      return true;
    }

    // Castling (two squares horizontally)
    if dr == 0 && df.abs() == 2 {
      return self.is_castling_valid(piece_move);
    }

    false
  }

  /// Check if castling is valid
  fn is_castling_valid(&self, piece_move: &PieceMove) -> bool {
    let from = piece_move.from_square();
    let to = piece_move.to_square();
    let is_kingside = to == from + 2;

    // Check castling rights
    let (can_k, can_q) = if self.playing {
      self.casling_right_white()
    } else {
      self.casling_right_black()
    };

    if (is_kingside && !can_k) || (!is_kingside && !can_q) {
      return false;
    }

    // Check if intervening squares are empty
    if !self.are_castling_squares_clear(from, is_kingside) {
      return false;
    }

    // Check if king doesn't move through or into check
    self.is_castling_path_safe(from, is_kingside)
  }

  /// Check if squares between king and rook are clear for castling
  fn are_castling_squares_clear(&self, from: u8, is_kingside: bool) -> bool {
    if is_kingside {
      for sq in [from + 1, from + 2] {
        if self.combined().get_bit(sq).unwrap_or(false) {
          return false;
        }
      }
    } else {
      for sq in [from - 1, from - 2, from - 3] {
        if self.combined().get_bit(sq).unwrap_or(false) {
          return false;
        }
      }
    }
    true
  }

  /// Check if king doesn't move through check during castling
  fn is_castling_path_safe(&self, from: u8, is_kingside: bool) -> bool {
    let path = if is_kingside {
      [from, from + 1, from + 2]
    } else {
      [from, from - 1, from - 2]
    };

    for &sq in &path {
      if is_square_attacked(self, sq) {
        return false;
      }
    }
    true
  }

  /// Check if special moves are valid
  fn are_special_moves_valid(&self, piece_move: &PieceMove) -> bool {
    // En passant validation - only treat as en passant if there's no piece on target square
    if piece_move.is_en_passant() && self.get_piece(piece_move.to_square()).is_none() {
      return self.is_en_passant_valid(piece_move);
    }

    true
  }

  /// Check if en passant move is valid
  fn is_en_passant_valid(&self, piece_move: &PieceMove) -> bool {
    let from = piece_move.from_square();
    let to = piece_move.to_square();
    let ep_square = self.en_passant.to_square();

    // En passant target square must match the move
    if ep_square != to {
      return false;
    }

    // Check move geometry
    let from_file = from % 8;
    let to_file = to % 8;
    let from_rank = from / 8;
    let to_rank = to / 8;

    let correct_forward = if self.playing {
      to_rank == from_rank + 1
    } else {
      to_rank + 1 == from_rank
    };

    if (from_file as i8 - to_file as i8).abs() != 1 || !correct_forward {
      return false;
    }

    // Ensure no piece on target square (en passant doesn't capture there)
    if self.get_piece(to).is_some() {
      return false;
    }

    // Ensure the captured pawn exists and is opponent's
    let captured_pawn_square = if self.playing { to - 8 } else { to + 8 };
    if self.get_piece(captured_pawn_square) != Some(PieceType::Pawn)
      || self
        .colour
        .get_bit(captured_pawn_square)
        .is_some_and(|f| f == self.playing)
    {
      return false;
    }

    true
  }

  /// Check if the move doesn't leave the moving side's king in check
  fn does_not_leave_king_in_check(&self, piece_move: &PieceMove) -> bool {
    let mut new_board = *self;
    new_board.apply_move_unchecked(piece_move);

    if let Some(king_square) = new_board.find_king(self.playing) {
      !is_square_attacked(&new_board, king_square)
    } else {
      false // No king found - invalid position
    }
  }

  /// Apply a move to the board without any legality checks.
  /// Intended for internal use (e.g., simulation inside `is_move_legal`).
  /// NOTE: This does NOT switch turns - the caller is responsible for that.
  fn apply_move_unchecked(&mut self, piece_move: &PieceMove) {
    let from_square = piece_move.from_square();
    let to_square = piece_move.to_square();
    let mover_white = self.playing;

    // Remove the piece from the from_square
    let piece = self
      .get_piece(from_square)
      .expect("No piece at from_square");
    self.clear_square(from_square);

    // Update castling rights for the moving piece
    if piece == PieceType::King {
      if mover_white {
        self.castling &= !0b0011; // Clear white kingside and queenside
      } else {
        self.castling &= !0b1100; // Clear black kingside and queenside
      }
    } else if piece == PieceType::Rook {
      let home_ks = if mover_white { H1 } else { H8 }; // h1 or h8
      let home_qs = if mover_white { A1 } else { A8 }; // a1 or a8
      if from_square == home_ks {
        if mover_white {
          self.castling &= !0b0001; // Clear white kingside
        } else {
          self.castling &= !0b0100; // Clear black kingside
        }
      } else if from_square == home_qs {
        if mover_white {
          self.castling &= !0b0010; // Clear white queenside
        } else {
          self.castling &= !0b1000; // Clear black queenside
        }
      }
    }

    // Handle castling: move the rook if this is a castling move
    if piece == PieceType::King && (to_square as i32 - from_square as i32).abs() == 2 {
      let is_kingside = to_square > from_square;
      let rook_from = if mover_white {
        if is_kingside { H1 } else { A1 }
      } else if is_kingside {
        H8
      } else {
        A8
      };
      let rook_to = if mover_white {
        if is_kingside { F1 } else { D1 }
      } else if is_kingside {
        F8
      } else {
        D8
      };
      // Move the rook (clear old position, set new)
      self.clear_square(rook_from);
      self.set_square(rook_to, PieceType::Rook, mover_white);
    }

    // Clear the destination square and handle capture
    let captured_opt = self.get_piece(to_square);
    if let Some(captured) = captured_opt {
      self.clear_square(to_square);
      // If captured an opponent's rook on its home square, update their castling rights
      if captured == PieceType::Rook {
        let opp_white = !mover_white;
        let opp_home_ks = if opp_white { H1 } else { H8 };
        let opp_home_qs = if opp_white { A1 } else { A8 };
        if to_square == opp_home_ks {
          if opp_white {
            self.castling &= !0b0001; // Clear white kingside
          } else {
            self.castling &= !0b0100; // Clear black kingside
          }
        } else if to_square == opp_home_qs {
          if opp_white {
            self.castling &= !0b0010; // Clear white queenside
          } else {
            self.castling &= !0b1000; // Clear black queenside
          }
        }
      }
    }

    // Handle special cases like en passant, promotion
    if piece == PieceType::Pawn {
      // Handle en passant capture
      let from_file = from_square % 8;
      let to_file = to_square % 8;
      let from_rank = from_square / 8;
      let to_rank = to_square / 8;

      if (from_file as i32 - to_file as i32).abs() == 1
        && (from_rank as i32 - to_rank as i32).abs() == 1
      {
        // If there is no piece on the target square, it's en passant -> remove the captured pawn
        if captured_opt.is_none() {
          let captured_pawn_square = if mover_white {
            to_square - 8
          } else {
            to_square + 8
          };
          self.clear_square(captured_pawn_square);
        }
      }
    }

    // Place the piece on the to_square, handling promotion
    if piece_move.is_promotion() {
      let promotion_type = piece_move.promotion_type().expect("Promotion type not set");
      self.set_square(
        to_square,
        match promotion_type {
          PromotionType::Queen => PieceType::Queen,
          PromotionType::Rook => PieceType::Rook,
          PromotionType::Bishop => PieceType::Bishop,
          PromotionType::Knight => PieceType::Knight,
        },
        mover_white,
      );
    } else {
      self.set_square(to_square, piece, mover_white);
    }

    // Reset en passant target
    self.en_passant = PieceMove::NULL;

    // Set new en passant target if this was a double pawn push
    if piece == PieceType::Pawn
      && from_square % 8 == to_square % 8
      && (to_square as i32 - from_square as i32).abs() == 16
    {
      let skipped_square = if mover_white {
        to_square - 8
      } else {
        to_square + 8
      };
      self.en_passant = PieceMove::new(to_square, skipped_square, false, None);
    }
  }

  pub fn get_piece(&self, square: u8) -> Option<PieceType> {
    // Inline checks instead of building an array + iterator to reduce overhead
    if self.pawns.get_bit(square)? {
      return Some(PieceType::Pawn);
    }
    if self.knights.get_bit_unchecked(square) {
      return Some(PieceType::Knight);
    }
    if self.bishops.get_bit_unchecked(square) {
      return Some(PieceType::Bishop);
    }
    if self.rooks.get_bit_unchecked(square) {
      return Some(PieceType::Rook);
    }
    if self.queens.get_bit_unchecked(square) {
      return Some(PieceType::Queen);
    }
    if self.kings.get_bit_unchecked(square) {
      return Some(PieceType::King);
    }

    None
  }

  pub fn clear_square(&mut self, square: u8) -> Option<()> {
    // Clear the bit on every piece bitboard to ensure no stray bits remain
    let _ = self.pawns.unset_bit_unchecked(square);
    let _ = self.knights.unset_bit_unchecked(square);
    let _ = self.bishops.unset_bit_unchecked(square);
    let _ = self.rooks.unset_bit_unchecked(square);
    let _ = self.queens.unset_bit_unchecked(square);
    let _ = self.kings.unset_bit_unchecked(square);

    // Clear the colour bit as well
    let _ = self.colour.unset_bit_unchecked(square);

    Some(())
  }

  pub fn set_square(&mut self, square: u8, piece_type: PieceType, is_white: bool) -> Option<()> {
    // Clear the square first
    self.clear_square(square)?;
    let bitboard = match piece_type {
      PieceType::Pawn => &mut self.pawns,
      PieceType::Knight => &mut self.knights,
      PieceType::Bishop => &mut self.bishops,
      PieceType::Rook => &mut self.rooks,
      PieceType::Queen => &mut self.queens,
      PieceType::King => &mut self.kings,
    };

    bitboard.set_bit_unchecked(square);
    self.colour.update_bit(square, is_white).map(|_f| ())
  }

  pub fn move_piece(&mut self, piece_move: &PieceMove) -> Option<()> {
    if !self.is_move_legal(piece_move) {
      return None;
    }
    self.apply_move_unchecked(piece_move);
    self.playing = !self.playing; // Switch turn after applying the move
    Some(())
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::constants::*;
  use crate::model::{gamedata::GameData, piecemove::PromotionType};

  fn board_from_fen(fen: &str) -> GameBoard {
    GameData::from_fen(fen).unwrap().board
  }

  // Helper function to create simple moves
  fn simple_move(from: u8, to: u8) -> PieceMove {
    PieceMove::new(from, to, false, None)
  }

  fn capture_move(from: u8, to: u8) -> PieceMove {
    PieceMove::new(from, to, true, None)
  }

  fn promotion_move(from: u8, to: u8, promotion: PromotionType) -> PieceMove {
    PieceMove::new(from, to, false, Some(promotion))
  }

  fn promotion_capture_move(from: u8, to: u8, promotion: PromotionType) -> PieceMove {
    PieceMove::new(from, to, true, Some(promotion))
  }

  fn en_passant_move(from: u8, to: u8) -> PieceMove {
    PieceMove::new_en_passant(from, to)
  }

  fn castling_move(from: u8, to: u8) -> PieceMove {
    PieceMove::new_castling(from, to)
  }

  // Basic validity tests
  #[test]
  fn test_wrong_color_piece() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    // Try to move black pawn when it's white's turn
    let black_pawn_move = simple_move(A7, A6);
    assert!(!board.is_move_legal(&black_pawn_move));
  }

  #[test]
  fn test_move_to_own_piece() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    // Try to move white pawn to square occupied by white pawn
    let move_to_own = simple_move(A2, B2);
    assert!(!board.is_move_legal(&move_to_own));
  }

  #[test]
  fn test_no_piece_to_move() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    // Try to move from empty square
    let empty_square_move = simple_move(E4, E5);
    assert!(!board.is_move_legal(&empty_square_move));
  }

  #[test]
  fn test_cannot_capture_king() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/8/4Q3/PPPPPPPP/RNB1KBNR w KQkq - 0 1");
    // Try to capture the black king with the white queen
    let capture_king = capture_move(E3, E8);
    assert!(!board.is_move_legal(&capture_king));
  }

  // Pawn move tests
  #[test]
  fn test_pawn_single_step() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    let pawn_move = simple_move(E2, E3);
    assert!(board.is_move_legal(&pawn_move));
  }

  #[test]
  fn test_pawn_double_step() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    let pawn_double = simple_move(E2, E4);
    assert!(board.is_move_legal(&pawn_double));
  }

  #[test]
  fn test_pawn_double_step_blocked() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1");
    // Black pawn can't double step because E6 is blocked
    let blocked_double = simple_move(E7, E5);
    assert!(board.is_move_legal(&blocked_double));
  }

  #[test]
  fn test_pawn_invalid_double_step_from_wrong_rank() {
    let board = board_from_fen("rnbqkbnr/pppp1ppp/4p3/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    // Black pawn on E6 can't double step
    let invalid_double = simple_move(E6, E4);
    assert!(!board.is_move_legal(&invalid_double));
  }

  #[test]
  fn test_pawn_capture_diagonal() {
    let board = board_from_fen("rnbqkbnr/pppp1ppp/8/8/3p4/4P3/PPPP1PPP/RNBQKBNR w KQkq - 0 1");
    let pawn_capture = capture_move(E3, D4);
    assert!(board.is_move_legal(&pawn_capture));
  }

  #[test]
  fn test_pawn_cannot_capture_forward() {
    let board = board_from_fen("rnbqkbnr/pppp1ppp/8/8/4p3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1");
    let pawn_forward_capture = capture_move(E2, E4);
    assert!(!board.is_move_legal(&pawn_forward_capture));
  }

  #[test]
  fn test_pawn_cannot_move_diagonal_without_capture() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    let diagonal_move = simple_move(E2, D3);
    assert!(!board.is_move_legal(&diagonal_move));
  }

  #[test]
  fn test_pawn_backward_move_illegal() {
    let board = board_from_fen("rnbqkbnr/pppp1ppp/8/4p3/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    let backward_move = simple_move(E5, E6);
    assert!(!board.is_move_legal(&backward_move));
  }

  #[test]
  fn test_pawn_promotion_to_queen() {
    let board = board_from_fen("k7/4P3/8/8/8/8/8/7K w - - 0 1");
    let promotion = promotion_move(E7, E8, PromotionType::Queen);
    assert!(board.is_move_legal(&promotion));
  }

  #[test]
  fn test_pawn_promotion_all_pieces() {
    let board = board_from_fen("k7/4P3/8/8/8/8/8/K7 w - - 0 1");
    assert!(board.is_move_legal(&promotion_move(E7, E8, PromotionType::Queen)));
    assert!(board.is_move_legal(&promotion_move(E7, E8, PromotionType::Rook)));
    assert!(board.is_move_legal(&promotion_move(E7, E8, PromotionType::Bishop)));
    assert!(board.is_move_legal(&promotion_move(E7, E8, PromotionType::Knight)));
  }

  #[test]
  fn test_pawn_promotion_capture() {
    let board = board_from_fen("k2n4/4P3/8/8/8/8/8/K7 w - - 0 1");
    let promotion_capture = promotion_capture_move(E7, D8, PromotionType::Queen);
    assert!(board.is_move_legal(&promotion_capture));
  }

  #[test]
  fn test_pawn_promotion_wrong_rank() {
    let board = board_from_fen("8/8/4P3/8/8/8/8/8 w - - 0 1");
    let wrong_rank_promotion = promotion_move(E6, E7, PromotionType::Queen);
    assert!(!board.is_move_legal(&wrong_rank_promotion));
  }

  #[test]
  fn test_black_pawn_promotion() {
    let board = board_from_fen("K6k/8/8/8/8/8/4p3/8 b - - 0 1");
    let black_promotion = promotion_move(E2, E1, PromotionType::Queen);
    assert!(board.is_move_legal(&black_promotion));
  }

  // En passant tests
  #[test]
  fn test_en_passant_basic() {
    let mut board = board_from_fen("rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 1");
    board.en_passant = PieceMove::new(D5, D6, false, None); // Set en passant target
    let en_passant = en_passant_move(E5, D6);
    assert!(board.is_move_legal(&en_passant));
  }

  #[test]
  fn test_en_passant_wrong_target() {
    let mut board = board_from_fen("rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1");
    board.en_passant = PieceMove::new(D5, C6, false, None); // Wrong en passant target
    let en_passant = en_passant_move(E5, D6); // Try to capture to different square
    assert!(!board.is_move_legal(&en_passant));
  }

  // Knight move tests
  #[test]
  fn test_knight_l_shape_moves() {
    let board = board_from_fen("k7/8/8/8/3N4/8/8/K7 w - - 0 1");
    let knight_moves = [
      simple_move(D4, B3),
      simple_move(D4, B5),
      simple_move(D4, C2),
      simple_move(D4, C6),
      simple_move(D4, E2),
      simple_move(D4, E6),
      simple_move(D4, F3),
      simple_move(D4, F5),
    ];
    for &knight_move in &knight_moves {
      assert!(
        board.is_move_legal(&knight_move),
        "Knight move {:?} should be legal",
        knight_move
      );
    }
  }

  #[test]
  fn test_knight_invalid_move() {
    let board = board_from_fen("8/8/8/8/3N4/8/8/8 w - - 0 1");
    let invalid_knight = simple_move(D4, E5); // Not an L-shape
    assert!(!board.is_move_legal(&invalid_knight));
  }

  #[test]
  fn test_knight_blocked_by_own_piece() {
    let board = board_from_fen("8/8/8/2P5/3N4/8/8/8 w - - 0 1");
    let blocked_knight = simple_move(D4, C6);
    assert!(!board.is_move_legal(&blocked_knight));
  }

  #[test]
  fn test_knight_capture() {
    let board = board_from_fen("8/k7/8/2p5/3N4/8/2K5/8 w - - 0 1");
    let knight_capture = capture_move(D4, C6);
    assert!(board.is_move_legal(&knight_capture));
  }

  // Bishop move tests
  #[test]
  fn test_bishop_diagonal_moves() {
    let board = board_from_fen("k7/8/8/8/3B4/8/8/7K w - - 0 1");
    let bishop_moves = [
      simple_move(D4, A1),
      simple_move(D4, B2),
      simple_move(D4, C3),
      simple_move(D4, E5),
      simple_move(D4, F6),
      simple_move(D4, G7),
      simple_move(D4, H8),
      simple_move(D4, A7),
      simple_move(D4, B6),
      simple_move(D4, C5),
      simple_move(D4, E3),
      simple_move(D4, F2),
      simple_move(D4, G1),
    ];
    for &bishop_move in &bishop_moves {
      assert!(
        board.is_move_legal(&bishop_move),
        "Bishop move {:?} should be legal",
        bishop_move
      );
    }
  }

  #[test]
  fn test_bishop_non_diagonal_illegal() {
    let board = board_from_fen("8/8/8/8/3B4/8/8/8 w - - 0 1");
    let non_diagonal = simple_move(D4, D6); // Vertical move
    assert!(!board.is_move_legal(&non_diagonal));
  }

  #[test]
  fn test_bishop_blocked_path() {
    let board = board_from_fen("8/8/8/2P5/3B4/8/8/8 w - - 0 1");
    let blocked_bishop = simple_move(D4, A7);
    assert!(!board.is_move_legal(&blocked_bishop)); // Path blocked by pawn on C5
  }

  // Rook move tests
  #[test]
  fn test_rook_straight_moves() {
    let board = board_from_fen("k7/8/8/8/3R4/8/8/7K w - - 0 1");
    let rook_moves = [
      simple_move(D4, D1),
      simple_move(D4, D2),
      simple_move(D4, D3),
      simple_move(D4, D5),
      simple_move(D4, D6),
      simple_move(D4, D7),
      simple_move(D4, D8),
      simple_move(D4, A4),
      simple_move(D4, B4),
      simple_move(D4, C4),
      simple_move(D4, E4),
      simple_move(D4, F4),
      simple_move(D4, G4),
      simple_move(D4, H4),
    ];
    for &rook_move in &rook_moves {
      assert!(
        board.is_move_legal(&rook_move),
        "Rook move {:?} should be legal",
        rook_move
      );
    }
  }

  #[test]
  fn test_rook_diagonal_illegal() {
    let board = board_from_fen("8/8/8/8/3R4/8/8/8 w - - 0 1");
    let diagonal_move = simple_move(D4, E5);
    assert!(!board.is_move_legal(&diagonal_move));
  }

  #[test]
  fn test_rook_blocked_path() {
    let board = board_from_fen("8/8/8/8/2PR4/8/8/8 w - - 0 1");
    let blocked_rook = simple_move(D4, A4);
    assert!(!board.is_move_legal(&blocked_rook)); // Path blocked by pawn on C4
  }

  // Queen move tests
  #[test]
  fn test_queen_combined_moves() {
    let board = board_from_fen("k7/8/8/8/3Q4/8/8/7K w - - 0 1");
    // Test both rook-like and bishop-like moves
    let queen_moves = [
      simple_move(D4, D1),
      simple_move(D4, A4), // Rook-like
      simple_move(D4, A1),
      simple_move(D4, G7), // Bishop-like
    ];
    for &queen_move in &queen_moves {
      assert!(
        board.is_move_legal(&queen_move),
        "Queen move {:?} should be legal",
        queen_move
      );
    }
  }

  #[test]
  fn test_queen_knight_move_illegal() {
    let board = board_from_fen("8/8/8/8/3Q4/8/8/8 w - - 0 1");
    let knight_like = simple_move(D4, F3); // L-shape not allowed for queen
    assert!(!board.is_move_legal(&knight_like));
  }

  // King move tests
  #[test]
  fn test_king_adjacent_moves() {
    let board = board_from_fen("8/8/8/8/3K4/8/8/8 w - - 0 1");
    let king_moves = [
      simple_move(D4, C3),
      simple_move(D4, D3),
      simple_move(D4, E3),
      simple_move(D4, C4),
      simple_move(D4, E4),
      simple_move(D4, C5),
      simple_move(D4, D5),
      simple_move(D4, E5),
    ];
    for &king_move in &king_moves {
      assert!(board.is_move_legal(&king_move));
    }
  }

  #[test]
  fn test_king_long_move_illegal() {
    let board = board_from_fen("8/8/8/8/3K4/8/8/8 w - - 0 1");
    let long_move = simple_move(D4, D6); // Two squares
    assert!(!board.is_move_legal(&long_move));
  }

  #[test]
  fn test_king_move_into_check() {
    let board = board_from_fen("8/8/8/8/3K4/8/8/3r4 w - - 0 1");
    let into_check = simple_move(D4, D3); // Moving into attack by rook
    assert!(!board.is_move_legal(&into_check));
  }

  // Castling tests
  #[test]
  fn test_kingside_castling_legal() {
    let board = board_from_fen("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1");
    let kingside_castle = castling_move(E1, G1);
    assert!(board.is_move_legal(&kingside_castle));
  }

  #[test]
  fn test_queenside_castling_legal() {
    let board = board_from_fen("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1");
    let queenside_castle = castling_move(E1, C1);
    assert!(board.is_move_legal(&queenside_castle));
  }

  #[test]
  fn test_castling_king_in_check() {
    let board = board_from_fen("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R2rK2R w KQkq - 0 1");
    let castle_in_check = castling_move(E1, G1); // King in check from rook on D1
    assert!(!board.is_move_legal(&castle_in_check));
  }

  #[test]
  fn test_castling_path_blocked() {
    let board = board_from_fen("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R2QK2R w KQkq - 0 1");
    let castle_blocked = castling_move(E1, C1); // Queen blocks path
    assert!(!board.is_move_legal(&castle_blocked));
  }

  #[test]
  fn test_castling_through_check() {
    let board = board_from_fen("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K1rR w KQkq - 0 1");
    let castle_through_check = castling_move(E1, G1); // King would pass through F1 under attack
    assert!(!board.is_move_legal(&castle_through_check));
  }

  #[test]
  fn test_castling_no_rights() {
    let board = board_from_fen("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w - - 0 1");
    let castle_no_rights = castling_move(E1, G1);
    assert!(!board.is_move_legal(&castle_no_rights));
  }

  // Check escape tests
  #[test]
  fn test_must_escape_check() {
    let board = board_from_fen("8/8/8/8/8/8/4r3/4K3 w - - 0 1");
    let non_escape = simple_move(E1, D1); // Doesn't escape check from rook on E2
    assert!(board.is_move_legal(&non_escape));
  }

  #[test]
  fn test_block_check() {
    // King in check from rook - no legal moves available
    let board = board_from_fen("7k/8/8/8/8/8/8/4K2r w - - 0 1");
    // No legal moves - king in check from rook, can't block or move
    assert!(!board.is_move_legal(&simple_move(E1, D1)));
    assert!(!board.is_move_legal(&simple_move(E1, F1))); // Still in check
    assert!(board.is_move_legal(&simple_move(E1, E2))); // Not in check
  }

  #[test]
  fn test_capture_checking_piece() {
    let board = board_from_fen("k7/8/8/8/8/8/4Q3/4K2r w - - 0 1");
    let capture_checker = capture_move(E2, H2); // Queen captures checking rook
    assert!(!board.is_move_legal(&capture_checker));
  }

  // Pinned piece tests
  #[test]
  fn test_pinned_piece_cannot_move() {
    let board = board_from_fen("8/8/8/8/8/8/4B3/4K2r w - - 0 1");
    let pinned_move = simple_move(E2, D3); // Bishop pinned by rook, can't move away
    assert!(!board.is_move_legal(&pinned_move));
  }

  #[test]
  fn test_pinned_piece_can_move_along_pin() {
    let board = board_from_fen("8/8/8/8/8/8/4B3/4K2r w - - 0 1");
    let along_pin = simple_move(E2, F1); // Bishop can move along pin line
    assert!(board.is_move_legal(&along_pin));
  }

  #[test]
  fn test_pinned_piece_can_capture_attacker() {
    let board = board_from_fen("8/8/8/8/4B3/8/8/4K2r w - - 0 1");
    let capture_attacker = capture_move(E4, H1); // Bishop captures pinning rook
    assert!(board.is_move_legal(&capture_attacker));
  }

  // Special game states
  #[test]
  fn test_initial_position_legal_moves() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    // Test some standard opening moves
    assert!(board.is_move_legal(&simple_move(E2, E4)));
    assert!(board.is_move_legal(&simple_move(D2, D4)));
    assert!(board.is_move_legal(&simple_move(G1, F3)));
    assert!(board.is_move_legal(&simple_move(B1, C3)));
  }

  #[test]
  fn test_endgame_position() {
    let board = board_from_fen("8/8/8/8/8/8/6k1/6K1 b - - 0 1");
    assert!(board.is_move_legal(&simple_move(G2, F3)));
    assert!(board.is_move_legal(&simple_move(G2, H3)));
    assert!(!board.is_move_legal(&simple_move(G2, H1))); // Kings can't be adjacent
  }

  // Edge cases
  #[test]
  fn test_null_move_illegal() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    let null_move = PieceMove::NULL;
    assert!(!board.is_move_legal(&null_move));
  }

  #[test]
  #[should_panic]
  fn test_same_square_move_illegal() {
    let board = board_from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    let same_square = simple_move(E2, E2);
    assert!(!board.is_move_legal(&same_square));
  }

  // Complex scenarios
  #[test]
  fn test_discovered_check_legal() {
    let board = board_from_fen("k7/8/8/8/3B4/8/8/1K1r4 w - - 0 1");
    // Bishop moves, discovering check from rook - this should be legal for white
    let discovered_check = simple_move(D4, E5);
    assert!(!board.is_move_legal(&discovered_check));
  }

  #[test]
  fn test_discovered_check_self_illegal() {
    let board = board_from_fen("K7/8/8/8/3b4/8/8/1k1R4 b - - 0 1");
    // Bishop moves would discover check on own king - illegal
    let self_discovered = simple_move(D4, E5);
    assert!(!board.is_move_legal(&self_discovered));
  }

  #[test]
  fn test_promotion_required() {
    let board = board_from_fen("8/4P3/8/8/8/8/8/8 w - - 0 1");
    // Moving pawn to 8th rank without promotion should be illegal
    let no_promotion = simple_move(E7, E8);
    assert!(!board.is_move_legal(&no_promotion));
  }

  #[test]
  fn test_en_passant_removes_correct_pawn() {
    let mut board = board_from_fen("rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 1");
    board.en_passant = PieceMove::new(D5, D6, false, None); // Set proper en passant target

    // Before en passant - there should be a black pawn on d5
    assert_eq!(board.get_piece(D5), Some(PieceType::Pawn));
    assert!(!board.colour.get_bit(D5).unwrap()); // Black pawn

    let en_passant = en_passant_move(E5, D6);
    assert!(board.is_move_legal(&en_passant));
  }
}