kish 1.1.7

A high-performance Turkish Draughts (Dama) engine with bitboard representation
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
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
//! Move generation for Turkish Draughts.
//!
//! This module implements legal move generation following the official rules:
//!
//! # Move Generation Rules
//!
//! 1. **Mandatory capture**: If any capture is available, a capture must be made.
//!    Non-capturing moves are only generated when no captures exist.
//!
//! 2. **Maximum capture rule**: When multiple capture sequences are possible,
//!    the player must choose a sequence that captures the maximum number of pieces.
//!
//! 3. **180-degree turn prohibition**: During a multi-capture sequence, the piece
//!    cannot reverse direction (e.g., if moving up, cannot immediately move down).
//!
//! 4. **Flying captures**: Kings can capture from any distance along a rank/file,
//!    landing on any empty square beyond the captured piece.
//!
//! # Implementation Details
//!
//! ## Lookup Tables
//!
//! Precomputed move masks for each square type:
//! - `WHITE_PAWN_MOVES[sq]`: Valid non-capturing destinations for white pawns
//! - `BLACK_PAWN_MOVES[sq]`: Valid non-capturing destinations for black pawns
//!
//! ## Capture Generation Strategy
//!
//! Captures are generated using a recursive approach with XOR-based state updates:
//!
//! 1. Try all single captures from the current position
//! 2. For each capture, temporarily apply it using XOR (modifies state)
//! 3. Recursively search for additional captures
//! 4. Undo the capture using XOR (restores state)
//! 5. Track the maximum capture count across all sequences
//! 6. Only add actions that achieve the maximum capture count
//!
//! This approach is cache-friendly and avoids allocating new board states.
//!
//! ## Const Generics
//!
//! Team-specific logic uses `const TEAM_INDEX: usize` to generate specialized
//! code paths at compile time, avoiding runtime branching in hot loops.
//!
//! # Example
//!
//! ```rust
//! use kish::{Board, Team};
//!
//! let board = Board::new_default();
//! let actions = board.actions();
//!
//! // Initial position has no captures, so non-capturing moves are generated
//! // White pawns can move forward (32 pawns × 1 forward move each, minus blocked)
//! assert!(actions.len() > 0);
//! ```

use super::{Action, Board, Team};
use crate::state::{
    MASK_COL_A, MASK_COL_B, MASK_COL_G, MASK_COL_H, MASK_ROW_1, MASK_ROW_2, MASK_ROW_7, MASK_ROW_8,
    MASK_ROW_PROMOTIONS,
};

/// Precomputed ray masks for each direction from each square.
/// Used for early-exit checks in king capture generation.
/// LEFT_RAY[sq] = all squares to the left of sq on the same row
const LEFT_RAY: [u64; 64] = {
    let mut rays = [0u64; 64];
    let mut sq = 0;
    while sq < 64 {
        let col = sq % 8;
        let row_start = sq - col;
        // All squares from row_start to sq-1
        let mut mask = 0u64;
        let mut c = 0;
        while c < col {
            mask |= 1u64 << (row_start + c);
            c += 1;
        }
        rays[sq] = mask;
        sq += 1;
    }
    rays
};

/// RIGHT_RAY[sq] = all squares to the right of sq on the same row
const RIGHT_RAY: [u64; 64] = {
    let mut rays = [0u64; 64];
    let mut sq = 0;
    while sq < 64 {
        let col = sq % 8;
        let row_start = sq - col;
        // All squares from sq+1 to row_end
        let mut mask = 0u64;
        let mut c = col + 1;
        while c < 8 {
            mask |= 1u64 << (row_start + c);
            c += 1;
        }
        rays[sq] = mask;
        sq += 1;
    }
    rays
};

/// UP_RAY[sq] = all squares above sq on the same column
const UP_RAY: [u64; 64] = {
    let mut rays = [0u64; 64];
    let mut sq = 0;
    while sq < 64 {
        let col = sq % 8;
        let row = sq / 8;
        // All squares from sq+8 to top of column
        let mut mask = 0u64;
        let mut r = row + 1;
        while r < 8 {
            mask |= 1u64 << (r * 8 + col);
            r += 1;
        }
        rays[sq] = mask;
        sq += 1;
    }
    rays
};

/// DOWN_RAY[sq] = all squares below sq on the same column
const DOWN_RAY: [u64; 64] = {
    let mut rays = [0u64; 64];
    let mut sq = 0;
    while sq < 64 {
        let col = sq % 8;
        let row = sq / 8;
        // All squares from row-1 down to row 0
        let mut mask = 0u64;
        let mut r: i32 = row as i32 - 1;
        while r >= 0 {
            mask |= 1u64 << (r as usize * 8 + col);
            r -= 1;
        }
        rays[sq] = mask;
        sq += 1;
    }
    rays
};

/// Precomputed rank attack masks for king sliding moves.
/// RANK_ATTACKS[sq][occ6] where occ6 is the 6-bit occupancy of columns 1-6.
/// Returns a bitmask of all squares the king can reach along the rank.
#[allow(clippy::large_const_arrays)]
const RANK_ATTACKS: [[u64; 64]; 64] = {
    let mut table = [[0u64; 64]; 64];
    let mut sq = 0usize;
    while sq < 64 {
        let col = sq % 8;
        let row_start = sq - col;

        let mut occ6 = 0usize;
        while occ6 < 64 {
            // Expand 6-bit occupancy to full row (bits 1-6)
            let occ = (occ6 as u64) << 1;
            let mut attacks = 0u64;

            // Move left
            let mut c = col as i8 - 1;
            while c >= 0 {
                attacks |= 1u64 << (row_start + c as usize);
                if (occ & (1u64 << c)) != 0 {
                    break;
                }
                c -= 1;
            }

            // Move right
            c = col as i8 + 1;
            while c < 8 {
                attacks |= 1u64 << (row_start + c as usize);
                if (occ & (1u64 << c)) != 0 {
                    break;
                }
                c += 1;
            }

            table[sq][occ6] = attacks;
            occ6 += 1;
        }
        sq += 1;
    }
    table
};

/// Precomputed file attack masks for king sliding moves.
/// FILE_ATTACKS[sq][occ6] where occ6 is the 6-bit occupancy of rows 1-6.
/// Returns a bitmask of all squares the king can reach along the file.
#[allow(clippy::large_const_arrays)]
const FILE_ATTACKS: [[u64; 64]; 64] = {
    let mut table = [[0u64; 64]; 64];
    let mut sq = 0usize;
    while sq < 64 {
        let col = sq % 8;
        let row = sq / 8;

        let mut occ6 = 0usize;
        while occ6 < 64 {
            // Expand 6-bit occupancy to full file (rows 1-6)
            let occ = (occ6 as u64) << 1;
            let mut attacks = 0u64;

            // Move down
            let mut r = row as i8 - 1;
            while r >= 0 {
                attacks |= 1u64 << (r as usize * 8 + col);
                if (occ & (1u64 << r)) != 0 {
                    break;
                }
                r -= 1;
            }

            // Move up
            r = row as i8 + 1;
            while r < 8 {
                attacks |= 1u64 << (r as usize * 8 + col);
                if (occ & (1u64 << r)) != 0 {
                    break;
                }
                r += 1;
            }

            table[sq][occ6] = attacks;
            occ6 += 1;
        }
        sq += 1;
    }
    table
};

/// Masks to extract the relevant 6 bits for rank occupancy (columns 1-6).
const RANK_OCC_MASK: [u64; 64] = {
    let mut masks = [0u64; 64];
    let mut sq = 0;
    while sq < 64 {
        let col = sq % 8;
        let row_start = sq - col;
        // Columns 1-6 (bits 1-6 of the row)
        masks[sq] = 0x7Eu64 << row_start;
        sq += 1;
    }
    masks
};

const WHITE_PAWN_MOVES: [u64; 64] = {
    let mut moves = [0u64; 64];
    let mut src_index = 0;
    while src_index < 64 {
        let src_mask = 1u64 << src_index;
        moves[src_index] = (src_mask & !MASK_COL_A) >> 1u8 // left
            | (src_mask & !MASK_COL_H) << 1u8 // right
            | (src_mask & !MASK_ROW_8) << 8u8; // up
        src_index += 1;
    }
    moves
};

const BLACK_PAWN_MOVES: [u64; 64] = {
    let mut moves = [0u64; 64];
    let mut src_index = 0;
    while src_index < 64 {
        let src_mask = 1u64 << src_index;
        moves[src_index] = (src_mask & !MASK_COL_A) >> 1u8 // left
            | (src_mask & !MASK_COL_H) << 1u8 // right
            | (src_mask & !MASK_ROW_1) >> 8u8; // down
        src_index += 1;
    }
    moves
};

impl Board {
    /// Computes the valid actions of the board.
    #[must_use]
    #[inline]
    pub fn actions(&self) -> Vec<Action> {
        let mut actions = Vec::with_capacity(32);
        self.actions_into(&mut actions);
        actions
    }

    /// Computes the valid actions and stores them in the provided Vec.
    ///
    /// The Vec is cleared before adding actions. This allows callers to reuse
    /// a Vec across multiple calls without manual clearing.
    #[inline]
    pub fn actions_into(&self, actions: &mut Vec<Action>) {
        actions.clear();
        if self.turn == Team::White {
            self.generate_captures::<0>(actions);

            if actions.is_empty() {
                self.generate_moves::<0>(actions);
            }
        } else {
            self.generate_captures::<1>(actions);

            if actions.is_empty() {
                self.generate_moves::<1>(actions);
            }
        }
    }

    /// Counts the number of valid actions using the provided scratch buffer.
    ///
    /// This is faster than `actions_into` when you only need the count,
    /// particularly useful for bulk leaf counting in perft at depth 1.
    /// Returns 0 for terminal positions (no actions available).
    #[inline]
    pub fn count_actions(&self, scratch: &mut Vec<Action>) -> u64 {
        if self.turn == Team::White {
            let capture_count = self.count_captures::<0>(scratch);
            if capture_count > 0 {
                capture_count
            } else {
                self.count_moves::<0>()
            }
        } else {
            let capture_count = self.count_captures::<1>(scratch);
            if capture_count > 0 {
                capture_count
            } else {
                self.count_moves::<1>()
            }
        }
    }

    /// Count captures using provided scratch buffer to avoid allocation.
    fn count_captures<const TEAM_INDEX: usize>(&self, scratch: &mut Vec<Action>) -> u64 {
        let may_have_pawn_captures = self.has_any_pawn_captures::<TEAM_INDEX>();
        let has_friendly_kings = (self.friendly_pieces() & self.state.kings) != 0;

        if !may_have_pawn_captures && !has_friendly_kings {
            return 0;
        }

        // For captures, we need to track max length and generate actions
        // to properly implement the maximum capture rule.
        scratch.clear();
        let mut max_length: u32 = 0;
        let mut board = *self;

        if has_friendly_kings {
            Self::generate_king_captures_with_board::<TEAM_INDEX>(
                &mut board,
                scratch,
                &mut max_length,
            );
        }

        if may_have_pawn_captures {
            Self::generate_pawn_captures_with_board::<TEAM_INDEX>(
                &mut board,
                scratch,
                &mut max_length,
            );
        }

        scratch.len() as u64
    }

    /// Count non-capture moves without generating Action structs.
    fn count_moves<const TEAM_INDEX: usize>(&self) -> u64 {
        let empty = self.state.empty();
        self.count_king_moves::<TEAM_INDEX>(empty) + self.count_pawn_moves::<TEAM_INDEX>(empty)
    }

    /// Count pawn moves using bitboard popcount.
    #[inline]
    fn count_pawn_moves<const TEAM_INDEX: usize>(&self, empty: u64) -> u64 {
        let friendly_pawns = self.friendly_pieces() & !self.state.kings;
        let mut count = 0u64;
        let mut pawns = friendly_pawns;

        while pawns != 0 {
            let src_mask = pawns & pawns.wrapping_neg();
            let src_index = src_mask.trailing_zeros() as usize;
            let moves = if TEAM_INDEX == 0 {
                WHITE_PAWN_MOVES[src_index] & empty
            } else {
                BLACK_PAWN_MOVES[src_index] & empty
            };
            count += moves.count_ones() as u64;
            pawns ^= src_mask;
        }
        count
    }

    /// Count king moves using precomputed attack tables.
    /// This is much faster than iterating per-direction.
    #[inline]
    fn count_king_moves<const TEAM_INDEX: usize>(&self, empty: u64) -> u64 {
        let occupied = !empty;
        let mut friendly_kings = self.friendly_pieces() & self.state.kings;
        let mut count = 0u64;

        while friendly_kings != 0 {
            let src_mask = friendly_kings & friendly_kings.wrapping_neg();
            let sq = src_mask.trailing_zeros() as usize;

            // Get king attacks using lookup tables
            let attacks = Self::king_attacks_lut(sq, occupied);
            count += (attacks & empty).count_ones() as u64;

            friendly_kings ^= src_mask;
        }
        count
    }

    /// Get all squares a king can attack from a given square using lookup tables.
    /// This uses precomputed rank and file attack tables indexed by occupancy.
    #[inline(always)]
    fn king_attacks_lut(sq: usize, occupied: u64) -> u64 {
        // Extract 6-bit occupancy for rank (columns 1-6)
        let rank_occ = (occupied & RANK_OCC_MASK[sq]) >> (sq - sq % 8 + 1);
        let rank_occ6 = rank_occ as usize & 0x3F;

        // Extract 6-bit occupancy for file (rows 1-6)
        // We need to compress the file bits into 6 consecutive bits
        let col = sq % 8;
        let file_bits = (occupied >> col) & 0x0101_0101_0101_0101u64;
        // Multiply trick to gather bits: multiply by a magic number and shift
        let file_occ6 = ((file_bits.wrapping_mul(0x0002_0408_1020_4080u64)) >> 57) as usize & 0x3F;

        // Lookup attacks from precomputed tables
        RANK_ATTACKS[sq][rank_occ6] | FILE_ATTACKS[sq][file_occ6]
    }

    /// Quick check if any pawn captures are possible using bulk bitboard ops.
    ///
    /// For each direction, verifies that the SAME pawn has both:
    /// - An adjacent hostile piece (to capture)
    /// - An empty square beyond it (to land on)
    const fn has_any_pawn_captures<const TEAM_INDEX: usize>(&self) -> bool {
        let friendly_pawns = self.friendly_pieces() & !self.state.kings;
        if friendly_pawns == 0 {
            return false;
        }

        let hostile = self.hostile_pieces();
        let empty = self.state.empty();

        // Check left captures: pawn at P, hostile at P-1, empty at P-2
        {
            let eligible = friendly_pawns & !(MASK_COL_A | MASK_COL_B);
            let adjacent_hostile = (eligible >> 1) & hostile; // Hostiles at P-1
            let landing_clear = (eligible >> 2) & empty; // Empty at P-2
                                                         // Correlate: landing at L means hostile must be at L+1
            if (landing_clear << 1) & adjacent_hostile != 0 {
                return true;
            }
        }

        // Check right captures: pawn at P, hostile at P+1, empty at P+2
        {
            let eligible = friendly_pawns & !(MASK_COL_G | MASK_COL_H);
            let adjacent_hostile = (eligible << 1) & hostile; // Hostiles at P+1
            let landing_clear = (eligible << 2) & empty; // Empty at P+2
                                                         // Correlate: landing at L means hostile must be at L-1
            if (landing_clear >> 1) & adjacent_hostile != 0 {
                return true;
            }
        }

        // Check vertical captures (team-dependent)
        if TEAM_INDEX == 0 {
            // White: up captures (pawn at P, hostile at P+8, empty at P+16)
            let eligible = friendly_pawns & !(MASK_ROW_7 | MASK_ROW_8);
            let adjacent_hostile = (eligible << 8) & hostile;
            let landing_clear = (eligible << 16) & empty;
            if (landing_clear >> 8) & adjacent_hostile != 0 {
                return true;
            }
        } else {
            // Black: down captures (pawn at P, hostile at P-8, empty at P-16)
            let eligible = friendly_pawns & !(MASK_ROW_1 | MASK_ROW_2);
            let adjacent_hostile = (eligible >> 8) & hostile;
            let landing_clear = (eligible >> 16) & empty;
            if (landing_clear << 8) & adjacent_hostile != 0 {
                return true;
            }
        }

        false
    }

    #[inline]
    fn generate_captures<const TEAM_INDEX: usize>(&self, actions: &mut Vec<Action>) {
        // Early exit checks:
        // - Pawn captures use a fast bulk bitboard check (no iteration)
        // - King captures just check existence (actual capture check happens during generation)
        let may_have_pawn_captures = self.has_any_pawn_captures::<TEAM_INDEX>();
        let has_friendly_kings = (self.friendly_pieces() & self.state.kings) != 0;

        if !may_have_pawn_captures && !has_friendly_kings {
            return;
        }

        // Track max capture length inline to avoid second pass
        let mut max_length: u32 = 0;

        // We need a mutable copy for the recursive capture generation
        let mut board = *self;

        // Generate king captures first (kings often have longer chains)
        if has_friendly_kings {
            Self::generate_king_captures_with_board::<TEAM_INDEX>(
                &mut board,
                actions,
                &mut max_length,
            );
        }

        // Generate pawn captures (only if bulk check passed)
        if may_have_pawn_captures {
            Self::generate_pawn_captures_with_board::<TEAM_INDEX>(
                &mut board,
                actions,
                &mut max_length,
            );
        }
    }

    /// Helper to push action while tracking max capture length
    #[inline(always)]
    fn push_capture_action<const TEAM_INDEX: usize>(
        actions: &mut Vec<Action>,
        max_length: &mut u32,
        action: Action,
    ) {
        let length = action.delta.pieces[1 - TEAM_INDEX].count_ones();
        if length > *max_length {
            // New best - clear existing and update max
            actions.clear();
            *max_length = length;
            actions.push(action);
        } else if length == *max_length {
            // Equal to best - just add
            actions.push(action);
        }
        // length < max_length: discard
    }

    #[inline]
    fn generate_pawn_captures_with_board<const TEAM_INDEX: usize>(
        board: &mut Self,
        actions: &mut Vec<Action>,
        max_length: &mut u32,
    ) {
        let mut friendly_pawns = board.friendly_pieces() & !board.state.kings;

        while friendly_pawns != 0u64 {
            let src_mask = friendly_pawns & friendly_pawns.wrapping_neg(); // get lowest set bit

            // Start with no previous direction (0)
            Self::generate_pawn_captures_at::<TEAM_INDEX, 0>(
                board,
                actions,
                max_length,
                src_mask,
                Action::EMPTY,
            );

            friendly_pawns ^= src_mask; // clear lowest set bit
        }
    }

    /// Generates pawn capture sequences with 180-degree turn prevention.
    ///
    /// # Const Parameters
    /// - `TEAM_INDEX`: 0 for White, 1 for Black
    /// - `PREVIOUS_DIRECTION`: The direction of the previous capture in the sequence.
    ///   - 0: No previous capture (initial state)
    ///   - -1: Previous capture was left
    ///   - 1: Previous capture was right
    ///   - 8: Previous capture was up (forward for white)
    ///   - -8: Previous capture was down (forward for black)
    ///
    /// The 180-degree turn rule prohibits reversing direction within a capture sequence
    /// (e.g., left then right, or right then left).
    #[inline]
    fn generate_pawn_captures_at<const TEAM_INDEX: usize, const PREVIOUS_DIRECTION: i8>(
        board: &mut Self,
        actions: &mut Vec<Action>,
        max_length: &mut u32,
        src_mask: u64,
        previous_action: Action,
    ) {
        let mut has_more_captures = false;

        let hostile_pieces = board.hostile_pieces();
        let empty = board.state.empty();

        // Generate pawn left captures (direction = -1)
        // Skip if previous direction was right (+1), as that would be a 180-degree turn
        if PREVIOUS_DIRECTION != 1 {
            let left_capture_mask =
                ((src_mask & !(MASK_COL_A | MASK_COL_B)) >> 1u8) & hostile_pieces;
            let left_dest_mask = ((src_mask & !(MASK_COL_A | MASK_COL_B)) >> 2u8) & empty;
            if left_capture_mask != 0u64 && left_dest_mask != 0u64 {
                has_more_captures = true;

                let capture_action = Action::new_capture_as_pawn::<TEAM_INDEX>(
                    src_mask,
                    left_dest_mask,
                    left_capture_mask,
                    board.state.kings,
                );

                // Apply action
                board.apply_(&capture_action);

                Self::generate_pawn_captures_at::<TEAM_INDEX, -1>(
                    board,
                    actions,
                    max_length,
                    left_dest_mask,
                    previous_action.combine(&capture_action),
                );

                // Undo action
                board.apply_(&capture_action);
            }
        }

        // Generate pawn right captures (direction = +1)
        // Skip if previous direction was left (-1), as that would be a 180-degree turn
        if PREVIOUS_DIRECTION != -1 {
            let right_capture_mask =
                ((src_mask & !(MASK_COL_G | MASK_COL_H)) << 1u8) & hostile_pieces;
            let right_dest_mask = ((src_mask & !(MASK_COL_G | MASK_COL_H)) << 2u8) & empty;
            if right_capture_mask != 0u64 && right_dest_mask != 0u64 {
                has_more_captures = true;

                let capture_action = Action::new_capture_as_pawn::<TEAM_INDEX>(
                    src_mask,
                    right_dest_mask,
                    right_capture_mask,
                    board.state.kings,
                );

                // Apply action
                board.apply_(&capture_action);

                Self::generate_pawn_captures_at::<TEAM_INDEX, 1>(
                    board,
                    actions,
                    max_length,
                    right_dest_mask,
                    previous_action.combine(&capture_action),
                );

                // Undo action
                board.apply_(&capture_action);
            }
        }

        // Generate pawn vertical captures (white=up, black=down)
        // Vertical direction is +8 for white (up), -8 for black (down)
        // Note: Pawns cannot capture backward, so there's no opposite vertical direction to check
        let (vert_capture_mask, vert_dest_mask): (u64, u64) = if TEAM_INDEX == 0 {
            let mask = !(MASK_ROW_7 | MASK_ROW_8);
            (
                ((src_mask & mask) << 8u8) & hostile_pieces,
                ((src_mask & mask) << 16u8) & empty,
            )
        } else {
            let mask = !(MASK_ROW_1 | MASK_ROW_2);
            (
                ((src_mask & mask) >> 8u8) & hostile_pieces,
                ((src_mask & mask) >> 16u8) & empty,
            )
        };

        // Vertical captures don't conflict with left/right (no 180-degree turn possible)
        // since pawns can't capture backward
        if vert_capture_mask != 0u64 && vert_dest_mask != 0u64 {
            has_more_captures = true;
            let capture_action = Action::new_capture_as_pawn::<TEAM_INDEX>(
                src_mask,
                vert_dest_mask,
                vert_capture_mask,
                board.state.kings,
            );

            board.apply_(&capture_action);

            // Select const generic direction based on team (white=up/8, black=down/-8)
            if TEAM_INDEX == 0 {
                Self::generate_pawn_captures_at::<TEAM_INDEX, 8>(
                    board,
                    actions,
                    max_length,
                    vert_dest_mask,
                    previous_action.combine(&capture_action),
                );
            } else {
                Self::generate_pawn_captures_at::<TEAM_INDEX, -8>(
                    board,
                    actions,
                    max_length,
                    vert_dest_mask,
                    previous_action.combine(&capture_action),
                );
            }

            board.apply_(&capture_action);
        }

        if !has_more_captures && !previous_action.is_empty() {
            // Add promotion if the capture sequence ends on the promotion row.
            // Note: src_mask here is the final destination of the sequence.
            let mut final_action = previous_action;
            let promotion_mask = MASK_ROW_PROMOTIONS[TEAM_INDEX];
            if src_mask & promotion_mask != 0 {
                // Promote the pawn at the final destination
                final_action.delta.kings ^= src_mask;
            }
            Self::push_capture_action::<TEAM_INDEX>(actions, max_length, final_action);
        }
    }

    #[inline]
    fn generate_king_captures_with_board<const TEAM_INDEX: usize>(
        board: &mut Self,
        actions: &mut Vec<Action>,
        max_length: &mut u32,
    ) {
        let mut friendly_kings = board.friendly_pieces() & board.state.kings;

        while friendly_kings != 0u64 {
            let src_mask = friendly_kings & friendly_kings.wrapping_neg(); // get lowest set bit

            Self::generate_king_captures_at::<TEAM_INDEX, 0i8>(
                board,
                actions,
                max_length,
                src_mask,
                Action::EMPTY,
            );

            friendly_kings ^= src_mask; // clear lowest set bit
        }
    }

    #[inline]
    fn generate_king_captures_at<const TEAM_INDEX: usize, const PREVIOUS_DIRECTION: i8>(
        board: &mut Self,
        actions: &mut Vec<Action>,
        max_length: &mut u32,
        src_mask: u64,
        previous_action: Action,
    ) {
        let mut has_more_captures = false;

        let friendly_pieces = board.friendly_pieces();
        let hostile_pieces = board.hostile_pieces();

        let src_index = src_mask.trailing_zeros() as usize;

        // Early exit checks using ray masks:
        // Only scan a direction if there's at least one hostile in that direction.
        // This avoids calling gen_inner for directions with no possible captures.

        // Eat left (only if not coming from right and hostile exists left)
        if PREVIOUS_DIRECTION != 1 && (hostile_pieces & LEFT_RAY[src_index]) != 0 {
            board
                .gen_inner::<-1i8, PREVIOUS_DIRECTION, TEAM_INDEX, { !(MASK_COL_A | MASK_COL_B) }>(
                    src_mask,
                    src_index as u8,
                    friendly_pieces,
                    hostile_pieces,
                    &mut has_more_captures,
                    actions,
                    max_length,
                    previous_action,
                );
        }

        // Eat right (only if not coming from left and hostile exists right)
        if PREVIOUS_DIRECTION != -1 && (hostile_pieces & RIGHT_RAY[src_index]) != 0 {
            board.gen_inner::<1i8, PREVIOUS_DIRECTION, TEAM_INDEX, { !(MASK_COL_G | MASK_COL_H) }>(
                src_mask,
                src_index as u8,
                friendly_pieces,
                hostile_pieces,
                &mut has_more_captures,
                actions,
                max_length,
                previous_action,
            );
        }

        // Eat up (only if not coming from down and hostile exists up)
        if PREVIOUS_DIRECTION != -8 && (hostile_pieces & UP_RAY[src_index]) != 0 {
            board.gen_inner::<8i8, PREVIOUS_DIRECTION, TEAM_INDEX, { !(MASK_ROW_7 | MASK_ROW_8) }>(
                src_mask,
                src_index as u8,
                friendly_pieces,
                hostile_pieces,
                &mut has_more_captures,
                actions,
                max_length,
                previous_action,
            );
        }

        // Eat down (only if not coming from up and hostile exists down)
        if PREVIOUS_DIRECTION != 8 && (hostile_pieces & DOWN_RAY[src_index]) != 0 {
            board
                .gen_inner::<-8i8, PREVIOUS_DIRECTION, TEAM_INDEX, { !(MASK_ROW_1 | MASK_ROW_2) }>(
                    src_mask,
                    src_index as u8,
                    friendly_pieces,
                    hostile_pieces,
                    &mut has_more_captures,
                    actions,
                    max_length,
                    previous_action,
                );
        }

        if !has_more_captures && !previous_action.is_empty() {
            Self::push_capture_action::<TEAM_INDEX>(actions, max_length, previous_action);
        }
    }

    #[allow(clippy::too_many_arguments)] // Internal recursive function with const generics
    #[inline(always)]
    fn gen_inner<
        const DIRECTION: i8,
        const PREVIOUS_DIRECTION: i8,
        const TEAM_INDEX: usize,
        const CHECKMASK: u64,
    >(
        &mut self,
        src_mask: u64,
        src_index: u8,
        friendly_pieces: u64,
        hostile_pieces: u64,
        has_more_captures: &mut bool,
        actions: &mut Vec<Action>,
        max_length: &mut u32,
        previous_action: Action,
    ) {
        // Note: DIRECTION == -PREVIOUS_DIRECTION check is now done at caller level
        // with early ray mask checks for better performance.

        // If we cannot capture, abort
        if (CHECKMASK >> src_index) & 1u64 != 1u64 {
            return;
        }

        let mut temp_index = src_index as i8 + DIRECTION;
        const NO_CAPTURE: u8 = 255; // Sentinel value (valid indices are 0-63)
        let mut capture_index: u8 = NO_CAPTURE;
        let mut possible_move_indices: [u8; 7] = [0; 7]; // Max 7 landing squares in any direction
        let mut possible_move_count: usize = 0;

        // Use simple comparison instead of Range::contains for performance
        #[allow(clippy::manual_range_contains)]
        while temp_index >= 0 && temp_index <= 63 {
            let temp_index_mask = 1u64 << temp_index;
            if friendly_pieces & temp_index_mask != 0 {
                // Blocked by friendly
                break;
            }
            if hostile_pieces & temp_index_mask != 0 {
                if capture_index != NO_CAPTURE {
                    // Either encountered two hostiles after each other
                    // or, encountered a hostile then empty* then hostile
                    break;
                }
                // Encountered first hostile
                capture_index = temp_index as u8;
            } else if capture_index != NO_CAPTURE {
                // Empty square after capturing a hostile - valid landing
                #[allow(clippy::cast_sign_loss)] // temp_index is 0..=63 here
                {
                    possible_move_indices[possible_move_count] = temp_index as u8;
                }
                possible_move_count += 1;
            }

            // Check for edge conditions BEFORE advancing
            // This avoids redundant iteration
            if DIRECTION == 1 && temp_index % 8 == 7 {
                break; // At right edge, can't go further right
            }
            if DIRECTION == -1 && temp_index % 8 == 0 {
                break; // At left edge, can't go further left
            }

            temp_index += DIRECTION;
        }

        if possible_move_count == 0 {
            return;
        }
        // Note: possible_move_count > 0 implies capture_index != NO_CAPTURE
        // (we only increment count after finding a hostile to capture)

        let capture_index_mask = 1u64 << capture_index;

        for &possible_move_index in &possible_move_indices[..possible_move_count] {
            let dest_mask = 1u64 << possible_move_index;
            *has_more_captures = true;

            let capture_action = Action::new_capture_as_king::<TEAM_INDEX>(
                src_mask,
                dest_mask,
                capture_index_mask,
                self.state.kings,
            );

            // Apply action
            self.apply_(&capture_action);

            Self::generate_king_captures_at::<TEAM_INDEX, DIRECTION>(
                self,
                actions,
                max_length,
                dest_mask,
                previous_action.combine(&capture_action),
            );

            // Undo action
            self.apply_(&capture_action);
        }
    }

    #[inline]
    fn generate_moves<const TEAM_INDEX: usize>(&self, actions: &mut Vec<Action>) {
        let empty = self.state.empty();
        self.generate_king_moves::<TEAM_INDEX>(actions, empty);
        self.generate_pawn_moves::<TEAM_INDEX>(actions, empty);
    }

    #[inline]
    fn generate_pawn_moves<const TEAM_INDEX: usize>(&self, actions: &mut Vec<Action>, empty: u64) {
        let mut friendly_pawns = self.friendly_pieces() & !self.state.kings;
        while friendly_pawns != 0u64 {
            let src_mask = friendly_pawns & friendly_pawns.wrapping_neg(); // get lowest set bit
            let src_index: usize = src_mask.trailing_zeros() as usize;
            let mut possible_dest_masks = if TEAM_INDEX == 0 {
                WHITE_PAWN_MOVES[src_index] & empty
            } else {
                BLACK_PAWN_MOVES[src_index] & empty
            };

            while possible_dest_masks != 0u64 {
                let dest_mask = possible_dest_masks & possible_dest_masks.wrapping_neg(); // get lowest set bit
                actions.push(Action::new_move_as_pawn::<TEAM_INDEX>(src_mask, dest_mask));
                possible_dest_masks ^= dest_mask; // clear lowest set bit
            }

            friendly_pawns ^= src_mask; // clear lowest set bit
        }
    }

    /// Generate king moves using precomputed attack tables.
    /// Gets all attack squares in one lookup, then iterates destinations.
    #[inline]
    fn generate_king_moves<const TEAM_INDEX: usize>(&self, actions: &mut Vec<Action>, empty: u64) {
        let occupied = !empty;
        let mut friendly_kings = self.friendly_pieces() & self.state.kings;

        while friendly_kings != 0u64 {
            let src_mask = friendly_kings & friendly_kings.wrapping_neg();
            let sq = src_mask.trailing_zeros() as usize;

            // Get all attack squares using lookup table
            let attacks = Self::king_attacks_lut(sq, occupied);
            let mut moves = attacks & empty;

            // Iterate through all destination squares
            while moves != 0u64 {
                let dest_mask = moves & moves.wrapping_neg();
                actions.push(Action::new_move_as_king::<TEAM_INDEX>(src_mask, dest_mask));
                moves ^= dest_mask;
            }

            friendly_kings ^= src_mask;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::game_status::GameStatus;
    use crate::Square;

    // ========== Initial Position Tests ==========

    #[test]
    fn initial_position_action_count() {
        let board = Board::new_default();
        let actions = board.actions();
        // Initial position has white pawns on rows 2 and 3
        // Row 2 is blocked by row 3 (can't move up)
        // Row 3 can move up into row 4
        // Both rows can move left/right where possible
        assert!(!actions.is_empty(), "Initial position should have moves");
        // Just verify it's a reasonable number - the exact count depends on rules
        assert!(
            !actions.is_empty(),
            "Should have moves from initial position"
        );
    }

    #[test]
    fn initial_position_only_moves_no_captures() {
        let board = Board::new_default();
        let actions = board.actions();
        // In initial position, no captures are possible
        for action in &actions {
            // A move has no captures, so opponent pieces delta should be 0
            assert_eq!(
                action.delta.pieces[Team::Black.to_usize()],
                0,
                "Initial position should only have moves, not captures"
            );
        }
    }

    // ========== Forced Capture Tests ==========

    #[test]
    fn forced_capture_rule() {
        // White pawn at D4, black pawn at D5 (capturable) and black pawn at H8
        // White should be forced to capture
        let board = Board::from_squares(Team::White, &[Square::D4], &[Square::D5, Square::H8], &[]);
        let actions = board.actions();

        assert_eq!(actions.len(), 1, "Should have exactly one capture");
        // Verify it's a capture (black pieces are affected)
        assert_ne!(
            actions[0].delta.pieces[Team::Black.to_usize()],
            0,
            "Action should be a capture"
        );
    }

    #[test]
    fn multiple_capture_options() {
        // White pawn at D4, can capture left (C5) or right (E5) or up (D5)
        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[Square::C4, Square::E4, Square::D5],
            &[],
        );
        let actions = board.actions();

        assert_eq!(actions.len(), 3, "Should have three capture options");
        for action in &actions {
            assert_ne!(
                action.delta.pieces[Team::Black.to_usize()],
                0,
                "All actions should be captures"
            );
        }
    }

    // ========== Maximum Capture Rule Tests ==========

    #[test]
    fn maximum_capture_rule_prefers_longer_chain() {
        // White pawn at A4
        // Option 1: capture B4 landing at C4 (length 1)
        // Option 2: capture A5 landing at A6, then capture B6 landing at C6 (length 2)
        let board = Board::from_squares(
            Team::White,
            &[Square::A4],
            &[Square::B4, Square::A5, Square::B6],
            &[],
        );
        let actions = board.actions();

        // Should only return the length-2 capture chain
        for action in &actions {
            let capture_count = action.delta.pieces[Team::Black.to_usize()].count_ones();
            assert_eq!(
                capture_count, 2,
                "Should only return maximum length captures (2)"
            );
        }
    }

    #[test]
    fn maximum_capture_multiple_equal_length() {
        // Two capture chains of equal length should both be returned
        let board = Board::from_squares(Team::White, &[Square::D4], &[Square::D5, Square::E4], &[]);
        let actions = board.actions();

        // Both are length-1 captures
        assert_eq!(
            actions.len(),
            2,
            "Should have two equal-length capture options"
        );
    }

    // ========== King Movement Tests ==========

    #[test]
    fn king_can_slide_multiple_squares() {
        // White king at D4, should be able to move in all 4 directions multiple squares
        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[],
            &[Square::D4], // D4 is a king
        );
        let actions = board.actions();

        // King at D4 can move:
        // Left: C4, B4, A4 (3 moves)
        // Right: E4, F4, G4, H4 (4 moves)
        // Up: D5, D6, D7, D8 (4 moves)
        // Down: D3, D2, D1 (3 moves)
        // Total: 14 moves
        assert_eq!(actions.len(), 14, "King at D4 should have 14 moves");
    }

    #[test]
    fn king_blocked_by_friendly_piece() {
        // White king at D4, white pawn at D6
        let board = Board::from_squares(
            Team::White,
            &[Square::D4, Square::D6],
            &[],
            &[Square::D4], // D4 is a king
        );
        let actions = board.actions();

        // King can move up to D5 only (blocked by D6)
        // Left: 3, Right: 4, Up: 1 (blocked at D6), Down: 3
        // Total: 11 moves (king) + pawn moves
        let king_up_moves: Vec<_> = actions
            .iter()
            .filter(|a| {
                let dest = a.delta.pieces[Team::White.to_usize()] & !Square::D4.to_mask();
                dest == Square::D5.to_mask()
            })
            .collect();
        assert_eq!(
            king_up_moves.len(),
            1,
            "King should only be able to move to D5 going up"
        );
    }

    // ========== King Capture Tests ==========

    #[test]
    fn king_can_capture_from_distance() {
        // White king at A4, black pawn at D4, king can land on E4, F4, G4, or H4
        let board = Board::from_squares(
            Team::White,
            &[Square::A4],
            &[Square::D4],
            &[Square::A4], // A4 is a king
        );
        let actions = board.actions();

        // King captures D4 and can land on E4, F4, G4, H4 (4 options)
        assert_eq!(
            actions.len(),
            4,
            "King should have 4 landing options after capture"
        );
        for action in &actions {
            assert_eq!(
                action.delta.pieces[Team::Black.to_usize()],
                Square::D4.to_mask(),
                "Should capture D4"
            );
        }
    }

    #[test]
    fn king_multi_direction_capture() {
        // White king at D4, can capture in multiple directions
        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[Square::B4, Square::D6],
            &[Square::D4],
        );
        let actions = board.actions();

        // Should have captures in both directions
        assert!(
            actions.len() >= 2,
            "King should be able to capture in multiple directions"
        );
    }

    // ========== Pawn Movement Tests ==========

    #[test]
    fn white_pawn_moves_forward_and_sideways() {
        let board = Board::from_squares(Team::White, &[Square::D4], &[], &[]);
        let actions = board.actions();

        // D4 pawn can move to C4 (left), E4 (right), D5 (up)
        assert_eq!(actions.len(), 3, "Pawn at D4 should have 3 moves");
    }

    #[test]
    fn black_pawn_moves_forward_and_sideways() {
        let board = Board::from_squares(Team::Black, &[], &[Square::D5], &[]);
        let actions = board.actions();

        // D5 black pawn can move to C5 (left), E5 (right), D4 (down)
        assert_eq!(actions.len(), 3, "Black pawn at D5 should have 3 moves");
    }

    #[test]
    fn pawn_at_edge_has_fewer_moves() {
        let board = Board::from_squares(Team::White, &[Square::A4], &[], &[]);
        let actions = board.actions();

        // A4 pawn can move to B4 (right), A5 (up) - cannot go left
        assert_eq!(actions.len(), 2, "Pawn at A4 should have 2 moves");
    }

    // ========== Promotion Tests ==========

    #[test]
    fn pawn_promotes_on_last_rank() {
        let board = Board::from_squares(Team::White, &[Square::D7], &[], &[]);
        let actions = board.actions();

        // Find the move to D8 and verify it promotes
        let promotion_action = actions
            .iter()
            .find(|a| a.delta.pieces[Team::White.to_usize()] & Square::D8.to_mask() != 0);

        assert!(promotion_action.is_some(), "Should be able to move to D8");
        let action = promotion_action.unwrap();
        assert_ne!(
            action.delta.kings & Square::D8.to_mask(),
            0,
            "Pawn should promote to king at D8"
        );
    }

    #[test]
    fn pawn_promotes_during_capture() {
        // White pawn at C7, captures black pawn at C8... wait, that's impossible
        // Let's do: White pawn at B6, captures black pawn at B7, lands on B8
        let board = Board::from_squares(Team::White, &[Square::B6], &[Square::B7], &[]);
        let actions = board.actions();

        assert_eq!(actions.len(), 1, "Should have one capture");
        let action = &actions[0];
        assert_ne!(
            action.delta.kings & Square::B8.to_mask(),
            0,
            "Pawn should promote after capture landing on B8"
        );
    }

    // ========== No Actions Tests ==========

    #[test]
    fn no_pieces_no_actions() {
        let board = Board::from_squares(Team::White, &[], &[Square::D4], &[]);
        let actions = board.actions();
        assert!(actions.is_empty(), "No friendly pieces means no actions");
    }

    #[test]
    fn completely_blocked_pawn_can_capture() {
        // White pawn at B2, surrounded by black pawns
        // It's blocked for moves but can capture!
        let board = Board::from_squares(
            Team::White,
            &[Square::B2],
            &[Square::A2, Square::B3, Square::C2],
            &[],
        );
        let actions = board.actions();
        // The pawn can capture A2, B3, or C2
        assert!(
            !actions.is_empty(),
            "Surrounded pawn can capture adjacent enemies"
        );
        for action in &actions {
            assert_ne!(
                action.delta.pieces[Team::Black.to_usize()],
                0,
                "Actions should be captures"
            );
        }
    }

    #[test]
    fn truly_blocked_pawn() {
        // White pawn at B2, surrounded by friendly pawns (can't capture friendlies)
        let board = Board::from_squares(
            Team::White,
            &[Square::B2, Square::A2, Square::B3, Square::C2],
            &[Square::H8],
            &[],
        );
        let actions = board.actions();
        // B2 is blocked, but A2, B3, C2 can move
        // A2: B2 blocked, A3 available = 1 move
        // B3: A3 available, C3 available, B4 available = 3 moves
        // C2: B2 blocked, D2 available, C3 available = 2 moves
        // Total moves should not include B2 moving anywhere
        let b2_moves: Vec<_> = actions
            .iter()
            .filter(|a| {
                let delta = a.delta.pieces[Team::White.to_usize()];
                delta & Square::B2.to_mask() != 0
            })
            .collect();
        assert!(b2_moves.is_empty(), "B2 pawn should have no moves");
    }

    // ========== Chain Capture Tests ==========

    #[test]
    fn pawn_chain_capture_two_pieces() {
        // White pawn at A4
        // Can capture A5 -> A6, then B6 -> C6
        let board = Board::from_squares(Team::White, &[Square::A4], &[Square::A5, Square::B6], &[]);
        let actions = board.actions();

        // Should capture both pieces in chain
        assert!(!actions.is_empty(), "Should have capture chain");
        for action in &actions {
            let capture_count = action.delta.pieces[Team::Black.to_usize()].count_ones();
            assert_eq!(capture_count, 2, "Should capture 2 pieces in chain");
        }
    }

    #[test]
    fn pawn_chain_capture_three_pieces() {
        // White pawn at A2
        // Chain: A2 captures A3->A4, then B4->C4, then C5->C6
        let board = Board::from_squares(
            Team::White,
            &[Square::A2],
            &[Square::A3, Square::B4, Square::C5],
            &[],
        );
        let actions = board.actions();

        assert!(!actions.is_empty(), "Should have capture chain");
        for action in &actions {
            let capture_count = action.delta.pieces[Team::Black.to_usize()].count_ones();
            assert_eq!(capture_count, 3, "Should capture 3 pieces in chain");
        }
    }

    #[test]
    fn king_chain_capture() {
        // White king at A4
        // Can capture C4 -> E4, then E6 -> E8
        let board = Board::from_squares(
            Team::White,
            &[Square::A4],
            &[Square::C4, Square::E6],
            &[Square::A4],
        );
        let actions = board.actions();

        // Should have chain captures
        assert!(!actions.is_empty(), "King should have capture options");
        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap();
        assert_eq!(max_captures, 2, "King should capture 2 pieces in chain");
    }

    // ========== King Edge Cases ==========

    #[test]
    fn king_cannot_pass_through_friendly() {
        // White king at A4, white pawn at C4
        // King should not be able to move past C4 to the right
        let board = Board::from_squares(
            Team::White,
            &[Square::A4, Square::C4],
            &[Square::H8],
            &[Square::A4],
        );
        let actions = board.actions();

        // King should only reach B4 going right (blocked by C4)
        let right_moves: Vec<_> = actions
            .iter()
            .filter(|a| {
                let src = Square::A4.to_mask();
                let delta = a.delta.pieces[Team::White.to_usize()];
                // Check if it's a king move (src is toggled)
                delta & src != 0 && {
                    let dest = delta & !src;
                    // Check if destination is to the right of A4 (same row, higher column)
                    // SAFETY: dest is a single bit (action moves one piece).
                    let dest_sq = unsafe { Square::from_mask(dest) };
                    dest_sq.row() == 3 && dest_sq.column() > 0
                }
            })
            .collect();

        assert_eq!(
            right_moves.len(),
            1,
            "King should only reach B4 going right"
        );
    }

    #[test]
    fn king_cannot_jump_over_hostile_without_capturing() {
        // White king at A4, black pawn at C4
        // King cannot move to D4+ without capturing
        let board = Board::from_squares(Team::White, &[Square::A4], &[Square::C4], &[Square::A4]);
        let actions = board.actions();

        // All actions that go past C4 must be captures
        for action in &actions {
            let dest = action.delta.pieces[Team::White.to_usize()] & !Square::A4.to_mask();
            if dest != 0 {
                // SAFETY: dest is a single bit (action moves one piece).
                let dest_sq = unsafe { Square::from_mask(dest) };
                if dest_sq.row() == 3 && dest_sq.column() >= 3 {
                    // Past C4, must be a capture
                    assert_ne!(
                        action.delta.pieces[Team::Black.to_usize()],
                        0,
                        "Moving past hostile must be a capture"
                    );
                }
            }
        }
    }

    // ========== Black Team Tests ==========

    #[test]
    fn black_pawn_captures() {
        let board = Board::from_squares(Team::Black, &[Square::A1], &[Square::D5], &[]);
        let actions = board.actions();

        // Black pawn at D5 can move down/left/right
        // D5 -> D4 (down), C5 (left), E5 (right)
        assert_eq!(actions.len(), 3, "Black pawn at D5 should have 3 moves");
    }

    #[test]
    fn black_king_movement() {
        let board = Board::from_squares(Team::Black, &[Square::A1], &[Square::D4], &[Square::D4]);
        let actions = board.actions();

        // Black king at D4 should have 14 moves (same as white king)
        assert_eq!(actions.len(), 14, "Black king at D4 should have 14 moves");
    }

    // ========== Multiple Pieces Tests ==========

    #[test]
    fn multiple_pieces_all_can_move() {
        let board = Board::from_squares(Team::White, &[Square::A4, Square::H4], &[Square::D8], &[]);
        let actions = board.actions();

        // A4: B4 (right), A5 (up) = 2 moves
        // H4: G4 (left), H5 (up) = 2 moves
        // Total: 4 moves
        assert_eq!(actions.len(), 4, "Both pawns should contribute moves");
    }

    #[test]
    fn multiple_pieces_one_must_capture() {
        // A4 can capture, H4 cannot
        // Only A4's capture should be returned
        let board = Board::from_squares(Team::White, &[Square::A4, Square::H4], &[Square::A5], &[]);
        let actions = board.actions();

        assert_eq!(actions.len(), 1, "Only capture should be returned");
        assert_ne!(
            actions[0].delta.pieces[Team::Black.to_usize()],
            0,
            "Action should be a capture"
        );
    }

    // ========== Promotion Edge Cases ==========

    #[test]
    fn black_pawn_promotes_on_row_1() {
        let board = Board::from_squares(Team::Black, &[Square::H8], &[Square::D2], &[]);
        let actions = board.actions();

        // Find move to D1
        let promotion = actions
            .iter()
            .find(|a| a.delta.pieces[Team::Black.to_usize()] & Square::D1.to_mask() != 0);

        assert!(promotion.is_some(), "Should be able to move to D1");
        assert_ne!(
            promotion.unwrap().delta.kings & Square::D1.to_mask(),
            0,
            "Black pawn should promote at D1"
        );
    }

    #[test]
    fn pawn_capture_leads_to_promotion() {
        // White pawn at B6, captures B7, lands at B8 and promotes
        let board = Board::from_squares(Team::White, &[Square::B6], &[Square::B7], &[]);
        let actions = board.actions();

        assert_eq!(actions.len(), 1, "Should have one capture");
        let action = &actions[0];

        // Verify capture happened
        assert_eq!(
            action.delta.pieces[Team::Black.to_usize()],
            Square::B7.to_mask(),
            "Should capture B7"
        );

        // Verify promotion happened
        assert_ne!(
            action.delta.kings & Square::B8.to_mask(),
            0,
            "Pawn should promote at B8"
        );
    }

    // ========== Corner Cases ==========

    #[test]
    fn pawn_in_corner() {
        // White pawn at A8 (promoted position - should be king, but let's test as pawn)
        // Actually A8 is promotion row for white, so it would be a king
        // Let's use H1 for black
        let board = Board::from_squares(
            Team::Black,
            &[Square::A8],
            &[Square::H1],
            &[Square::H1], // H1 is a king
        );
        let actions = board.actions();

        // Black king at H1: can move G1 (left), H2 (up)
        // But H1 is corner, so limited moves
        // Left: G1, F1, E1, D1, C1, B1, A1 = 7 moves
        // Up: H2, H3, H4, H5, H6, H7, H8 = 7 moves
        assert_eq!(actions.len(), 14, "King at H1 should have 14 moves");
    }

    // ============================================================================
    // COMPREHENSIVE RULE TESTS
    // These tests cover all rules from rules.md to ensure complete coverage
    // ============================================================================

    // ========== 180-DEGREE TURN RESTRICTION TESTS ==========
    // Rule: During a multiple capture sequence, a piece cannot make a 180-degree turn

    #[test]
    fn king_cannot_reverse_up_down_during_capture() {
        // White king at D1, black pieces at D3 and D5
        // King captures D3, landing at D4
        // From D4, the king should NOT be able to capture D5 going up then reverse to go down
        // Actually this tests: after going UP to capture, can't immediately go DOWN

        // Setup: King at D1, enemies at D3 (capture going up, land D4-D7)
        // Then enemy at D2 would require going down (180° turn)
        // But D2 is below D1, so let's set up differently:

        // King at D4, enemies at D2 and D6
        // If king captures D6 (going up), lands at D7 or D8
        // From there, D2 requires going down - but it's a different line

        // Better test: King at D4, enemy at D6, enemy at D3
        // Capture D6 (up) -> land D7
        // From D7, enemy D3 is far below, would need to go DOWN (opposite of UP)
        // This should be blocked by 180° rule... but wait, D3 from D7 is a long capture

        // Simplest test: King at D4, enemies at D6 and D8 placed so after first capture
        // the only continuation would require reversing
        // Actually: King at D4, enemy at D6. After capture, land at D7.
        // Now if there's an enemy at D5 that we skipped... no, we capture D6.

        // Real test: King D4, enemies at D2 (below) and D6 (above)
        // King has two 1-capture options. But if setup allows chain that reverses, it should fail.

        // Setup for reversal test:
        // King at D4, enemy at D6, enemy at D4... wait can't overlap
        //
        // King at D5, enemies at D3 and D7
        // Capture D7 going up, land at D8
        // From D8, capture D3 going down? D3 is at row 2, D8 is row 7
        // That's a valid long-range capture going DOWN
        // After going UP, going DOWN is 180° - should be blocked!

        let board = Board::from_squares(
            Team::White,
            &[Square::D5],
            &[Square::D3, Square::D7],
            &[Square::D5], // D5 is a king
        );
        let actions = board.actions();

        // King can capture D3 (going down) or D7 (going up)
        // Each is a single capture - no chain should form because reversing is blocked
        // So we should have multiple single-capture actions, not a 2-capture chain

        for action in &actions {
            let capture_count = action.delta.pieces[Team::Black.to_usize()].count_ones();
            assert_eq!(
                capture_count, 1,
                "King should NOT chain captures that require 180° turn (up then down)"
            );
        }
    }

    #[test]
    fn king_cannot_reverse_left_right_during_capture() {
        // King at D4, enemies at B4 (left) and F4 (right)
        // After capturing B4 going left (land at A4),
        // capturing F4 would require going right (180° turn) - should be blocked

        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[Square::B4, Square::F4],
            &[Square::D4], // D4 is a king
        );
        let actions = board.actions();

        // Should have two single-capture options, NOT a 2-capture chain
        for action in &actions {
            let capture_count = action.delta.pieces[Team::Black.to_usize()].count_ones();
            assert_eq!(
                capture_count, 1,
                "King should NOT chain captures that require 180° turn (left then right)"
            );
        }
    }

    #[test]
    fn king_can_turn_90_degrees_during_capture() {
        // King at D4, enemy at D6 (up), enemy at F6 (right from D6 area)
        // Capture D6 going up, land at D7
        // From D7, capture F7 going right (90° turn, not 180°) - should be ALLOWED

        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[Square::D6, Square::F7],
            &[Square::D4],
        );
        let actions = board.actions();

        // Should have a 2-capture chain: D6 (up) then F7 (right)
        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap_or(0);

        assert_eq!(
            max_captures, 2,
            "King should be able to chain captures with 90° turn"
        );
    }

    #[test]
    fn king_180_restriction_complex_scenario() {
        // More complex: King at A1, enemies arranged in an L-shape
        // A1 -> captures A3 (up) -> lands A4-A8
        // From A5, could capture C5 (right) -> lands D5-H5
        // From E5, could capture E3 (down) - this is 90° from right, allowed

        let board = Board::from_squares(
            Team::White,
            &[Square::A1],
            &[Square::A3, Square::C5, Square::E3],
            &[Square::A1],
        );
        let actions = board.actions();

        // Should be able to do 3-capture chain with 90° turns
        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap_or(0);

        assert_eq!(
            max_captures, 3,
            "King should chain 3 captures with 90° turns"
        );
    }

    /// Black king at D4 can capture D2 (South) or D6 (North), but NOT both
    /// since that would require a 180° turn.
    #[test]
    fn king_180_turn_prohibited_vertical() {
        let board = Board::from_squares(
            Team::Black,
            &[Square::D2, Square::D6],
            &[Square::D4],
            &[Square::D4, Square::D6],
        );

        let actions = board.actions();

        assert!(!actions.is_empty());
        for action in &actions {
            assert_eq!(
                action.capture_count(Team::Black),
                1,
                "180° turn should prevent chaining North->South or South->North captures"
            );
        }
    }

    /// Black king at D4 can capture B4 (West) or F4 (East), but NOT both
    /// since that would require a 180° turn.
    #[test]
    fn king_180_turn_prohibited_horizontal() {
        let board = Board::from_squares(
            Team::Black,
            &[Square::B4, Square::F4],
            &[Square::D4],
            &[Square::D4, Square::F4],
        );

        let actions = board.actions();

        assert!(!actions.is_empty());
        for action in &actions {
            assert_eq!(
                action.capture_count(Team::Black),
                1,
                "180° turn should prevent chaining East->West or West->East captures"
            );
        }
    }

    // ========== IMMEDIATE PIECE REMOVAL TESTS ==========
    // Rule: Captured pieces are removed immediately, allowing crossing the same square

    #[test]
    fn can_cross_captured_square_pawn() {
        // This tests that after capturing a piece, the square becomes available
        // White pawn at A4, enemies at A5, A7
        // Capture A5 -> land A6
        // Capture A7 -> land A8 (crosses through where A5 was)
        // Wait, A6 doesn't cross A5's square...

        // Better: White pawn at A2, enemies at A3 and B4
        // Capture A3 -> land A4
        // Capture B4 -> land C4
        // This doesn't cross A3's square either

        // For pawns, crossing the same square is hard to set up because they jump 2 squares
        // The "crossing same square" benefit is more relevant for kings
        // Let's test with a king instead
        let board = Board::from_squares(Team::White, &[Square::A2], &[Square::A3, Square::B4], &[]);
        let actions = board.actions();

        // Should capture both in chain
        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap_or(0);

        assert_eq!(max_captures, 2, "Pawn should chain 2 captures");
    }

    #[test]
    fn king_can_cross_captured_square() {
        // King at A4, enemies at C4 and E4 (not on promotion row)
        // Capture C4 -> can land at D4
        // From D4, capture E4 -> land F4, G4, or H4
        // This uses the path through where C4 was

        let board = Board::from_squares(
            Team::White,
            &[Square::A4],
            &[Square::C4, Square::E4],
            &[Square::A4],
        );
        let actions = board.actions();

        // Should have 2-capture chains
        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap_or(0);

        assert_eq!(
            max_captures, 2,
            "King should be able to chain captures (immediate removal allows path)"
        );
    }

    #[test]
    fn king_complex_crossing_pattern() {
        // Create a scenario where the king must cross a previously captured square
        // King at A4, enemies at C4 and A6
        // Option 1: Capture C4 (right) -> land D4-H4
        // Option 2: Capture A6 (up) -> land A7-A8
        //
        // For crossing: King at D4, enemies at D2 and D6 and F4
        // Capture D2 (down) -> land D1
        // Can't continue (D1 is edge)
        //
        // Better: King at D4, enemy at B4, enemy at B6
        // Capture B4 (left) -> land A4
        // From A4, capture B6? B6 is not adjacent diagonally, let's think orthogonally
        // From A4, enemy at A6 would be capturable (going up)

        // King at D4, enemies at B4 (capture left), B2 (would need to pass through B4's position)
        // Capture B4 -> land A4
        // From A4, enemy at B2 is diagonal - invalid
        //
        // King at D4, enemies at B4 and D2
        // Capture B4 (left) -> land A4
        // From A4, capture D2? D2 is not directly accessible from A4

        // Simpler: King at E4, enemies at C4 and C2
        // Capture C4 (left) -> land A4 or B4
        // From B4, capture C2 (right-down)? No, orthogonal only
        // From A4, capture C2? Not directly possible

        // The crossing scenario needs more thought. Let's test basic immediate removal:
        let board = Board::from_squares(
            Team::White,
            &[Square::A4],
            &[Square::C4, Square::E4, Square::G4],
            &[Square::A4],
        );
        let actions = board.actions();

        // King can capture C4 -> land D4
        // From D4, capture E4 -> land F4
        // From F4, capture G4 -> land H4
        // This is a 3-capture chain going right continuously (no crossing needed)
        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap_or(0);

        assert_eq!(max_captures, 3, "King should chain 3 captures in a line");
    }

    // ========== MID-SEQUENCE PROMOTION TESTS ==========
    // Tests for how promotion works during capture sequences

    #[test]
    fn pawn_promotes_at_end_of_capture_sequence() {
        // White pawn at D6, enemies at D7 and E8
        // Capture D7 -> land D8 (promotion row)
        // If more captures available from D8 as a king, it depends on interpretation
        // Current implementation: continues as pawn (Interpretation B)

        let board = Board::from_squares(Team::White, &[Square::D6], &[Square::D7], &[]);
        let actions = board.actions();

        assert_eq!(actions.len(), 1, "Should have one capture");
        let action = &actions[0];

        // Verify lands on D8 and promotes
        let dest = action.delta.pieces[Team::White.to_usize()] & !Square::D6.to_mask();
        assert_eq!(dest, Square::D8.to_mask(), "Should land on D8");
        assert_ne!(
            action.delta.kings & Square::D8.to_mask(),
            0,
            "Should promote to king at D8"
        );
    }

    #[test]
    fn pawn_captures_and_promotes_when_ending_on_back_row() {
        // White pawn at D6, enemy at D7
        // Capture D7 -> land D8 (promotion row)
        // Since no more captures available, pawn promotes

        let board = Board::from_squares(Team::White, &[Square::D6], &[Square::D7], &[]);
        let actions = board.actions();

        assert_eq!(actions.len(), 1, "Should have one capture");
        let action = &actions[0];

        // Verify lands on D8 and promotes
        let dest = action.delta.pieces[Team::White.to_usize()] & !Square::D6.to_mask();
        assert_eq!(dest, Square::D8.to_mask(), "Should land on D8");
        assert_ne!(
            action.delta.kings & Square::D8.to_mask(),
            0,
            "Should promote to king at D8"
        );
    }

    #[test]
    fn pawn_continues_capturing_from_promotion_row() {
        // Rule: A pawn that lands on the promotion row mid-capture does NOT
        // become a king until the turn ends. It must continue capturing as a pawn.
        //
        // Setup: White pawn at D6, enemies at D7 and C8
        // - Pawn captures D7 -> lands on D8 (promotion row)
        // - Pawn should continue capturing C8 -> lands on B8 (as a PAWN)
        // - Only NOW does the pawn promote to king (sequence ended on promotion row)

        let board = Board::from_squares(Team::White, &[Square::D6], &[Square::D7, Square::C8], &[]);
        let actions = board.actions();

        // Should have exactly one action: the 2-capture chain
        // (single captures are filtered out by max capture rule)
        assert_eq!(actions.len(), 1, "Should have one 2-capture action");
        let action = &actions[0];

        // Verify captures both D7 and C8
        let captures = action.delta.pieces[Team::Black.to_usize()];
        assert_eq!(
            captures,
            Square::D7.to_mask() | Square::C8.to_mask(),
            "Should capture both D7 and C8"
        );

        // Verify pawn ends on B8 (2 squares left from D8)
        let final_pos = action.delta.pieces[Team::White.to_usize()] & !Square::D6.to_mask();
        assert_eq!(final_pos, Square::B8.to_mask(), "Should end on B8");

        // Verify pawn promotes at B8 (final position on promotion row)
        assert_ne!(
            action.delta.kings & Square::B8.to_mask(),
            0,
            "Should promote to king at B8"
        );
    }

    #[test]
    fn pawn_chain_capture_ending_on_promotion_row() {
        // White pawn at A4, enemies at A5 and B6
        // Capture A5 -> land A6
        // Capture B6 -> land C6
        // Note: This doesn't involve promotion row, but tests chain captures work

        let board = Board::from_squares(Team::White, &[Square::A4], &[Square::A5, Square::B6], &[]);
        let actions = board.actions();

        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap_or(0);

        assert_eq!(max_captures, 2, "Should capture both in chain");
    }

    // NOTE: Mid-capture promotion tests are not included because the current
    // implementation doesn't support pawns continuing to capture after landing
    // on the promotion row. The Action::new_capture_as_pawn function panics
    // if source is on promotion row. This is documented in implementation_status.md.

    // ========== MAXIMUM CAPTURE RULE TESTS ==========
    // Rule: Must capture maximum number of pieces; can choose among equals

    #[test]
    fn max_capture_three_vs_two() {
        // Setup where one path captures 3, another captures 2
        // White pawn at A2
        // Path 1: A2 captures A3->A4, then B4->C4 (2 captures)
        // Path 2: A2 captures A3->A4, then B4->C4, then C5->C6 (3 captures)

        let board = Board::from_squares(
            Team::White,
            &[Square::A2],
            &[Square::A3, Square::B4, Square::C5],
            &[],
        );
        let actions = board.actions();

        // All returned actions should be 3 captures (maximum)
        for action in &actions {
            let count = action.delta.pieces[Team::Black.to_usize()].count_ones();
            assert_eq!(count, 3, "Only 3-capture sequences should be returned");
        }
    }

    #[test]
    fn max_capture_equal_length_all_returned() {
        // Multiple paths with same capture count should all be available
        // White pawn at D4
        // Can capture: D5->D6 (1 capture up) OR C4->B4 (1 capture left) OR E4->F4 (1 capture right)

        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[Square::D5, Square::C4, Square::E4],
            &[],
        );
        let actions = board.actions();

        // Should have 3 capture options, all length 1
        assert_eq!(
            actions.len(),
            3,
            "Should have 3 equal-length capture options"
        );
        for action in &actions {
            let count = action.delta.pieces[Team::Black.to_usize()].count_ones();
            assert_eq!(count, 1, "All captures should be length 1");
        }
    }

    #[test]
    fn max_capture_king_vs_pawn_count_equally() {
        // Capturing a king counts the same as capturing a pawn
        // D4 pawn, D5 king (1 capture), C4+B5 pawns (2 captures via chain)
        // Path 1: capture D5 (1 king)
        // Path 2: capture C4->B4, then B5->B6 (2 pawns)
        // Should prefer 2 captures even though path 1 captures a king
        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[Square::D5, Square::C4, Square::B5],
            &[Square::D5], // D5 is a king
        );
        let actions = board.actions();

        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap_or(0);

        assert_eq!(
            max_captures, 2,
            "Should prefer 2-pawn capture over 1-king capture"
        );
    }

    // ========== MEN MOVEMENT TESTS ==========
    // Rule: Men move forward, left, right only (no backward, no diagonal)

    #[test]
    fn white_pawn_cannot_move_backward() {
        // White pawn at D4, space behind at D3
        // Should NOT be able to move to D3
        let board = Board::from_squares(Team::White, &[Square::D4], &[Square::H8], &[]);
        let actions = board.actions();

        let backward_move = actions.iter().find(|a| {
            let dest = a.delta.pieces[Team::White.to_usize()] & !Square::D4.to_mask();
            dest == Square::D3.to_mask()
        });

        assert!(
            backward_move.is_none(),
            "White pawn should not move backward to D3"
        );
    }

    #[test]
    fn black_pawn_cannot_move_backward() {
        // Black pawn at D5, space behind at D6
        // Should NOT be able to move to D6
        let board = Board::from_squares(Team::Black, &[Square::H1], &[Square::D5], &[]);
        let actions = board.actions();

        let backward_move = actions.iter().find(|a| {
            let dest = a.delta.pieces[Team::Black.to_usize()] & !Square::D5.to_mask();
            dest == Square::D6.to_mask()
        });

        assert!(
            backward_move.is_none(),
            "Black pawn should not move backward to D6"
        );
    }

    #[test]
    fn pawn_cannot_move_diagonally() {
        // White pawn at D4, all diagonal squares empty
        let board = Board::from_squares(Team::White, &[Square::D4], &[Square::H8], &[]);
        let actions = board.actions();

        let diagonal_squares = [Square::C3, Square::C5, Square::E3, Square::E5];
        for sq in &diagonal_squares {
            let diag_move = actions.iter().find(|a| {
                let dest = a.delta.pieces[Team::White.to_usize()] & !Square::D4.to_mask();
                dest == sq.to_mask()
            });
            assert!(
                diag_move.is_none(),
                "Pawn should not move diagonally to {sq:?}"
            );
        }
    }

    #[test]
    fn pawn_moves_exactly_one_square() {
        // Pawn should only move 1 square, not 2+
        let board = Board::from_squares(Team::White, &[Square::D4], &[Square::H8], &[]);
        let actions = board.actions();

        // Valid destinations: C4, E4, D5 (distance 1)
        // Invalid: B4, F4, D6 (distance 2)
        let valid_dests = [Square::C4, Square::E4, Square::D5];
        let invalid_dests = [Square::B4, Square::F4, Square::D6];

        for sq in &valid_dests {
            let found = actions.iter().any(|a| {
                let dest = a.delta.pieces[Team::White.to_usize()] & !Square::D4.to_mask();
                dest == sq.to_mask()
            });
            assert!(found, "Pawn should be able to move to {sq:?}");
        }

        for sq in &invalid_dests {
            let found = actions.iter().any(|a| {
                let dest = a.delta.pieces[Team::White.to_usize()] & !Square::D4.to_mask();
                dest == sq.to_mask()
            });
            assert!(!found, "Pawn should NOT move 2 squares to {sq:?}");
        }
    }

    // ========== KING MOVEMENT TESTS ==========
    // Rule: Kings move any distance orthogonally (like a rook)

    #[test]
    fn king_moves_multiple_squares_all_directions() {
        // King at D4 in center, should reach all squares in its row and column
        let board = Board::from_squares(Team::White, &[Square::D4], &[Square::H8], &[Square::D4]);
        let actions = board.actions();

        // Row 4: A4, B4, C4, E4, F4, G4, H4 (7 squares)
        // Column D: D1, D2, D3, D5, D6, D7, D8 (7 squares)
        // Total: 14 moves
        assert_eq!(actions.len(), 14, "King at D4 should have 14 moves");
    }

    #[test]
    fn king_cannot_move_diagonally() {
        let board = Board::from_squares(Team::White, &[Square::D4], &[Square::H8], &[Square::D4]);
        let actions = board.actions();

        let diagonal_squares = [
            Square::A1,
            Square::B2,
            Square::C3,
            Square::E5,
            Square::F6,
            Square::G7,
            Square::A7,
            Square::B6,
            Square::C5,
            Square::E3,
            Square::F2,
            Square::G1,
        ];

        for sq in &diagonal_squares {
            let diag_move = actions.iter().find(|a| {
                let dest = a.delta.pieces[Team::White.to_usize()] & !Square::D4.to_mask();
                dest == sq.to_mask()
            });
            assert!(
                diag_move.is_none(),
                "King should not move diagonally to {sq:?}"
            );
        }
    }

    // ========== MEN CAPTURE TESTS ==========
    // Rule: Men capture forward, left, right (no backward, no diagonal)

    #[test]
    fn white_pawn_cannot_capture_backward() {
        // White pawn at D4, black pawn at D3 (behind)
        // Should NOT be able to capture backward
        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[Square::D3, Square::D5], // D3 behind, D5 in front
            &[],
        );
        let actions = board.actions();

        // Should only capture D5 (forward), not D3 (backward)
        assert_eq!(actions.len(), 1, "Should have only 1 capture");
        assert_eq!(
            actions[0].delta.pieces[Team::Black.to_usize()],
            Square::D5.to_mask(),
            "Should capture D5 (forward), not D3 (backward)"
        );
    }

    #[test]
    fn black_pawn_cannot_capture_backward() {
        // Black pawn at D5, white pawn at D6 (behind for black)
        let board = Board::from_squares(
            Team::Black,
            &[Square::D6, Square::D4], // D6 behind, D4 in front
            &[Square::D5],
            &[],
        );
        let actions = board.actions();

        // Should only capture D4 (forward for black = down), not D6 (backward)
        assert_eq!(actions.len(), 1, "Should have only 1 capture");
        assert_eq!(
            actions[0].delta.pieces[Team::White.to_usize()],
            Square::D4.to_mask(),
            "Should capture D4 (forward for black), not D6 (backward)"
        );
    }

    #[test]
    fn pawn_cannot_capture_diagonally() {
        // White pawn at D4, black pawns at diagonal positions
        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[Square::C3, Square::C5, Square::E3, Square::E5],
            &[],
        );
        let actions = board.actions();

        // Should have NO captures (all enemies are diagonal)
        // Without captures, should have moves instead
        for action in &actions {
            assert_eq!(
                action.delta.pieces[Team::Black.to_usize()],
                0,
                "Should not capture diagonal enemies"
            );
        }
    }

    // ========== KING CAPTURE TESTS ==========
    // Rule: Kings use flying capture (any distance, land anywhere beyond)

    #[test]
    fn king_capture_from_distance() {
        // King at A4, enemy at E4 (distance 4)
        // Should be able to capture and land on F4, G4, or H4
        let board = Board::from_squares(Team::White, &[Square::A4], &[Square::E4], &[Square::A4]);
        let actions = board.actions();

        // Should have 3 capture options (landing on F4, G4, H4)
        assert_eq!(actions.len(), 3, "King should have 3 landing options");

        for action in &actions {
            assert_eq!(
                action.delta.pieces[Team::Black.to_usize()],
                Square::E4.to_mask(),
                "All captures should take E4"
            );
        }
    }

    #[test]
    fn king_cannot_capture_two_in_line() {
        // King at A4, enemies at C4 and E4 (two in a row)
        // Should NOT be able to jump both in one move
        let board = Board::from_squares(
            Team::White,
            &[Square::A4],
            &[Square::C4, Square::E4],
            &[Square::A4],
        );
        let actions = board.actions();

        // Can capture C4 (landing D4), or start a chain
        // After capturing C4, E4 is still there to capture
        // This should be a 2-capture chain
        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap_or(0);

        assert_eq!(
            max_captures, 2,
            "Should capture both in chain, not single jump"
        );
    }

    // ========== MANDATORY CAPTURE TESTS ==========
    // Rule: If capture is available, must capture (no moves allowed)

    #[test]
    fn must_capture_when_available() {
        // White pawn at D4, can move OR capture
        // Enemy at D5 makes capture available
        let board = Board::from_squares(Team::White, &[Square::D4], &[Square::D5], &[]);
        let actions = board.actions();

        // All actions must be captures
        for action in &actions {
            assert_ne!(
                action.delta.pieces[Team::Black.to_usize()],
                0,
                "Must capture when capture is available"
            );
        }
    }

    #[test]
    fn moves_allowed_when_no_capture() {
        // White pawn at D4, no capturable enemies
        let board = Board::from_squares(
            Team::White,
            &[Square::D4],
            &[Square::H8], // Far away enemy
            &[],
        );
        let actions = board.actions();

        // All actions should be moves (non-captures)
        for action in &actions {
            assert_eq!(
                action.delta.pieces[Team::Black.to_usize()],
                0,
                "Should be moves, not captures"
            );
        }
        assert!(!actions.is_empty(), "Should have moves available");
    }

    // ========== DRAW CONDITION TESTS ==========
    // Note: Most draw conditions are in board.rs status() tests

    #[test]
    fn one_piece_each_is_draw() {
        // One white piece vs one black piece
        let board = Board::from_squares(Team::White, &[Square::A1], &[Square::H8], &[]);
        assert_eq!(
            board.status(),
            GameStatus::Draw,
            "One piece each should be draw"
        );
    }

    #[test]
    fn one_king_each_is_draw() {
        let board = Board::from_squares(
            Team::White,
            &[Square::A1],
            &[Square::H8],
            &[Square::A1, Square::H8],
        );
        assert_eq!(
            board.status(),
            GameStatus::Draw,
            "One king each should be draw"
        );
    }

    #[test]
    fn king_vs_pawn_is_draw() {
        // Current implementation: 1 vs 1 is always draw regardless of type
        let board = Board::from_squares(
            Team::White,
            &[Square::A1],
            &[Square::H8],
            &[Square::A1], // White has king, black has pawn
        );
        assert_eq!(
            board.status(),
            GameStatus::Draw,
            "King vs pawn (1v1) is draw in current implementation"
        );
    }

    // ========== WIN CONDITION TESTS ==========

    #[test]
    fn no_pieces_means_loss() {
        let board = Board::from_squares(Team::White, &[], &[Square::D4], &[]);
        assert_eq!(
            board.status(),
            GameStatus::Won(Team::Black),
            "No white pieces means black wins"
        );
    }

    #[test]
    fn blocked_means_loss() {
        // White pawns at A2 and A3, completely surrounded by enemies
        // such that all captures are blocked (landing squares occupied)
        // This is the same setup as board.rs::status_friendly_blocked
        let board = Board::from_squares(
            Team::White,
            &[Square::A2, Square::A3],
            &[
                Square::A4, // Blocks A3's forward capture landing
                Square::A5, // Extra blocker
                Square::B2, // Adjacent to A2 (capturable but landing blocked)
                Square::B3, // Adjacent to A3 (capturable but landing blocked)
                Square::C2, // Blocks B2 capture landing
                Square::C3, // Blocks B3 capture landing
            ],
            &[],
        );
        // A2: left=edge, forward=A3(friendly), right=B2(enemy, but C2 blocks landing)
        // A3: left=edge, forward=A4(enemy, but A5 blocks landing), right=B3(enemy, but C3 blocks)
        // Both white pieces are truly blocked!
        assert_eq!(
            board.status(),
            GameStatus::Won(Team::Black),
            "Completely blocked white should lose"
        );
    }

    // ========== COMPLEX MULTI-CAPTURE CHAIN TESTS ==========

    #[test]
    fn pawn_four_capture_chain() {
        // White pawn at A2
        // Chain: A3->A4, B4->C4, C5->C6, D6->E6
        let board = Board::from_squares(
            Team::White,
            &[Square::A2],
            &[Square::A3, Square::B4, Square::C5, Square::D6],
            &[],
        );
        let actions = board.actions();

        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap_or(0);

        assert_eq!(max_captures, 4, "Should capture 4 pieces in chain");
    }

    #[test]
    fn king_five_capture_chain() {
        // White king at A2 (not on promotion row)
        // Create a zigzag path for the king:
        // A2 -> captures A4 (up) -> land A5
        // A5 -> captures C5 (right) -> land D5
        // D5 -> captures D3 (down) -> land D2
        // D2 -> captures F2 (right) -> land G2
        // This is 4 captures with alternating directions

        let board = Board::from_squares(
            Team::White,
            &[Square::A2],
            &[Square::A4, Square::C5, Square::D3, Square::F2],
            &[Square::A2],
        );
        let actions = board.actions();

        let max_captures = actions
            .iter()
            .map(|a| a.delta.pieces[Team::Black.to_usize()].count_ones())
            .max()
            .unwrap_or(0);

        // King should chain 4 captures with 90° turns
        assert!(max_captures >= 3, "King should capture at least 3 in chain");
    }

    // ========== EDGE AND CORNER TESTS ==========

    #[test]
    fn pawn_on_edge_limited_moves() {
        // White pawn on left edge
        let board = Board::from_squares(Team::White, &[Square::A4], &[Square::H8], &[]);
        let actions = board.actions();

        // A4 can move: B4 (right), A5 (up) - cannot go left
        assert_eq!(actions.len(), 2, "Edge pawn should have 2 moves");
    }

    #[test]
    fn pawn_in_corner_very_limited() {
        // White pawn at A1 corner (but this is promotion row for black, not valid for white pawn)
        // Use white pawn at H3 instead (near corner)
        let board = Board::from_squares(Team::White, &[Square::H3], &[Square::A8], &[]);
        let actions = board.actions();

        // H3 can move: G3 (left), H4 (up) - cannot go right (edge)
        assert_eq!(actions.len(), 2, "Corner-area pawn should have 2 moves");
    }

    #[test]
    fn king_in_corner_moves() {
        // King at A1
        let board = Board::from_squares(Team::White, &[Square::A1], &[Square::H8], &[Square::A1]);
        let actions = board.actions();

        // A1 king can go: right (B1-H1 = 7) + up (A2-A8 = 7) = 14 moves
        assert_eq!(actions.len(), 14, "Corner king should have 14 moves");
    }

    // ========== BLOCKING TESTS ==========

    #[test]
    fn pawn_blocked_by_friendly() {
        // White pawns at D4 and D5 - D4 can't move up
        let board = Board::from_squares(Team::White, &[Square::D4, Square::D5], &[Square::H8], &[]);
        let actions = board.actions();

        // D4 can move: C4, E4 (not D5 - blocked)
        // D5 can move: C5, E5, D6
        // Total: 2 + 3 = 5 moves
        let d4_moves: Vec<_> = actions
            .iter()
            .filter(|a| {
                let delta = a.delta.pieces[Team::White.to_usize()];
                delta & Square::D4.to_mask() != 0
            })
            .collect();

        let d4_to_d5 = d4_moves.iter().find(|a| {
            let dest = a.delta.pieces[Team::White.to_usize()] & !Square::D4.to_mask();
            dest == Square::D5.to_mask()
        });

        assert!(
            d4_to_d5.is_none(),
            "D4 should not be able to move to D5 (blocked)"
        );
    }

    #[test]
    fn king_blocked_by_friendly_cannot_pass() {
        // King at A4, friendly pawn at C4
        let board = Board::from_squares(
            Team::White,
            &[Square::A4, Square::C4],
            &[Square::H8],
            &[Square::A4],
        );
        let actions = board.actions();

        // King going right can only reach B4 (blocked by C4)
        let past_c4 = actions.iter().find(|a| {
            let delta = a.delta.pieces[Team::White.to_usize()];
            if delta & Square::A4.to_mask() != 0 {
                let dest = delta & !Square::A4.to_mask() & !Square::C4.to_mask();
                // Check if destination is D4, E4, F4, G4, or H4
                dest & (Square::D4.to_mask()
                    | Square::E4.to_mask()
                    | Square::F4.to_mask()
                    | Square::G4.to_mask()
                    | Square::H4.to_mask())
                    != 0
            } else {
                false
            }
        });

        assert!(
            past_c4.is_none(),
            "King should not pass through friendly piece"
        );
    }

    // ========== INITIAL POSITION TESTS ==========

    #[test]
    fn initial_position_piece_count() {
        let board = Board::new_default();
        assert_eq!(
            board.friendly_pieces().count_ones(),
            16,
            "White should have 16 pieces"
        );
        assert_eq!(
            board.hostile_pieces().count_ones(),
            16,
            "Black should have 16 pieces"
        );
    }

    #[test]
    fn initial_position_no_kings() {
        let board = Board::new_default();
        assert_eq!(board.state.kings, 0, "No kings at start");
    }

    #[test]
    fn initial_position_white_to_move() {
        let board = Board::new_default();
        assert_eq!(board.turn, Team::White, "White moves first");
    }

    /// Tests that path reconstruction enforces the 180° turn prohibition
    /// in complex multi-capture king sequences.
    ///
    /// This position has 9 different 10-capture sequences. Path reconstruction
    /// must find valid paths without 180° reversals for all of them.
    #[test]
    fn king_10_capture_path_no_180_turns() {
        let board = Board::from_squares(
            Team::White,
            &[Square::C2, Square::G2, Square::H4],
            &[
                Square::C1,
                Square::E2,
                Square::C3,
                Square::B4,
                Square::D4,
                Square::A5,
                Square::E5,
                Square::B6,
                Square::D6,
                Square::H6,
                Square::C7,
                Square::H7,
            ],
            &[
                Square::C1,
                Square::C2,
                Square::D4,
                Square::B6,
                Square::D6,
                Square::C7,
            ],
        );

        let actions = board.actions();
        assert_eq!(actions.len(), 9, "Expected 9 maximum-capture actions");

        for action in &actions {
            assert_eq!(
                action.capture_count(Team::White),
                10,
                "All actions should capture 10 pieces"
            );

            let detailed = action.to_detailed(board.turn, &board.state);
            let path = detailed.path();
            let mut prev_dir: Option<(i8, i8)> = None;

            for i in 1..path.len() {
                let from = path[i - 1];
                let to = path[i];

                let dcol = (to.column() as i8 - from.column() as i8).signum();
                let drow = (to.row() as i8 - from.row() as i8).signum();

                if let Some((pcol, prow)) = prev_dir {
                    assert!(
                        !(dcol == -pcol && drow == -prow && (dcol != 0 || drow != 0)),
                        "180° turn detected in path: {} -> {}",
                        from,
                        to
                    );
                }
                prev_dir = Some((dcol, drow));
            }
        }
    }
}