eschac 0.1.0

computing chess moves
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
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
//! **Move generation.**

use crate::array_vec::*;
use crate::bitboard::*;
use crate::board::*;
use crate::lookup::*;
use crate::san::*;
use crate::setup::*;
use crate::uci::*;

use std::iter::ExactSizeIterator;
use std::iter::FusedIterator;

/// **A chess position.**
///
/// ## Game's state
///
/// This type records the following information:
/// - the position of pieces on the board
/// - the color to play
/// - the available castling rights
/// - the optional en passant target square (even if taking is not possible)
///
/// ## Validity & Legality
///
/// This type can only represent "valid" chess positions. Valid positions include but are not
/// limited to legal chess positions (i.e. positions that can be reached from a sequence of legal
/// moves starting on the initial position). It is neither computably feasible nor desirable to
/// reject all illegal positions. eschac will only reject illegal positions when chess rules can't
/// be applied unambiguously or when doing so enables some code optimisation. See
/// [`IllegalPositionReason`] for details about rejected positions.
///
/// ## Move generation & play
///
/// The [`legal_moves`](Position::legal_moves) method generates the list of all legal moves on the
/// position. [`UciMove::to_move`] and [`San::to_move`] can also be used to convert chess notation
/// to playable moves.
///
/// Playing a move is done through a copy-make interface. [`legal_moves`](Position::legal_moves)
/// returns a sequence of [`Move`] objects. Moves hold a reference to the position from where they
/// were computed. They can be played without further checks and without potential panics using
/// [`Move::make`].
///
/// ## En passant & Equality
///
/// The en passant target square is set when the [`Position`] is obtained after playing a double
/// pawn advance, even when there is no pawn to take or when taking is not legal. In that case,
/// [`remove_en_passant_target_square`](Position::remove_en_passant_target_square) can be used to
/// remove the target square from the record. As a consequence, [`Eq`] is not defined in accordance
/// with the FIDE laws of chess.
///
/// ## Ordering
//
/// The order on [`Position`] is defined only for use in data structures. Hence its only
/// requirement is to be efficient while respecting the [`Ord`] trait protocol. It should not be
/// considered stable.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Position {
    setup: Setup,
    lookup: InitialisedLookup,
}

const MAX_LEGAL_MOVES: usize = 218;

impl std::fmt::Debug for Position {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        f.debug_tuple("Position")
            .field(&self.as_setup().to_string())
            .finish()
    }
}

impl Position {
    /// Returns the initial position of a chess game.
    ///
    /// i.e. `rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -`
    #[inline]
    pub fn new() -> Self {
        Self {
            setup: Setup {
                w: Bitboard(0x000000000000FFFF),
                p_b_q: Bitboard(0x2CFF00000000FF2C),
                n_b_k: Bitboard(0x7600000000000076),
                r_q_k: Bitboard(0x9900000000000099),
                turn: Color::White,
                castling_rights: CastlingRights::full(),
                en_passant: OptionSquare::None,
            },
            lookup: InitialisedLookup::init(),
        }
    }

    /// Tries to read a valid position from a text record.
    ///
    /// This is a shortcut for parsing and validating a [`Setup`]:
    /// ```
    /// # use eschac::setup::Setup;
    /// # |s: &str| -> Option<eschac::position::Position> {
    /// s.parse::<Setup>().ok().and_then(|pos| pos.validate().ok())
    /// # };
    /// ```
    #[inline]
    pub fn from_text_record(s: &str) -> Option<Self> {
        s.parse::<Setup>().ok().and_then(|pos| pos.validate().ok())
    }

    /// Returns all the legal moves on the position.
    #[inline]
    pub fn legal_moves<'l>(&'l self) -> Moves<'l> {
        fn aux(position: &Position, visitor: &mut Moves) {
            position.generate_moves(visitor);
        }
        let mut visitor = Moves {
            position: self,
            is_check: false,
            en_passant_is_legal: false,
            array: ArrayVec::new(),
        };
        aux(self, &mut visitor);
        visitor
    }

    /// Counts the legal moves on the position.
    ///
    /// This is equivalent but faster than:
    /// ```
    /// # use eschac::position::Position;
    /// # |position: &Position| -> usize {
    /// position.legal_moves().len()
    /// # };
    /// ```
    #[inline]
    pub fn count_legal_moves(&self) -> usize {
        struct VisitorImpl {
            len: usize,
        }
        impl VisitorImpl {
            fn new() -> Self {
                Self { len: 0 }
            }
        }
        impl Visitor for VisitorImpl {
            #[inline]
            fn is_check(&mut self) {}
            #[inline]
            fn en_passant_is_legal(&mut self) {}
            #[inline]
            fn moves<I>(&mut self, iter: I)
            where
                I: Iterator<Item = RawMove> + ExactSizeIterator,
            {
                self.len += iter.len();
            }
        }
        fn aux(position: &Position, visitor: &mut VisitorImpl) {
            position.generate_moves(visitor);
        }
        let mut visitor = VisitorImpl::new();
        aux(self, &mut visitor);
        visitor.len
    }

    /// Discards the optional en passant target square.
    ///
    /// This function is useful to check for position equality, notably when implementing FIDE's
    /// draw by repetition rules. Note that this function will remove the en passant target square
    /// even if taking en passant is legal. If this is not desirable, it is the caller's
    /// responsibility to rule out the legality of en passant before calling this function.
    #[inline]
    pub fn remove_en_passant_target_square(&mut self) {
        self.setup.en_passant = OptionSquare::None;
    }

    /// Returns the occupancy of a square.
    #[inline]
    pub fn get(&self, square: Square) -> Option<Piece> {
        self.setup.get(square)
    }

    /// Returns the color whose turn it is to play.
    #[inline]
    pub fn turn(&self) -> Color {
        self.setup.turn()
    }

    /// Returns `true` if castling is available for the given color and side.
    #[inline]
    pub fn castling_rights(&self, color: Color, side: CastlingSide) -> bool {
        self.setup.castling_rights(color, side)
    }

    /// Returns the en passant target square if it exists.
    ///
    /// Note that if an en passant target square exists, it does not mean that taking en passant is
    /// legal or even pseudo-legal.
    #[inline]
    pub fn en_passant_target_square(&self) -> Option<Square> {
        self.setup.en_passant_target_square()
    }

    /// Discards the castling rights for the given color and side.
    #[inline]
    pub fn remove_castling_rights(&mut self, color: Color, side: CastlingSide) {
        self.setup.set_castling_rights(color, side, false);
    }

    /// Borrows the position as a [`Setup`].
    #[inline]
    pub fn as_setup(&self) -> &Setup {
        &self.setup
    }

    /// Converts a position to a [`Setup`], allowing to edit the position without enforcing its
    /// legality.
    #[inline]
    pub fn into_setup(self) -> Setup {
        self.setup
    }

    /// Tries to pass the turn to the other color, failing if it would leave the king in check.
    ///
    /// When possible, this inverts the color to play and removes the en passant square if it
    /// exists.
    pub fn pass(&self) -> Option<Self> {
        let d = self.lookup;
        let setup = &self.setup;
        let blockers = setup.p_b_q | setup.n_b_k | setup.r_q_k;
        let k = setup.n_b_k & setup.r_q_k;
        let q = setup.p_b_q & setup.r_q_k;
        let b = setup.p_b_q & setup.n_b_k;
        let n = setup.n_b_k ^ b ^ k;
        let r = setup.r_q_k ^ q ^ k;
        let p = setup.p_b_q ^ b ^ q;
        let (us, them) = match setup.turn {
            Color::White => (setup.w, blockers ^ setup.w),
            Color::Black => (blockers ^ setup.w, setup.w),
        };
        let king_square = (us & k).next().unwrap();
        let checkers = them
            & (d.pawn_attack(setup.turn, king_square) & p
                | d.knight(king_square) & n
                | d.bishop(king_square, blockers) & (q | b)
                | d.rook(king_square, blockers) & (q | r));
        checkers.is_empty().then(|| Self {
            setup: Setup {
                turn: !setup.turn,
                en_passant: OptionSquare::None,
                ..setup.clone()
            },
            lookup: d,
        })
    }

    /// Returns the mirror image of the position (see [`Setup::mirror`]).
    #[inline]
    pub fn mirror(&self) -> Self {
        Self {
            setup: self.setup.mirror(),
            lookup: self.lookup,
        }
    }

    /// Returns the number of possible chess games for a given number of moves.
    ///
    /// This function is intended for benchmarking and is written as a simple recursion without any
    /// caching.
    pub fn perft(&self, depth: usize) -> u128 {
        fn aux(position: &Position, depth: usize) -> u128 {
            match depth.checked_sub(1) {
                None => position.count_legal_moves() as u128,
                Some(depth) => position
                    .legal_moves()
                    .into_iter()
                    .map(|m| aux(&m.make(), depth))
                    .sum(),
            }
        }
        match depth.checked_sub(1) {
            None => 1,
            Some(depth) => aux(self, depth),
        }
    }

    pub(crate) fn move_from_uci<'l>(&'l self, uci: UciMove) -> Result<Move<'l>, InvalidUciMove> {
        struct VisitorImpl<const ROLE: u8> {
            role: Role,
            from: Bitboard,
            to: Bitboard,
            found: Option<RawMove>,
        }
        impl<const ROLE: u8> VisitorImpl<ROLE> {
            #[inline]
            fn new(role: Role, from: Bitboard, to: Bitboard) -> Self {
                Self {
                    role,
                    from,
                    to,
                    found: None,
                }
            }
        }
        impl<const ROLE: u8> Visitor for VisitorImpl<ROLE> {
            #[inline]
            fn roles(&self, role: Role) -> bool {
                role as u8 == ROLE
            }
            #[inline]
            fn from(&self) -> Bitboard {
                self.from
            }
            #[inline]
            fn to(&self) -> Bitboard {
                self.to
            }
            #[inline]
            fn is_check(&mut self) {}
            #[inline]
            fn en_passant_is_legal(&mut self) {}
            #[inline]
            fn moves<I>(&mut self, iter: I)
            where
                I: Iterator<Item = RawMove> + ExactSizeIterator,
            {
                iter.for_each(|raw| {
                    debug_assert!(raw.role() as u8 == ROLE);
                    debug_assert!(self.from.contains(raw.from()));
                    debug_assert!(self.to.contains(raw.to()));
                    if raw.role == self.role {
                        debug_assert!(self.found.is_none());
                        self.found = Some(raw);
                    }
                });
            }
        }
        let UciMove {
            from,
            to,
            promotion,
        } = uci;
        let role = self.setup.get_role(from).ok_or(InvalidUciMove::Illegal)?;
        #[inline]
        fn aux<'l, const ROLE: u8>(
            position: &'l Position,
            role: Role,
            from: Square,
            to: Square,
        ) -> Result<Move<'l>, InvalidUciMove> {
            let mut visitor = VisitorImpl::<ROLE>::new(role, from.bitboard(), to.bitboard());
            position.generate_moves(&mut visitor);
            let raw = visitor.found.ok_or(InvalidUciMove::Illegal)?;
            Ok(Move { position, raw })
        }
        let promotion = if role == Role::Pawn {
            promotion.unwrap_or(Role::Pawn)
        } else if promotion.is_some() {
            return Err(InvalidUciMove::Illegal);
        } else {
            role
        };
        match role {
            Role::Pawn => aux::<1>(self, promotion, from, to),
            Role::Knight => aux::<2>(self, promotion, from, to),
            Role::Bishop => aux::<3>(self, promotion, from, to),
            Role::Rook => aux::<4>(self, promotion, from, to),
            Role::Queen => aux::<5>(self, promotion, from, to),
            Role::King => aux::<6>(self, promotion, from, to),
        }
    }

    pub(crate) fn move_from_san<'l>(&'l self, san: &San) -> Result<Move<'l>, InvalidSan> {
        struct VisitorImpl<const ROLE: u8> {
            role: Role,
            from: Bitboard,
            to: Bitboard,
            found: Option<RawMove>,
            found_other: bool,
        }
        impl<const ROLE: u8> VisitorImpl<ROLE> {
            #[inline]
            fn new(role: Role, from: Bitboard, to: Bitboard) -> Self {
                Self {
                    role,
                    from,
                    to,
                    found: None,
                    found_other: false,
                }
            }
        }
        impl<const ROLE: u8> Visitor for VisitorImpl<ROLE> {
            #[inline]
            fn roles(&self, role: Role) -> bool {
                role as u8 == ROLE
            }
            #[inline]
            fn from(&self) -> Bitboard {
                self.from
            }
            #[inline]
            fn to(&self) -> Bitboard {
                self.to
            }
            #[inline]
            fn is_check(&mut self) {}
            #[inline]
            fn en_passant_is_legal(&mut self) {}
            #[inline]
            fn moves<I>(&mut self, iter: I)
            where
                I: Iterator<Item = RawMove> + ExactSizeIterator,
            {
                iter.for_each(|raw| {
                    debug_assert!(raw.role() as u8 == ROLE);
                    debug_assert!(self.from.contains(raw.from()));
                    debug_assert!(self.to.contains(raw.to()));
                    if raw.role == self.role {
                        match self.found {
                            Some(_) => self.found_other = true,
                            None => self.found = Some(raw),
                        }
                    }
                });
            }
        }
        let (role, promotion, from, to) = match san.inner {
            SanInner::Castle(CastlingSide::Short) => (
                Role::King,
                Role::King,
                Square::new(File::E, self.turn().home_rank()).bitboard(),
                Square::new(File::G, self.turn().home_rank()).bitboard(),
            ),
            SanInner::Castle(CastlingSide::Long) => (
                Role::King,
                Role::King,
                Square::new(File::E, self.turn().home_rank()).bitboard(),
                Square::new(File::C, self.turn().home_rank()).bitboard(),
            ),
            SanInner::Normal {
                role,
                target,
                file,
                rank,
                promotion,
                ..
            } => (
                role,
                if role == Role::Pawn {
                    promotion.unwrap_or(Role::Pawn)
                } else if promotion.is_some() {
                    return Err(InvalidSan::Illegal);
                } else {
                    role
                },
                file.map_or(!Bitboard::new(), |file| file.bitboard())
                    & rank.map_or(!Bitboard::new(), |rank| rank.bitboard()),
                target.bitboard(),
            ),
        };
        #[inline]
        fn aux<'l, const ROLE: u8>(
            position: &'l Position,
            role: Role,
            from: Bitboard,
            to: Bitboard,
        ) -> Result<Move<'l>, InvalidSan> {
            let mut visitor = VisitorImpl::<ROLE>::new(role, from, to);
            position.generate_moves(&mut visitor);
            match visitor.found {
                None => Err(InvalidSan::Illegal),
                Some(raw) => match visitor.found_other {
                    true => Err(InvalidSan::Ambiguous),
                    false => Ok(Move { position, raw }),
                },
            }
        }
        match role {
            Role::Pawn => aux::<1>(self, promotion, from, to),
            Role::Knight => aux::<2>(self, promotion, from, to),
            Role::Bishop => aux::<3>(self, promotion, from, to),
            Role::Rook => aux::<4>(self, promotion, from, to),
            Role::Queen => aux::<5>(self, promotion, from, to),
            Role::King => aux::<6>(self, promotion, from, to),
        }
    }
}

/// A legal move.
#[derive(Clone, Copy)]
pub struct Move<'l> {
    position: &'l Position,
    raw: RawMove,
}

impl<'l> Move<'l> {
    /// Returns the position after playing the move.
    ///
    /// This sets the en passant square after a double pawn advance, even when there is no pawn to
    /// capture or when capturing is not legal.
    pub fn make(self) -> Position {
        let mut position = self.position.clone();
        unsafe { position.play_unchecked(self.raw) };
        position
    }

    /// Returns the position the move is played on.
    #[inline]
    pub fn position(self) -> &'l Position {
        self.position
    }

    /// Returns the type of piece that moves.
    #[inline]
    pub fn role(self) -> Role {
        self.raw.role()
    }

    /// Returns the origin square of the move.
    #[inline]
    pub fn from(self) -> Square {
        self.raw.from()
    }

    /// Returns the target square of the move.
    #[inline]
    pub fn to(self) -> Square {
        self.raw.to()
    }

    /// Returns the type of piece that the pawn is promoted to, if the move is a promotion.
    #[inline]
    pub fn promotion(self) -> Option<Role> {
        self.raw.promotion()
    }

    /// Returns `true` if the move is a capture.
    #[inline]
    pub fn is_capture(self) -> bool {
        self.raw.kind == MoveType::EnPassant
            || !((self.position.setup.p_b_q
                | self.position.setup.n_b_k
                | self.position.setup.r_q_k)
                & self.to().bitboard())
            .is_empty()
    }

    /// Returns the type of piece that is captured, if the move is a capture.
    #[inline]
    pub fn captured(self) -> Option<Role> {
        match self.raw.kind {
            MoveType::EnPassant => Some(Role::Pawn),
            _ => self.position.setup.get_role(self.raw.to),
        }
    }

    /// Returns the UCI notation of the move.
    #[inline]
    pub fn to_uci(self) -> UciMove {
        self.raw.uci()
    }

    /// Returns the standard algebraic notation of the move.
    pub fn to_san(self) -> San {
        struct VisitorImpl<const ROLE: u8> {
            to: Bitboard,
            candidates: Bitboard,
        }
        impl<const ROLE: u8> VisitorImpl<ROLE> {
            #[inline]
            fn new(to: Square) -> Self {
                Self {
                    to: to.bitboard(),
                    candidates: Bitboard::new(),
                }
            }
        }
        impl<const ROLE: u8> Visitor for VisitorImpl<ROLE> {
            #[inline]
            fn roles(&self, role: Role) -> bool {
                role as u8 == ROLE
            }
            #[inline]
            fn to(&self) -> Bitboard {
                self.to
            }
            #[inline]
            fn is_check(&mut self) {}
            #[inline]
            fn en_passant_is_legal(&mut self) {}
            #[inline]
            fn moves<I>(&mut self, iter: I)
            where
                I: Iterator<Item = RawMove> + ExactSizeIterator,
            {
                iter.for_each(|raw| {
                    debug_assert!(raw.role() as u8 == ROLE);
                    debug_assert!(self.to.contains(raw.to()));
                    self.candidates.insert(raw.from);
                });
            }
        }
        San {
            inner: match self.raw.kind {
                MoveType::CastleShort => SanInner::Castle(CastlingSide::Short),
                MoveType::CastleLong => SanInner::Castle(CastlingSide::Long),
                MoveType::PawnAdvance
                | MoveType::PawnAdvancePromotion
                | MoveType::PawnDoubleAdvance => SanInner::Normal {
                    role: Role::Pawn,
                    file: None,
                    rank: None,
                    capture: false,
                    target: self.to(),
                    promotion: self.promotion(),
                },
                MoveType::PawnAttack | MoveType::PawnAttackPromotion | MoveType::EnPassant => {
                    SanInner::Normal {
                        role: Role::Pawn,
                        file: Some(self.from().file()),
                        rank: None,
                        capture: true,
                        target: self.to(),
                        promotion: self.promotion(),
                    }
                }
                _ => {
                    fn aux<const ROLE: u8>(m: &Move) -> SanInner {
                        let mut visitor = VisitorImpl::<ROLE>::new(m.to());
                        m.position().generate_moves(&mut visitor);
                        let candidates = visitor.candidates;
                        let (file, rank) = if candidates == m.from().bitboard() {
                            (None, None)
                        } else if candidates & m.from().file().bitboard() == m.from().bitboard() {
                            (Some(m.from().file()), None)
                        } else if candidates & m.from().rank().bitboard() == m.from().bitboard() {
                            (None, Some(m.from().rank()))
                        } else {
                            (Some(m.from().file()), Some(m.from().rank()))
                        };
                        SanInner::Normal {
                            role: m.role(),
                            file,
                            rank,
                            capture: m.is_capture(),
                            target: m.to(),
                            promotion: None,
                        }
                    }
                    match self.role() {
                        Role::Pawn => aux::<1>(&self),
                        Role::Knight => aux::<2>(&self),
                        Role::Bishop => aux::<3>(&self),
                        Role::Rook => aux::<4>(&self),
                        Role::Queen => aux::<5>(&self),
                        Role::King => aux::<6>(&self),
                    }
                }
            },
            suffix: {
                let pos = self.make();
                let mut visitor = MateCollector::new();
                pos.generate_moves(&mut visitor);
                visitor.is_check.then(|| match visitor.is_mate {
                    true => SanSuffix::Checkmate,
                    false => SanSuffix::Check,
                })
            },
        }
    }
}

/// A list of legal moves on the same position.
///
/// It can be obtained using the [`Position::legal_moves`] method. This type is an iterator over
/// [`Move`] objects.
pub struct Moves<'l> {
    position: &'l Position,
    is_check: bool,
    en_passant_is_legal: bool,
    array: ArrayVec<RawMove, MAX_LEGAL_MOVES>,
}

impl<'l> Moves<'l> {
    /// Returns the position on which the moves are played.
    #[inline]
    pub fn position(&self) -> &Position {
        self.position
    }

    /// Iterates over the moves.
    #[inline]
    pub fn iter(&'l self) -> MovesIter<'l> {
        MovesIter {
            position: self.position,
            iter: (&self.array).into_iter(),
        }
    }

    /// Returns the number of moves in the list.
    #[inline]
    pub fn len(&self) -> usize {
        self.array.len()
    }

    /// Returns `true` if en passant is legal.
    #[inline]
    pub fn en_passant_is_legal(&self) -> bool {
        self.en_passant_is_legal
    }

    /// Returns `true` if the king is in check.
    #[inline]
    pub fn is_check(&self) -> bool {
        self.is_check
    }

    /// Returns the move at the given index, if it exists.
    #[inline]
    pub fn get(&self, index: usize) -> Option<Move<'l>> {
        self.array.get(index).copied().map(|raw| Move {
            position: self.position,
            raw,
        })
    }

    /// Sorts the moves in the list.
    ///
    /// See [`slice::sort_unstable_by`] for potential panics.
    #[inline]
    pub fn sort_by<F>(&mut self, mut compare: F)
    where
        F: FnMut(Move, Move) -> std::cmp::Ordering,
    {
        self.array.as_slice_mut().sort_unstable_by(|a, b| {
            compare(
                Move {
                    position: self.position,
                    raw: *a,
                },
                Move {
                    position: self.position,
                    raw: *b,
                },
            )
        });
    }
}

/// An iterator over legal moves.
pub struct MovesIter<'l> {
    position: &'l Position,
    iter: ArrayVecIter<'l, RawMove, MAX_LEGAL_MOVES>,
}
impl<'l> Iterator for MovesIter<'l> {
    type Item = Move<'l>;
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|raw| Move {
            position: self.position,
            raw,
        })
    }
}
impl<'l> FusedIterator for MovesIter<'l> {}
impl<'l> ExactSizeIterator for MovesIter<'l> {
    #[inline]
    fn len(&self) -> usize {
        self.iter.len()
    }
}
impl<'l> IntoIterator for &'l Moves<'l> {
    type Item = Move<'l>;
    type IntoIter = MovesIter<'l>;
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// An iterator over legal moves.
pub struct MovesIntoIter<'l> {
    position: &'l Position,
    iter: ArrayVecIntoIter<RawMove, MAX_LEGAL_MOVES>,
}
impl<'l> Iterator for MovesIntoIter<'l> {
    type Item = Move<'l>;
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|raw| Move {
            position: self.position,
            raw,
        })
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}
impl<'l> FusedIterator for MovesIntoIter<'l> {}
impl<'l> ExactSizeIterator for MovesIntoIter<'l> {
    #[inline]
    fn len(&self) -> usize {
        self.iter.len()
    }
}
impl<'l> IntoIterator for Moves<'l> {
    type Item = Move<'l>;
    type IntoIter = MovesIntoIter<'l>;
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        MovesIntoIter {
            position: self.position,
            iter: self.array.into_iter(),
        }
    }
}

#[derive(Clone, Copy)]
pub(crate) struct RawMove {
    kind: MoveType,
    role: Role,
    from: Square,
    to: Square,
}

#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
enum MoveType {
    CastleShort,
    CastleLong,
    KingMove,
    PieceMove,
    PawnAdvance,
    PawnAttack,
    PawnAdvancePromotion,
    PawnAttackPromotion,
    PawnDoubleAdvance,
    EnPassant,
}

impl RawMove {
    #[inline]
    fn from(&self) -> Square {
        self.from
    }
    #[inline]
    fn to(&self) -> Square {
        self.to
    }
    #[inline]
    fn role(&self) -> Role {
        match self.kind {
            MoveType::CastleShort | MoveType::CastleLong | MoveType::KingMove => Role::King,
            MoveType::PieceMove => self.role,
            MoveType::PawnAdvance
            | MoveType::PawnAttack
            | MoveType::PawnAdvancePromotion
            | MoveType::PawnAttackPromotion
            | MoveType::PawnDoubleAdvance
            | MoveType::EnPassant => Role::Pawn,
        }
    }
    #[inline]
    fn promotion(&self) -> Option<Role> {
        match self.kind {
            MoveType::PawnAdvancePromotion | MoveType::PawnAttackPromotion => Some(self.role),
            _ => None,
        }
    }
    #[inline]
    fn uci(&self) -> UciMove {
        UciMove {
            from: self.from(),
            to: self.to(),
            promotion: self.promotion(),
        }
    }
}

trait Visitor {
    #[inline]
    fn roles(&self, _role: Role) -> bool {
        true
    }
    #[inline]
    fn from(&self) -> Bitboard {
        !Bitboard::new()
    }
    #[inline]
    fn to(&self) -> Bitboard {
        !Bitboard::new()
    }

    fn is_check(&mut self);
    fn en_passant_is_legal(&mut self);
    fn moves<I>(&mut self, iter: I)
    where
        I: Iterator<Item = RawMove> + ExactSizeIterator;
}

impl<'l> Visitor for Moves<'l> {
    #[inline]
    fn is_check(&mut self) {
        self.is_check = true;
    }
    #[inline]
    fn en_passant_is_legal(&mut self) {
        self.en_passant_is_legal = true;
    }
    #[inline]
    fn moves<I>(&mut self, iter: I)
    where
        I: Iterator<Item = RawMove> + ExactSizeIterator,
    {
        iter.for_each(|raw| unsafe { self.array.push_unchecked(raw) });
    }
}

impl Position {
    /// SAFETY: The position must be valid.
    pub(crate) unsafe fn from_setup(setup: Setup) -> Self {
        Self {
            setup,
            lookup: InitialisedLookup::init(),
        }
    }

    fn generate_moves<T>(&self, visitor: &mut T)
    where
        T: Visitor,
    {
        let global_mask_from = visitor.from();
        let global_mask_to = visitor.to();

        let Setup {
            w,
            p_b_q,
            n_b_k,
            r_q_k,
            turn,
            en_passant,
            castling_rights,
        } = self.setup;
        let d = self.lookup;

        let blockers = p_b_q | n_b_k | r_q_k;
        let (us, them) = match turn {
            Color::White => (w, blockers ^ w),
            Color::Black => (blockers ^ w, w),
        };

        let k = n_b_k & r_q_k;
        let q = p_b_q & r_q_k;
        let b = p_b_q & n_b_k;
        let n = n_b_k ^ b ^ k;
        let r = r_q_k ^ q ^ k;
        let p = p_b_q ^ b ^ q;
        let ours = ByRole([p & us, n & us, b & us, r & us, q & us, k & us]);
        let theirs = ByRole([
            p & them,
            n & them,
            (q | b) & them,
            (q | r) & them,
            q & them,
            k & them,
        ]);
        let king_square = unsafe {
            // SAFETY: the position is legal
            ours.king().first().unwrap_unchecked()
        };

        let forward = turn.forward();

        let x = d.bishop(king_square, blockers);
        let y = d.rook(king_square, blockers);
        let checkers = d.pawn_attack(turn, king_square) & theirs.pawn()
            | d.knight(king_square) & theirs.knight()
            | x & theirs.bishop()
            | y & theirs.rook();
        let blockers_x_ray = blockers & !(x | y);
        let pinned = (d.bishop(king_square, blockers_x_ray) & theirs.bishop()
            | d.rook(king_square, blockers_x_ray) & theirs.rook())
        .map(|sq| d.segment(king_square, sq))
        .reduce_or();

        if visitor.roles(Role::King) && global_mask_from.contains(king_square) {
            let attacked = {
                let blockers = blockers ^ ours.king();
                theirs
                    .king()
                    .map(|sq| d.king(sq))
                    .chain(theirs.bishop().map(|sq| d.bishop(sq, blockers)))
                    .chain(theirs.rook().map(|sq| d.rook(sq, blockers)))
                    .chain(theirs.knight().map(|sq| d.knight(sq)))
                    .chain(std::iter::once(
                        theirs.pawn().trans(!forward).trans(Direction::East),
                    ))
                    .chain(std::iter::once(
                        theirs.pawn().trans(!forward).trans(Direction::West),
                    ))
                    .reduce_or()
            };
            // king moves
            visitor.moves(
                (global_mask_to & d.king(king_square) & !us & !attacked).map(|to| RawMove {
                    kind: MoveType::KingMove,
                    from: king_square,
                    to,
                    role: Role::King,
                }),
            );
            // castling
            if castling_rights.get(turn, CastlingSide::Short) {
                let (x, y) = match turn {
                    Color::White => (Bitboard(0x0000000000000070), Bitboard(0x0000000000000060)),
                    Color::Black => (Bitboard(0x7000000000000000), Bitboard(0x6000000000000000)),
                };
                if (attacked & x | blockers & y).is_empty() {
                    let from = king_square;
                    let to = unsafe {
                        from.trans_unchecked(Direction::East)
                            .trans_unchecked(Direction::East)
                    };
                    if global_mask_to.contains(to) {
                        visitor.moves(std::iter::once(RawMove {
                            kind: MoveType::CastleShort,
                            from,
                            to,
                            role: Role::King,
                        }))
                    }
                }
            }
            if castling_rights.get(turn, CastlingSide::Long) {
                let (x, y) = match turn {
                    Color::White => (Bitboard(0x000000000000001C), Bitboard(0x000000000000000E)),
                    Color::Black => (Bitboard(0x1C00000000000000), Bitboard(0x0E00000000000000)),
                };
                if (attacked & x | blockers & y).is_empty() {
                    let from = king_square;
                    let to = unsafe {
                        from.trans_unchecked(Direction::West)
                            .trans_unchecked(Direction::West)
                    };
                    if global_mask_to.contains(to) {
                        visitor.moves(std::iter::once(RawMove {
                            kind: MoveType::CastleLong,
                            from,
                            to,
                            role: Role::King,
                        }))
                    }
                }
            }
        }

        if checkers.len() > 1 {
            visitor.is_check();
            return;
        }

        let checker = checkers.first();
        let block_check = checker
            .map(|checker| d.segment(king_square, checker))
            .unwrap_or(Bitboard(!0));
        let target_mask = global_mask_to & block_check;

        // pawns
        if visitor.roles(Role::Pawn) {
            let kside = match turn {
                Color::White => Direction::NorthEast,
                Color::Black => Direction::SouthEast,
            };
            let qside = match turn {
                Color::White => Direction::NorthWest,
                Color::Black => Direction::SouthWest,
            };
            let third_rank = match turn {
                Color::White => Rank::Third,
                Color::Black => Rank::Sixth,
            };
            let adv = (global_mask_from & ours.pawn() & (!pinned | king_square.file().bitboard()))
                .trans(forward)
                & !blockers;
            let promotion = turn.promotion_rank().bitboard();
            // pawn advances
            {
                let targets = adv & target_mask;
                visitor.moves((targets & !promotion).map(|to| RawMove {
                    kind: MoveType::PawnAdvance,
                    from: unsafe { to.trans_unchecked(!forward) },
                    to,
                    role: Role::Pawn,
                }));
                visitor.moves(WithPromotion::new((targets & promotion).map(|to| {
                    RawMove {
                        kind: MoveType::PawnAdvancePromotion,
                        from: unsafe { to.trans_unchecked(!forward) },
                        to,
                        role: Role::Pawn,
                    }
                })));
            }
            // pawn attacks kingside
            {
                let targets =
                    (global_mask_from & ours.pawn() & (!pinned | d.ray(king_square, kside)))
                        .trans(kside)
                        & them
                        & target_mask;
                visitor.moves((targets & !promotion).map(|to| RawMove {
                    kind: MoveType::PawnAttack,
                    from: unsafe { to.trans_unchecked(!kside) },
                    to,
                    role: Role::Pawn,
                }));
                visitor.moves(WithPromotion::new((targets & promotion).map(|to| {
                    RawMove {
                        kind: MoveType::PawnAttackPromotion,
                        from: unsafe { to.trans_unchecked(!kside) },
                        to,
                        role: Role::Pawn,
                    }
                })));
            }
            // pawn attacks queenside
            {
                let targets =
                    (global_mask_from & ours.pawn() & (!pinned | d.ray(king_square, qside)))
                        .trans(qside)
                        & them
                        & target_mask;
                visitor.moves((targets & !promotion).map(|to| RawMove {
                    kind: MoveType::PawnAttack,
                    from: unsafe { to.trans_unchecked(!qside) },
                    to,
                    role: Role::Pawn,
                }));
                visitor.moves(WithPromotion::new((targets & promotion).map(|to| {
                    RawMove {
                        kind: MoveType::PawnAttackPromotion,
                        from: unsafe { to.trans_unchecked(!qside) },
                        to,
                        role: Role::Pawn,
                    }
                })));
            }
            // pawn double advances
            visitor.moves(
                ((adv & third_rank.bitboard()).trans(forward) & !blockers & target_mask).map(
                    |to| RawMove {
                        kind: MoveType::PawnDoubleAdvance,
                        from: unsafe { to.trans_unchecked(!forward).trans_unchecked(!forward) },
                        to,
                        role: Role::Pawn,
                    },
                ),
            );
            // en passant
            if let Some(to) = en_passant.try_into_square() {
                if global_mask_to.contains(to) {
                    let capture_square = unsafe {
                        // SAFETY: the position is legal
                        to.trans_unchecked(!forward)
                    };
                    if block_check.contains(to)
                        || checker.is_none_or(|checker| checker == capture_square)
                    {
                        let candidates = d.pawn_attack(!turn, to) & ours.pawn();
                        let blockers = blockers ^ capture_square.bitboard();
                        let pinned = pinned
                            | (d.rook(king_square, blockers & !(d.rook(king_square, blockers)))
                                & theirs.rook())
                            .map(|sq| d.segment(king_square, sq))
                            .reduce_or();
                        (global_mask_from & candidates & (!pinned | d.segment(king_square, to)))
                            .for_each(|from| {
                                visitor.en_passant_is_legal();
                                visitor.moves(std::iter::once(RawMove {
                                    kind: MoveType::EnPassant,
                                    from,
                                    to,
                                    role: Role::Pawn,
                                }))
                            })
                    }
                }
            }
        }

        // pieces not pinned
        {
            let aux = |visitor: &mut T, role| {
                for from in global_mask_from & *ours.get(role) & !pinned {
                    visitor.moves(
                        (d.targets(role, from, blockers) & !us & target_mask).map(|to| RawMove {
                            kind: MoveType::PieceMove,
                            from,
                            to,
                            role,
                        }),
                    )
                }
            };
            if visitor.roles(Role::Knight) {
                aux(visitor, Role::Knight)
            }
            if visitor.roles(Role::Bishop) {
                aux(visitor, Role::Bishop)
            }
            if visitor.roles(Role::Rook) {
                aux(visitor, Role::Rook)
            }
            if visitor.roles(Role::Queen) {
                aux(visitor, Role::Queen)
            }
        }

        if checker.is_some() {
            visitor.is_check();
            return;
        }

        // pinned pieces
        {
            let aux = |visitor: &mut T, role| {
                for from in global_mask_from & *ours.get(role) & pinned {
                    visitor.moves(
                        (global_mask_to
                            & d.targets(role, from, blockers)
                            & !us
                            & d.line(king_square, from))
                        .map(|to| RawMove {
                            kind: MoveType::PieceMove,
                            from,
                            to,
                            role,
                        }),
                    )
                }
            };
            if visitor.roles(Role::Bishop) {
                aux(visitor, Role::Bishop)
            }
            if visitor.roles(Role::Rook) {
                aux(visitor, Role::Rook)
            }
            if visitor.roles(Role::Queen) {
                aux(visitor, Role::Queen)
            }
        }
    }

    #[inline]
    unsafe fn play_unchecked(&mut self, m: RawMove) {
        let Self { setup, .. } = self;

        setup.en_passant = OptionSquare::None;

        let RawMove {
            kind,
            from,
            to,
            role,
        } = m;

        match kind {
            MoveType::CastleShort => aux_play_castle(setup, CastlingSide::Short),
            MoveType::CastleLong => aux_play_castle(setup, CastlingSide::Long),
            MoveType::KingMove => aux_play_normal(setup, Role::King, from, to),
            MoveType::PieceMove => aux_play_normal(setup, role, from, to),
            MoveType::PawnAdvance => aux_play_pawn_advance(setup, Role::Pawn, from, to),
            MoveType::PawnAttack => aux_play_normal(setup, Role::Pawn, from, to),
            MoveType::PawnAdvancePromotion => aux_play_pawn_advance(setup, role, from, to),
            MoveType::PawnAttackPromotion => aux_play_normal(setup, role, from, to),
            MoveType::PawnDoubleAdvance => {
                aux_play_pawn_advance(setup, Role::Pawn, from, to);
                setup.en_passant = OptionSquare::new(Some(Square::new(
                    from.file(),
                    match setup.turn {
                        Color::White => Rank::Third,
                        Color::Black => Rank::Sixth,
                    },
                )));
            }
            MoveType::EnPassant => {
                let direction = !setup.turn.forward();
                let x = (unsafe { to.trans_unchecked(direction) }).bitboard();
                setup.p_b_q ^= x;
                setup.w &= !x;
                aux_play_pawn_advance(setup, Role::Pawn, from, to);
            }
        }

        setup.turn = !setup.turn;
    }
}

struct WithPromotion<I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator> {
    inner: I,
    cur: std::mem::MaybeUninit<RawMove>,
    role: Role,
}
impl<I> WithPromotion<I>
where
    I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator,
{
    #[inline]
    fn new(inner: I) -> Self {
        Self {
            inner,
            cur: std::mem::MaybeUninit::uninit(),
            role: Role::King,
        }
    }
}
impl<I> Iterator for WithPromotion<I>
where
    I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator,
{
    type Item = RawMove;
    #[inline]
    fn next(&mut self) -> Option<RawMove> {
        if self.role == Role::King {
            self.cur.write(self.inner.next()?);
            self.role = Role::Knight;
        }
        let raw = unsafe { self.cur.assume_init() };
        let res = RawMove {
            role: self.role,
            ..raw
        };
        self.role = unsafe { Role::transmute((self.role as u8).unchecked_add(1)) };
        Some(res)
    }
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.len();
        (len, Some(len))
    }
}
impl<I> FusedIterator for WithPromotion<I> where
    I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator
{
}
impl<I> ExactSizeIterator for WithPromotion<I>
where
    I: Iterator<Item = RawMove> + ExactSizeIterator + FusedIterator,
{
    #[inline]
    fn len(&self) -> usize {
        unsafe { self.inner.len().unchecked_mul(4) }
    }
}

#[inline]
fn aux_play_normal(setup: &mut Setup, role: Role, from: Square, target: Square) {
    let from = from.bitboard();
    let to = target.bitboard();
    let mask = !(from | to);
    setup.w &= mask;
    setup.p_b_q &= mask;
    setup.n_b_k &= mask;
    setup.r_q_k &= mask;
    if target == Square::new(File::H, setup.turn.promotion_rank()) {
        setup
            .castling_rights
            .unset(!setup.turn, CastlingSide::Short);
    }
    if target == Square::new(File::A, setup.turn.promotion_rank()) {
        setup.castling_rights.unset(!setup.turn, CastlingSide::Long);
    }
    match role {
        Role::King => {
            setup.n_b_k |= to;
            setup.r_q_k |= to;
            setup.castling_rights.unset(setup.turn, CastlingSide::Short);
            setup.castling_rights.unset(setup.turn, CastlingSide::Long);
        }
        Role::Queen => {
            setup.p_b_q |= to;
            setup.r_q_k |= to;
        }
        Role::Bishop => {
            setup.p_b_q |= to;
            setup.n_b_k |= to;
        }
        Role::Knight => {
            setup.n_b_k |= to;
        }
        Role::Rook => {
            setup.r_q_k |= to;
            if from == Square::new(File::H, setup.turn.home_rank()).bitboard() {
                setup.castling_rights.unset(setup.turn, CastlingSide::Short);
            }
            if from == Square::new(File::A, setup.turn.home_rank()).bitboard() {
                setup.castling_rights.unset(setup.turn, CastlingSide::Long);
            }
        }
        Role::Pawn => {
            setup.p_b_q |= to;
        }
    }
    if setup.turn == Color::White {
        setup.w |= to;
    }
}

#[inline]
fn aux_play_pawn_advance(setup: &mut Setup, role: Role, from: Square, to: Square) {
    let from = from.bitboard();
    let to = to.bitboard();
    match role {
        Role::King => unreachable!(),
        Role::Queen => {
            setup.p_b_q ^= from | to;
            setup.r_q_k |= to;
        }
        Role::Bishop => {
            setup.p_b_q ^= from | to;
            setup.n_b_k |= to;
        }
        Role::Knight => {
            setup.p_b_q ^= from;
            setup.n_b_k |= to;
        }
        Role::Rook => {
            setup.p_b_q ^= from;
            setup.r_q_k |= to;
        }
        Role::Pawn => setup.p_b_q ^= from | to,
    }
    if setup.turn == Color::White {
        setup.w ^= from | to;
    }
}

#[inline]
fn aux_play_castle(setup: &mut Setup, side: CastlingSide) {
    let rank = setup.turn.home_rank();
    let (king_flip, rook_flip) = match side {
        CastlingSide::Short => (
            Square::new(File::E, rank).bitboard() | Square::new(File::G, rank).bitboard(),
            Square::new(File::H, rank).bitboard() | Square::new(File::F, rank).bitboard(),
        ),
        CastlingSide::Long => (
            Square::new(File::E, rank).bitboard() | Square::new(File::C, rank).bitboard(),
            Square::new(File::A, rank).bitboard() | Square::new(File::D, rank).bitboard(),
        ),
    };

    if setup.turn == Color::White {
        setup.w ^= king_flip | rook_flip;
    }
    setup.n_b_k ^= king_flip;
    setup.r_q_k ^= king_flip | rook_flip;

    setup.castling_rights.unset(setup.turn, CastlingSide::Short);
    setup.castling_rights.unset(setup.turn, CastlingSide::Long);
}

struct MateCollector {
    is_check: bool,
    is_mate: bool,
}
impl MateCollector {
    #[inline]
    fn new() -> Self {
        Self {
            is_check: false,
            is_mate: true,
        }
    }
}
impl Visitor for MateCollector {
    #[inline]
    fn is_check(&mut self) {
        self.is_check = true;
    }
    #[inline]
    fn en_passant_is_legal(&mut self) {}
    #[inline]
    fn moves<I>(&mut self, iter: I)
    where
        I: Iterator<Item = RawMove> + ExactSizeIterator,
    {
        self.is_mate &= iter.len() == 0;
    }
}