hearts-engine 0.1.1

Hearts bots: heuristic and Monte Carlo strategies over an information-hygienic view
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
//! [`MonteCarloBot`]: determinized Monte Carlo move selection
//!
//! Unlike the gin engine this crate mirrors, there is no separate rollout
//! simulator to keep in sync with the rules: every Hearts observation
//! except the hidden hands is public, so a sampled world is a real
//! [`hearts::Round`] — reconstructed as a hold deal of the sampled original
//! hands with the public history replayed — and rollouts run on the real
//! state machine.

use crate::heuristic::{
    greedy_pass, greedy_play, moon_defense, pass_key, rollout_play, shoot_play,
};
use crate::{HeuristicConfig, Strategy, View};
use hearts::{Card, Hand, PassDirection, Phase, Rank, Round, RoundResult, Rules, Seat, Suit};
use rand::{Rng, RngExt as _};

/// How many candidate plays a decision weighs after collapsing
/// rank-adjacent equal-value equivalents; the rest are never worth a rollout.
const MAX_CANDIDATES: usize = 8;

/// How many of the highest [`pass_key`] cards seed the pass triples: every
/// triple of the top 6 is rolled, after the greedy incumbent and before the
/// short-suit void candidates.
const PASS_POOL: usize = 6;

/// The world count of the first scoring batch; each later batch doubles the
/// evaluated total, so elimination checkpoints fall after 32, 64, 128, ...
/// worlds.  A decision of 32 samples or fewer is a single batch, identical
/// to an unbatched run.
const BATCH: usize = 32;

/// Maximum multiple of the configured world count for a contested decision.
const MAX_WIDTH_MULTIPLIER: usize = 3;

/// What the rollouts said about one candidate: its per-world equities, its
/// summed round points, and how many of those worlds it shot the moon in
type Scored = (Vec<f64>, f64, u32);

/// One candidate action to score: the move a [`Strategy`] method would
/// return, paired with its rendered [`Assessment::action`] label
struct Candidate {
    label: String,
    choice: Choice,
    /// Whether to roll the continuation as a moon attempt by the deciding
    /// seat rather than with the greedy policy.  Shooting is a plan, not a
    /// single card, so it lives on the rollout instead of on [`Choice`].
    shoot: bool,
}

/// A typed candidate move
#[derive(Clone, Copy)]
enum Choice {
    /// Pass these three cards.
    Pass([Card; 3]),
    /// Play this card.
    Play(Card),
}

impl Choice {
    /// Apply the move to a world and play it out: greedily, or with `me`
    /// shooting the moon against a table that defends.
    fn roll(self, round: &mut Round, me: Seat, shoot: bool) -> RoundResult {
        match self {
            Self::Pass(cards) => round
                .pass(me, cards.into_iter().collect())
                .expect("a candidate pass is legal in every sampled world"),
            Self::Play(card) => round
                .play(me, card)
                .expect("a candidate play is legal in every sampled world"),
        }
        let mut played = round.played();
        while let Some(seat) = round.turn() {
            let card = rollout_play(round, seat, shoot.then_some(me), played);
            round
                .play(seat, card)
                .expect("the rollout policy picks from legal_plays");
            played.insert(card);
        }
        round.result().expect("a turnless round is finished")
    }
}

/// A determinized Monte Carlo player
///
/// At every decision the bot samples hidden worlds consistent with its
/// [`View`] — the receiver of its pass holds every passed card not yet
/// played, seats shown void in a suit receive none of it, and the
/// remaining unseen cards are distributed randomly.  The known incoming pass
/// then softly favors worlds that fit the same pass model without becoming a
/// hard card-location claim.  The bot plays each world out with the greedy
/// policy on all seats and picks the action with the best
/// expected value *for the game*: each rollout's result lands on the
/// running [`game scores`](View::game_scores), a result that reaches
/// [`game_target`](Rules::game_target) counts the final standings as
/// normalized `3-2-1-0` matchpoints with ties averaged, and anything
/// short of one counts its round points.
/// The same worlds are reused across candidate actions (common random
/// numbers), and the bot deviates from the greedy baseline only when the
/// paired samples show a statistically clear gain.  Worlds are rolled in
/// growing batches, and a challenger the incumbent already statistically
/// dominates is dropped at a batch boundary — once none remain, the
/// remaining worlds are never rolled at all — so an easy decision costs a
/// fraction of the full sample count.  If the configured width leaves the
/// mean-best challenger inside the gate in both directions, the bot adds
/// base-width samples for the surviving candidates, capped at three times
/// the configured width.
///
/// One candidate per decision is a moon attempt, rolled with the deciding
/// seat shooting and the other three defending the way the live table
/// does, once the shot shows eight points.  It has
/// to clear the usual significance gate *and* actually reach the moon in
/// most of the sampled worlds, and once chosen it is carried to the end of
/// the round rather than re-argued each trick — a shot re-decided per trick
/// takes the points and misses the moon.
///
/// The bot owns its random number generator, so a seeded generator makes
/// its play reproducible.
pub struct MonteCarloBot<R: Rng> {
    rng: R,
    samples: u32,
    /// How many paired standard errors a challenger must clear in
    /// [`beats`] before the bot deviates from the greedy incumbent.
    gate: f64,
    /// The pass policy the search models with, in all three of its roles:
    /// the rollout opponents' passes, the observation reweight, and the
    /// bot's own candidate ranking.
    pass_model: HeuristicConfig,
    /// Whether a moon attempt is under way, carried across the tricks of
    /// one round so the search does not re-litigate it every turn.
    shooting: bool,
    moon_attempts: u32,
    moon_passes: u32,
}

/// One candidate action's Monte Carlo assessment, for a solver or hint view
///
/// Produced by [`MonteCarloBot::assess`]: the same rollouts the bot chooses
/// with, surfaced per candidate instead of collapsed to the single action a
/// [`Strategy`] method returns.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Assessment {
    /// A rendered label for the action, e.g. `"play Q♠"` or
    /// `"pass Q♠ A♥ K♥"`.
    pub action: String,
    /// Mean equity in `[0, 1]` — the quantity the bot maximizes, so
    /// candidates rank by it.  A rollout that ends the game pays its
    /// normalized `3-2-1-0` matchpoints (a sole win 1, a sole last 0,
    /// ties averaged); one that does not pays the mid-game margin band.
    /// For a player-facing figure, `4 − 3·equity` is the expected
    /// finishing place.  A candidate the bot eliminated early averages
    /// over the worlds it saw before elimination rather than the full
    /// sample count.
    pub equity: f64,
    /// Mean round points this action costs the deciding seat — **lower is
    /// better**, unlike the gin engine's signed gain.  Averaged over the
    /// same worlds as [`equity`](Self::equity).
    pub ev: f64,
    /// Whether this is the bot's own pick — the move a [`Strategy`] method
    /// would return on this view.  Because the bot deviates from the greedy
    /// baseline only on a statistically clear gain, this need not be the
    /// highest-equity candidate.
    pub recommended: bool,
}

impl<R: Rng> MonteCarloBot<R> {
    /// A bot with a default base width of 128 worlds per decision
    pub const fn new(rng: R) -> Self {
        Self {
            rng,
            samples: 128,
            gate: 1.5,
            pass_model: HeuristicConfig::new(),
            shooting: false,
            moon_attempts: 0,
            moon_passes: 0,
        }
    }

    /// Set how many worlds each decision samples
    ///
    /// More samples play stronger and slower; easy decisions stop at a
    /// fraction of the budget, while a contested decision may extend to
    /// three times it.  The `parallel` feature divides the cost by most of a
    /// machine's cores without changing a single decision.
    #[must_use]
    pub const fn samples(mut self, samples: u32) -> Self {
        self.samples = samples;
        self
    }

    /// Set the significance gate: how many paired standard errors a
    /// challenger must clear before the bot deviates from the greedy
    /// incumbent
    ///
    /// The default is 1.5.  Duplicate-deal measurement found strength
    /// monotone falling in the threshold over `[1.0, 2.5]` — the old 2.0
    /// over-corrected for multiplicity by about 0.2 points a deal — while
    /// a lower gate trusts more of the rollout noise.
    #[must_use]
    pub const fn gate(mut self, gate: f64) -> Self {
        self.gate = gate;
        self
    }

    /// Set the pass policy the search models with
    ///
    /// One config serves all three of the search's pass-model roles at
    /// once: the passes the three rollout opponents submit in a sampled
    /// world, the policy the observation reweight re-passes a plausible
    /// giver holding with, and the ranking that orders the bot's own pass
    /// candidates — whose first triple is the incumbent challengers must
    /// beat by [`gate`](Self::gate) standard errors.  The default is
    /// [`HeuristicConfig::new`], so a bot tuned away from the shipped
    /// knobs must be told, or it models the shipped policy instead of its
    /// own.
    ///
    /// Play-side knobs are not read: `moon_defense` belongs to the live
    /// overlay and the rollout policy, not to passing.
    #[must_use]
    pub const fn pass_model(mut self, config: HeuristicConfig) -> Self {
        self.pass_model = config;
        self
    }

    /// How many shoot-the-moon plans this bot has begun
    ///
    /// The count is lifetime-monotone across rounds; callers can diff it
    /// before and after any interval they want to measure.
    pub const fn moon_attempts(&self) -> u32 {
        self.moon_attempts
    }

    /// How many shoot-shaped passes this bot has chosen
    ///
    /// The count is lifetime-monotone across rounds; callers can diff it
    /// before and after any interval they want to measure.
    pub const fn moon_passes(&self) -> u32 {
        self.moon_passes
    }

    /// Sample one set of current hidden hands consistent with the view, by
    /// randomized most-constrained-first backtracking over the unseen cards,
    /// softly weighted by the known incoming pass
    ///
    /// Hard constraints: exact hand sizes, no card in a suit the seat is void
    /// in, and the still-unplayed passed cards pinned to the receiver.  Soft
    /// evidence comes from the known incoming pass.  The true deal satisfies
    /// every hard constraint, so failure is a sampling accident — the caller
    /// retries.
    fn sample_hands(&mut self, view: &View<'_>) -> Option<[Hand; 4]> {
        let me = view.seat();
        let mut hands = [Hand::EMPTY; 4];
        let mut room = [0usize; 4];
        hands[me as usize] = view.hand();
        for seat in Seat::ALL {
            if seat != me {
                let known = view.known_cards(seat);
                hands[seat as usize] = known;
                // A pass-phase world samples *pre-pass* hands: a seat that
                // already passed holds 10 now, but its 3 hidden passes are
                // part of the unseen pool and belong back in its hand.
                let size = if view.phase() == Phase::Passing {
                    13
                } else {
                    view.hand_len(seat)
                };
                room[seat as usize] = size - known.len();
            }
        }

        // Shuffle for randomness, then most-constrained cards first.
        let mut cards: Vec<Card> = view.unseen().into_iter().collect();
        for i in (1..cards.len()).rev() {
            let j = self.rng.random_range(0..=i);
            cards.swap(i, j);
        }
        let allowed = |card: Card, seat: Seat| seat != me && !view.is_void(seat, card.suit);
        cards.sort_by_key(|&card| Seat::ALL.iter().filter(|&&s| allowed(card, s)).count());

        fn place(
            cards: &[Card],
            allowed: &impl Fn(Card, Seat) -> bool,
            hands: &mut [Hand; 4],
            room: &mut [usize; 4],
            rng: &mut (impl Rng + ?Sized),
        ) -> bool {
            let Some((&card, rest)) = cards.split_first() else {
                return true;
            };
            let mut order = Seat::ALL;
            for i in (1..4).rev() {
                let j = rng.random_range(0..=i);
                order.swap(i, j);
            }
            for seat in order {
                if room[seat as usize] > 0 && allowed(card, seat) {
                    room[seat as usize] -= 1;
                    hands[seat as usize].insert(card);
                    if place(rest, allowed, hands, room, rng) {
                        return true;
                    }
                    hands[seat as usize].remove(card);
                    room[seat as usize] += 1;
                }
            }
            false
        }

        if !place(&cards, &allowed, &mut hands, &mut room, &mut self.rng) {
            return None;
        }

        if let Some(received) = view.received() {
            let giver = view.direction().giver(me);
            let mut plausible = hands[giver as usize] | received;
            for trick in view.tricks().iter().copied().chain(view.current_trick()) {
                if let Some(card) = trick.card_from(giver) {
                    plausible.insert(card);
                }
            }
            let likelihood = pass_observation_likelihood(plausible, received, self.pass_model);
            if self.rng.random::<f64>() > likelihood {
                return None;
            }
        }
        Some(hands)
    }

    /// Sample one world: a real [`Round`] at this decision point
    ///
    /// A play-phase world is a hold deal of the sampled *original* hands
    /// (current hand plus own plays) with the public history replayed; a
    /// pass-phase world is a fresh deal of sampled pre-pass hands with the
    /// three opponents' greedy passes already submitted, waiting on ours.
    fn sample_world(&mut self, view: &View<'_>) -> Option<Round> {
        let me = view.seat();
        let hands = self.sample_hands(view)?;

        if view.phase() == Phase::Passing {
            let mut round = Round::from_deal(*view.rules(), view.direction(), hands).ok()?;
            for seat in Seat::ALL {
                if seat != me {
                    let pass: Hand = greedy_pass(hands[seat as usize], self.pass_model)
                        .into_iter()
                        .collect();
                    round.pass(seat, pass).ok()?;
                }
            }
            return Some(round);
        }

        // Original post-pass hands: what each seat holds now plus what it
        // has already played.
        let mut originals = hands;
        for trick in view.tricks().iter().copied().chain(view.current_trick()) {
            for (seat, card) in trick.plays() {
                originals[seat as usize].insert(card);
            }
        }
        let mut round = Round::from_deal(*view.rules(), PassDirection::Hold, originals).ok()?;
        for trick in view.tricks().iter().copied().chain(view.current_trick()) {
            for (seat, card) in trick.plays() {
                round.play(seat, card).ok()?;
            }
        }
        Some(round)
    }

    /// Sample `count` worlds, retrying the rare inconsistent construction
    fn sample_worlds(&mut self, view: &View<'_>, count: u32) -> Vec<Round> {
        let mut worlds = Vec::with_capacity(count as usize);
        // The true deal is always consistent, so failures are sampling
        // accidents; the budget only guards against a pathological view.
        let mut attempts = count * 4 + 64;
        while worlds.len() < count as usize && attempts > 0 {
            attempts -= 1;
            if let Some(world) = self.sample_world(view) {
                worlds.push(world);
            }
        }
        worlds
    }

    /// The ordered candidate moves for the current decision, the greedy
    /// incumbent first; empty when the seat has no real choice
    fn candidates(&self, view: &View<'_>) -> Vec<Candidate> {
        match view.phase() {
            Phase::Passing => pass_candidates(view, self.pass_model),
            Phase::Playing => play_candidates(view),
            Phase::Finished => Vec::new(),
        }
    }

    /// Score every candidate on freshly sampled worlds and return the move
    /// to play: the greedy incumbent (`candidates[0]`) unless a challenger
    /// clears the significance gate
    fn choose(&mut self, view: &View<'_>, candidates: &[Candidate]) -> Choice {
        let scored = self.score(view, candidates);
        let best = recommended(&scored, candidates, self.gate);
        if candidates[best].shoot {
            self.moon_passes += 1;
        }
        candidates[best].choice
    }

    /// Score one decision, extending only an unresolved incumbent/challenger
    /// comparison by another base-width sample, up to three times the budget.
    fn score(&mut self, view: &View<'_>, candidates: &[Candidate]) -> Vec<Scored> {
        let mut scored: Vec<Scored> = vec![(Vec::new(), 0.0, 0); candidates.len()];
        let mut alive: Vec<usize> = (1..candidates.len()).collect();
        for _ in 0..MAX_WIDTH_MULTIPLIER {
            let worlds = self.sample_worlds(view, self.samples);
            if worlds.is_empty() {
                break;
            }
            score_worlds(
                view,
                &worlds,
                candidates,
                self.gate,
                &mut scored,
                &mut alive,
            );
            if !contested(&scored, &alive, self.gate) {
                break;
            }
        }
        scored
    }

    /// Assess every candidate action for the current decision, each with
    /// its Monte Carlo equity and expected round points, ranked by equity
    /// with the bot's own pick flagged — the read a solver or hint view
    /// shows
    ///
    /// When every legal play collapses into one equivalence class the read
    /// is still offered, against each legal play tied: a human cannot see
    /// from the hand alone that the ranks between the cards are already gone,
    /// so refusing the read would look like a bug.
    ///
    /// Returns empty only when there is genuinely nothing to weigh: a single
    /// legal play, a finished round, or a view no consistent world can be
    /// sampled for (e.g. a seat mid-pass).
    #[must_use]
    pub fn assess(&mut self, view: &View<'_>) -> Vec<Assessment> {
        let candidates = self.candidates(view);
        if candidates.is_empty() {
            return Vec::new();
        }
        let scored = self.score(view, &candidates);
        if scored[0].0.is_empty() {
            // No consistent world: averaging zero rollouts would put NaN
            // in the public rows.
            return Vec::new();
        }
        let mean = |(equities, ev_sum, _): &Scored| {
            let n = equities.len() as f64;
            (equities.iter().sum::<f64>() / n, ev_sum / n)
        };

        // A single candidate means the legal plays all collapsed into one
        // class — every one interchangeable.  List them as tied so the panel
        // says "any of these" instead of going blank.
        if candidates.len() == 1 {
            let (equity, ev) = mean(&scored[0]);
            return view
                .legal_plays()
                .into_iter()
                .map(|card| Assessment {
                    action: format!("play {card}"),
                    equity,
                    ev,
                    recommended: true,
                })
                .collect();
        }

        let best = recommended(&scored, &candidates, self.gate);
        let mut out: Vec<Assessment> = candidates
            .iter()
            .zip(&scored)
            .enumerate()
            .map(|(i, (candidate, score))| {
                let (equity, ev) = mean(score);
                Assessment {
                    action: candidate.label.clone(),
                    equity,
                    ev,
                    recommended: i == best,
                }
            })
            .collect();
        out.sort_by(|a, b| b.equity.total_cmp(&a.equity));
        out
    }
}

/// A soft likelihood for the cards known to have arrived from our giver
///
/// We cannot distinguish the giver's ten kept cards from its three incoming
/// cards, so score the known pass against that deliberately over-broad
/// plausible pre-pass holding.  Every mismatch costs only a quarter of the
/// weight; an opponent need not share our pass policy.
fn pass_observation_likelihood(plausible: Hand, received: Hand, config: HeuristicConfig) -> f64 {
    let modeled: Hand = greedy_pass(plausible, config).into_iter().collect();
    let misses = 3 - (modeled & received).len() as i32;
    0.75f64.powi(misses)
}

/// Add `triple` unless the pool already holds those three cards
///
/// A pass is a set, so two orderings of one triple are one candidate.  The
/// incumbent is seeded first and is exactly the triple the pool loop is most
/// likely to regenerate, which is why this is shared rather than inline.
fn push_new(triples: &mut Vec<[Card; 3]>, triple: [Card; 3]) {
    let set: Hand = triple.into_iter().collect();
    if !triples
        .iter()
        .any(|old| old.iter().copied().collect::<Hand>() == set)
    {
        triples.push(triple);
    }
}

/// The candidate passes: the greedy incumbent first, then every triple of the
/// [`PASS_POOL`] highest-ranked cards, passes that empty a short side suit,
/// plus the one moon pass
///
/// Candidate 0 is [`greedy_pass`] itself, built rather than inferred.  The
/// ranking below agrees with the policy on the first card and nowhere after
/// it — the policy rescores against the shrinking hand — so the invariant the
/// significance gate defends can no longer be inherited from two sorts that
/// happened to agree.
fn pass_candidates(view: &View<'_>, config: HeuristicConfig) -> Vec<Candidate> {
    let hand = view.hand();
    let mut ranked: Vec<Card> = hand.into_iter().collect();
    // `pass_key` is injective over a hand, so the sort needs no stability and
    // the order is the same on every build.
    ranked.sort_unstable_by_key(|&card| core::cmp::Reverse(pass_key(hand, card, config)));

    // The mirror of the greedy triple: the three cards the pass policy least
    // wants gone are low cards of long suits, which is exactly the ballast a
    // shot sheds.  One candidate, rolled with a shooting continuation.
    // Ranked without queen insurance: it keeps low spades to duck an incoming
    // Q♠, which a shooter wants, and ranking them dead last would make them
    // the ballast (measured: −8 SE of the seat's own moons).
    let mut ducker = config;
    ducker.queen_insurance = 0;
    let mut ballast: Vec<Card> = hand.into_iter().collect();
    ballast.sort_unstable_by_key(|&card| core::cmp::Reverse(pass_key(hand, card, ducker)));
    let losers: [Card; 3] = ballast[ballast.len() - 3..]
        .try_into()
        .expect("a passing hand holds thirteen cards");

    let pool = &ranked[..ranked.len().min(PASS_POOL)];
    let mut triples = Vec::<[Card; 3]>::with_capacity(24);
    triples.push(greedy_pass(hand, config));
    for i in 0..pool.len() {
        for j in i + 1..pool.len() {
            for k in j + 1..pool.len() {
                push_new(&mut triples, [pool[i], pool[j], pool[k]]);
            }
        }
    }

    // Independent card scores can award three separate void bonuses without
    // actually emptying a suit.  Make every short non-spade void reachable,
    // filling a one- or two-card suit's triple with the most dangerous cards
    // outside it.
    for suit in Suit::ASC.into_iter().filter(|&suit| suit != Suit::Spades) {
        let suited: Vec<Card> = hand[suit].iter().map(|rank| Card { suit, rank }).collect();
        if suited.is_empty() || suited.len() > 3 {
            continue;
        }
        let mut cards = suited;
        cards.extend(
            ranked
                .iter()
                .copied()
                .filter(|card| card.suit != suit)
                .take(3 - cards.len()),
        );
        push_new(
            &mut triples,
            cards.try_into().expect("a passing hand has filler"),
        );
    }

    let mut out: Vec<Candidate> = triples
        .into_iter()
        .map(|triple| Candidate {
            label: format!("pass {} {} {}", triple[0], triple[1], triple[2]),
            choice: Choice::Pass(triple),
            shoot: false,
        })
        .collect();
    out.push(Candidate {
        label: format!("shoot, pass {} {} {}", losers[0], losers[1], losers[2]),
        choice: Choice::Pass(losers),
        shoot: true,
    });
    out
}

/// The candidate plays: the legal set collapsed by rank adjacency — cards
/// of a suit separated only by ranks this seat has seen (own hand, or
/// played to a **completed** trick — a rank sitting in the trick in progress
/// still separates the cards above it from the cards below) *and* of equal
/// penalty value are interchangeable — the greedy incumbent first, capped at
/// [`MAX_CANDIDATES`]
fn play_candidates(view: &View<'_>) -> Vec<Candidate> {
    let legal = view.legal_plays();
    if legal.len() < 2 {
        return Vec::new();
    }
    let trick = view.current_trick().expect("a play decision has a trick");
    let played = view.played();
    let incumbent = greedy_play(legal, trick, played);
    // Cards in the trick in progress are committed but still contest THIS
    // trick — behind a led J♦ the Q♦ wins and the T♦ does not, so they are no
    // sequence.  Collapse only across ranks gone in completed tricks.
    let seen = view.hand() | (played - trick.cards());

    let mut reps = vec![incumbent];
    for suit in Suit::ASC {
        let mut group: Vec<Rank> = Vec::new();
        let flush = |group: &mut Vec<Rank>, reps: &mut Vec<Card>| {
            if let Some(&low) = group.first() {
                let card = |rank| Card { suit, rank };
                if !group.iter().any(|&rank| card(rank) == incumbent) {
                    reps.push(card(low));
                }
            }
            group.clear();
        };
        let mut prev: Option<Rank> = None;
        for rank in legal[suit] {
            if let Some(prev) = prev {
                // Rank-adjacency is not equivalence: two touching cards of
                // unequal penalty value (Q♠ vs J♠) are distinct plays.
                // ponytail: keys on Card::points() (u8, standard Hearts); a
                // variant like Omnibus (J♦ = -10, 10♣ doubler) must widen the
                // value function this consults, then this line follows it.
                let same_value =
                    (Card { suit, rank: prev }).points() == (Card { suit, rank }).points();
                let contiguous = same_value
                    && (prev.get() + 1..rank.get()).all(|gap| seen[suit].contains(Rank::new(gap)));
                if !contiguous {
                    flush(&mut group, &mut reps);
                }
            }
            group.push(rank);
            prev = Some(rank);
        }
        flush(&mut group, &mut reps);
    }

    reps.truncate(MAX_CANDIDATES);
    let mut out: Vec<Candidate> = reps
        .into_iter()
        .map(|card| Candidate {
            label: format!("play {card}"),
            choice: Choice::Play(card),
            shoot: false,
        })
        .collect();

    // One moon candidate, past the cap: the truncation runs in suit order
    // and would otherwise drop the very high cards a shot opens with.
    // Offered only while nobody else has scored — a moon is dead the moment
    // they do — and only when the plays differ at all, so a fully collapsed
    // legal set keeps `assess`'s "every play tied" read.
    let alive = Seat::ALL
        .into_iter()
        .all(|seat| seat == view.seat() || view.points_taken(seat) == 0);
    if alive && out.len() > 1 {
        let card = shoot_play(legal, trick, played);
        out.push(Candidate {
            label: format!("shoot, play {card}"),
            choice: Choice::Play(card),
            shoot: true,
        });
    }
    out
}

/// Roll candidates through the same `worlds` (common random numbers) in
/// growing batches, eliminating challengers the incumbent already
/// dominates, and return per candidate its per-world equities and summed
/// round points
///
/// Survivors always reach the full world count, so the final
/// [`recommended`] read over them is exactly the unbatched one.  An
/// eliminated candidate keeps the equities it accumulated: its paired mean
/// against the incumbent is negative on that prefix, and [`beats`] zips to
/// the shorter slice, so [`recommended`] rejects it with no special
/// casing.  Each serial evaluator or parallel worker reuses a scratch
/// [`Round`], whose `clone_from` retains the completed-trick allocation.
fn score_worlds(
    view: &View<'_>,
    worlds: &[Round],
    candidates: &[Candidate],
    gate: f64,
    scored: &mut [Scored],
    alive: &mut Vec<usize>,
) {
    let me = view.seat();
    let rules = view.rules();
    let standing = absolute_standing(me, view.game_scores());
    let eval = |candidate: &Candidate, world: &Round, scratch: &mut Round| {
        scratch.clone_from(world);
        let result = candidate.choice.roll(scratch, me, candidate.shoot);
        let moon = u32::from(result.shooter() == Some(me));
        let scores = result.scores(rules);
        (
            equity(&scores, me, standing, rules),
            f64::from(scores[me as usize]),
            moon,
        )
    };

    #[cfg(not(feature = "parallel"))]
    let mut scratch = worlds.first().cloned();
    let mut done = 0;
    while done < worlds.len() {
        let batch = &worlds[done..worlds.len().min(done + done.max(BATCH))];
        for &i in std::iter::once(&0).chain(alive.iter()) {
            let candidate = &candidates[i];
            #[cfg(feature = "parallel")]
            let results: Vec<(f64, f64, u32)> = {
                use rayon::prelude::*;
                batch
                    .par_iter()
                    .map_init(
                        || batch[0].clone(),
                        |scratch, world| eval(candidate, world, scratch),
                    )
                    .collect()
            };
            #[cfg(not(feature = "parallel"))]
            let results = batch.iter().map(|world| {
                eval(
                    candidate,
                    world,
                    scratch
                        .as_mut()
                        .expect("a non-empty batch has a scratch round"),
                )
            });

            // Reduced sequentially in world order in both builds, so a
            // parallel bot makes bit-identical decisions to a serial one.
            let (equities, ev_sum, moons) = &mut scored[i];
            for (equity, points, moon) in results {
                equities.push(equity);
                *ev_sum += points;
                *moons += moon;
            }
        }
        done += batch.len();
        alive.retain(|&i| !beats(&scored[0].0, &scored[i].0, gate));
        if alive.is_empty() {
            break;
        }
    }
}

/// Whether the strongest surviving challenger remains inside the
/// significance gate in both directions, with no challenger already clear.
fn contested(scored: &[Scored], alive: &[usize], gate: f64) -> bool {
    let mean = |e: &[f64]| e.iter().sum::<f64>() / e.len() as f64;
    let defend = &scored[0].0;
    if alive.iter().any(|&i| beats(&scored[i].0, defend, gate)) {
        return false;
    }
    alive
        .iter()
        .copied()
        .max_by(|&a, &b| mean(&scored[a].0).total_cmp(&mean(&scored[b].0)))
        .is_some_and(|best| !beats(defend, &scored[best].0, gate))
}

/// The index of the recommended candidate: the greedy incumbent
/// (`scored[0]`) unless a challenger's paired advantage clears the
/// [`beats`] gate, in which case the largest such gain
///
/// A moon challenger must clear a second bar: the rollouts have to actually
/// shoot it more often than not.
///
/// Its per-world equities are bimodal — the moon band's ceiling against a
/// heap of failures — and a paired test on heavy-tailed differences waves
/// through edges that are not there: measured against the greedy field, the
/// gate alone attempted a moon in two rounds in five to land one in twenty,
/// at a cost of over two points a round.  The break-even is arithmetic
/// rather than taste.  A moon is worth `0.5 + 26/112 ≈ 0.732` mid-game, a
/// failed shot leaves us holding about half the deck's points for roughly
/// `0.423`, and the greedy line is worth about `0.577`; the indifference
/// point between them sits within a hair of an even chance.  A moon rate
/// over the sampled worlds is a plain Bernoulli count with none of the tail
/// trouble, so require it to be a majority.
fn recommended(scored: &[Scored], candidates: &[Candidate], gate: f64) -> usize {
    let mean = |e: &[f64]| e.iter().sum::<f64>() / e.len() as f64;
    let likely = |i: usize| 2 * scored[i].2 as usize > scored[i].0.len();
    let defend = &scored[0].0;
    (1..scored.len())
        .filter(|&i| beats(&scored[i].0, defend, gate))
        .filter(|&i| !candidates[i].shoot || likely(i))
        .max_by(|&a, &b| mean(&scored[a].0).total_cmp(&mean(&scored[b].0)))
        .unwrap_or(0)
}

/// Whether the challenger's paired advantage over the incumbent is large
/// enough to trust
///
/// The true value difference between most candidate actions is well below
/// the rollout noise floor, and deviating from the solid greedy baseline
/// on noise alone plays *worse* than the baseline.  A one-sided paired
/// test — the mean difference at least `gate` standard errors above zero,
/// since several challengers get tested per decision — keeps only the
/// deviations the samples actually support.
fn beats(challenger: &[f64], incumbent: &[f64], gate: f64) -> bool {
    let n = challenger.len() as f64;
    let mean = challenger
        .iter()
        .zip(incumbent)
        .map(|(c, i)| c - i)
        .sum::<f64>()
        / n;
    if mean <= 0.0 {
        return false;
    }
    let var = challenger
        .iter()
        .zip(incumbent)
        .map(|(c, i)| (c - i - mean).powi(2))
        .sum::<f64>()
        / n;
    mean > gate * (var / n).sqrt()
}

/// Re-key the seat-relative [`View::game_scores`] by absolute [`Seat`]
fn absolute_standing(me: Seat, relative: [u16; 4]) -> [u16; 4] {
    let mut standing = [0; 4];
    standing[me as usize] = relative[0];
    standing[me.left() as usize] = relative[1];
    standing[me.across() as usize] = relative[2];
    standing[me.right() as usize] = relative[3];
    standing
}

/// The value to `me` of a rollout that charged `scores`, from the
/// `standing` game totals: normalized `3-2-1-0` matchpoints if the game
/// ends, otherwise affine in the round-point margin
///
/// A game ends when a post-round total reaches the target, and the lowest
/// total finishes first.  A finished game pays its matchpoints over three
/// — a sole win 1, then ⅔, ⅓, and 0 for a sole last, ties averaged — the
/// same payoff the arena's `rank` column scores, so the search keeps
/// fighting for placement after the win is gone.  Short of a clinch the
/// value is `0.5` plus the margin between the opponents' average round
/// points and ours, scaled into `(1/4, 3/4)` — strictly below a sole win
/// and strictly above a sole last, so [`beats`] deviates from the
/// round-point objective only when a rollout can actually end the game.
/// A two-way crown (⅚) also outranks any running position; deeper ties
/// and the middle placements interleave with the band on purpose — a
/// middling finish is commensurate with a middling position in a game
/// still running.
fn equity(scores: &[u16; 4], me: Seat, standing: [u16; 4], rules: &Rules) -> f64 {
    let mut totals = standing;
    for seat in Seat::ALL {
        let total = &mut totals[seat as usize];
        *total = total.saturating_add(scores[seat as usize]);
    }
    if totals.iter().any(|&total| total >= rules.game_target) {
        // Twice the 3-2-1-0 matchpoints stays an integer through the tie
        // average; `ahead + share ≤ 4` bounds the subtraction at 6.
        let mine = totals[me as usize];
        let ahead = totals.iter().filter(|&&total| total < mine).count();
        let share = totals.iter().filter(|&&total| total == mine).count();
        return (6 - 2 * ahead - (share - 1)) as f64 / 6.0;
    }
    let mine = f64::from(scores[me as usize]);
    let others: f64 = Seat::ALL
        .iter()
        .filter(|&&seat| seat != me)
        .map(|&seat| f64::from(scores[seat as usize]))
        .sum::<f64>()
        / 3.0;
    0.5 + (others - mine) / 112.0
}

impl<R: Rng> Strategy for MonteCarloBot<R> {
    fn pass_cards(&mut self, view: &View<'_>) -> [Card; 3] {
        let candidates = self.candidates(view);
        match self.choose(view, &candidates) {
            Choice::Pass(cards) => cards,
            Choice::Play(_) => unreachable!("the passing phase yields pass choices"),
        }
    }

    fn play_card(&mut self, view: &View<'_>) -> Card {
        // Reactive moon defense: the rollout policy models every opponent as
        // a greedy ducker who never shoots the moon, so the search is blind
        // to a moon in progress.  Break it with the same overlay HeuristicBot
        // uses (threshold 8, its default).  Draws no randomness, so seeded
        // play stays reproducible.
        if let Some(card) = moon_defense(view, 8) {
            return card;
        }
        let legal = view.legal_plays();
        if legal.len() == 1 {
            // A forced card needs no rollout — and draws no randomness,
            // keeping seeded play reproducible.
            return legal.into_iter().next().expect("checked non-empty");
        }
        let trick = view.current_trick().expect("a play decision has a trick");

        // A moon is a plan, not a card, and the rollouts that chose it
        // priced thirteen tricks of shooting.  Re-deciding it every trick
        // instead measured out at three overrides in ten — the search kept
        // wandering back to the greedy line holding points it had already
        // paid for, which is the worst of both.  So carry the plan: the
        // first trick of a round clears it, another seat scoring kills it,
        // and in between the shot is simply played.
        if view.tricks().is_empty()
            || Seat::ALL
                .into_iter()
                .any(|seat| seat != view.seat() && view.points_taken(seat) > 0)
        {
            self.shooting = false;
        }
        if self.shooting {
            return shoot_play(legal, trick, view.played());
        }

        let candidates = self.candidates(view);
        if candidates.len() < 2 {
            // Every legal card collapsed into one equivalence class.
            return greedy_play(legal, trick, view.played());
        }
        let scored = self.score(view, &candidates);
        let best = recommended(&scored, &candidates, self.gate);
        if candidates[best].shoot {
            self.moon_attempts += 1;
        }
        self.shooting = candidates[best].shoot;
        match candidates[best].choice {
            Choice::Play(card) => card,
            Choice::Pass(_) => unreachable!("the playing phase yields play choices"),
        }
    }

    fn name(&self) -> &str {
        "mc"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{HeuristicBot, Table};
    use hearts::Holding;
    use rand::SeedableRng as _;
    use rand::rngs::StdRng;

    /// The sorted deck dealt round-robin on a hold deal.
    fn fixed_table() -> Table {
        let mut hands = [Hand::EMPTY; 4];
        for (i, card) in Hand::ALL.into_iter().enumerate() {
            hands[i % 4].insert(card);
        }
        let round = Round::from_deal(Rules::new(), PassDirection::Hold, hands)
            .expect("a round-robin deal partitions the deck");
        Table::new(round)
    }

    /// Drive a few tricks with the greedy bot to reach a mid-round spot.
    fn mid_round() -> Table {
        let mut table = fixed_table();
        let mut bot = crate::HeuristicBot::new();
        while table.round().tricks().len() < 5 {
            table.step(&mut bot).expect("greedy plays are legal");
        }
        table
    }

    #[test]
    fn sampled_worlds_are_consistent_with_the_view() {
        let table = mid_round();
        let me = table.turn().expect("a mid-round table has a mover");
        let view = table.view(me);
        let mut bot = MonteCarloBot::new(StdRng::seed_from_u64(1));

        let worlds = bot.sample_worlds(&view, 32);
        assert_eq!(worlds.len(), 32);
        for world in &worlds {
            // The public position is reproduced exactly.
            assert_eq!(world.tricks(), view.tricks());
            assert_eq!(world.current_trick(), view.current_trick());
            assert_eq!(world.played(), view.played());
            assert_eq!(world.hand(me), view.hand());
            assert_eq!(world.phase(), Phase::Playing);

            for seat in Seat::ALL {
                let hand = world.hand(seat);
                // Right sizes, only possible cards, known cards pinned.
                assert_eq!(hand.len(), view.hand_len(seat));
                assert_eq!(hand - view.possible_cards(seat), Hand::EMPTY);
                let known = view.known_cards(seat);
                assert_eq!(hand & known, known);
            }
        }
    }

    #[test]
    fn a_dangerous_pass_is_more_likely() {
        fn hand(text: &str) -> Hand {
            text.parse().expect("a valid hand")
        }
        let plausible = hand("2345.6789.TJ.QKA");
        let modeled: Hand = greedy_pass(plausible, HeuristicConfig::default())
            .into_iter()
            .collect();
        let quiet = hand("234...");
        let default = HeuristicConfig::default();
        assert_eq!(
            pass_observation_likelihood(plausible, modeled, default),
            1.0
        );
        assert!(pass_observation_likelihood(plausible, quiet, default) < 1.0);
    }

    #[test]
    fn the_pass_model_reaches_every_role() {
        // The knob is worthless if it stops at the struct, and the
        // byte-identical arena CSV cannot tell a wired knob from an unwired
        // one — it only pins the default path.  So exercise all three roles
        // through the bot, with a config that re-sorts almost any hand.
        let wild = HeuristicConfig {
            heart_weight: 9,
            ..HeuristicConfig::default()
        };
        let bot = |config| MonteCarloBot::new(StdRng::seed_from_u64(4)).pass_model(config);

        let mut deal = StdRng::seed_from_u64(5);
        let mut table = Table::deal(Rules::new(), PassDirection::Left, &mut deal);
        let me = table.turn().expect("passing starts at North");

        // Ranking (and so candidate 0, the incumbent): a pure function of
        // the view and the config, no rollouts.
        let labels = |config| {
            bot(config)
                .candidates(&table.view(me))
                .into_iter()
                .map(|candidate| candidate.label)
                .collect::<Vec<String>>()
        };
        assert_ne!(labels(HeuristicConfig::default()), labels(wild));

        // The rollout population: at `Phase::Passing` nothing is observed,
        // so both configs draw the identical hands from the identical seed
        // and the modeled pass is the only thing that can differ.
        let population = |config| {
            bot(config)
                .sample_worlds(&table.view(me), 1)
                .first()
                .expect("a pass-phase world samples")
                .passed(me.left())
        };
        assert_ne!(
            population(HeuristicConfig::default()),
            population(wild),
            "the modeled opponents ignored the pass model"
        );

        // The observation model, once the exchange resolves.  Both configs
        // consume the same RNG prefix and exactly one uniform per attempt,
        // so they differ only in the `0.75^misses` threshold they compare
        // against — and so in how many samples they reject.
        for _ in 0..4 {
            table
                .step(&mut HeuristicBot::new())
                .expect("greedy passes are legal");
        }
        let view = table.view(me);
        assert!(view.received().is_some(), "the observation model is live");
        let rejects = |config| {
            let mut bot = bot(config);
            (0..64)
                .filter(|_| bot.sample_hands(&view).is_none())
                .count()
        };
        assert_ne!(
            rejects(HeuristicConfig::default()),
            rejects(wild),
            "the observation model ignored the pass model"
        );
    }

    #[test]
    fn reconstruction_replays_any_real_position() {
        // The omniscient reconstruction — original hands as a hold deal,
        // history replayed — must land on the real position, whatever the
        // real pass direction was.  This is the guard the gin engine needed
        // a whole sync-sim skill for.
        let mut rng = StdRng::seed_from_u64(7);
        for _ in 0..20 {
            let mut table = Table::deal(Rules::new(), PassDirection::Left, &mut rng);
            let mut bot = crate::HeuristicBot::new();
            for _ in 0..rng.random_range(4..30) {
                table.step(&mut bot).expect("greedy plays are legal");
            }
            let round = table.round();
            if round.phase() != Phase::Playing {
                continue;
            }

            // Original hands: what each seat holds now plus what it played.
            let mut originals = [Hand::EMPTY; 4];
            for seat in Seat::ALL {
                let mut own = round.hand(seat);
                for trick in round.tricks().iter().copied().chain(round.current_trick()) {
                    if let Some(card) = trick.card_from(seat) {
                        own.insert(card);
                    }
                }
                originals[seat as usize] = own;
            }

            let mut replay = Round::from_deal(*round.rules(), PassDirection::Hold, originals)
                .expect("original hands partition the deck");
            for trick in round.tricks().iter().copied().chain(round.current_trick()) {
                for (seat, card) in trick.plays() {
                    replay.play(seat, card).expect("the real history is legal");
                }
            }
            assert_eq!(replay.tricks(), round.tricks());
            assert_eq!(replay.current_trick(), round.current_trick());
            for seat in Seat::ALL {
                assert_eq!(replay.hand(seat), round.hand(seat));
            }
        }
    }

    #[test]
    fn seeded_bots_repeat_their_decisions() {
        let table = mid_round();
        let me = table.turn().expect("a mid-round table has a mover");
        let decide = |seed| {
            let mut bot = MonteCarloBot::new(StdRng::seed_from_u64(seed)).samples(16);
            bot.play_card(&table.view(me))
        };
        assert_eq!(decide(3), decide(3));
    }

    #[test]
    fn equity_is_terminal_at_the_target() {
        let rules = Rules::new();
        let me = Seat::North;
        // North ends lowest when West crosses: a sole win.
        let win = equity(&[0, 10, 10, 6], me, [50, 60, 70, 95], &rules);
        assert_eq!(win, 1.0);
        // North crosses alone: a sole last.
        let loss = equity(&[13, 13, 0, 0], me, [95, 0, 0, 0], &rules);
        assert_eq!(loss, 0.0);
        // The middle placements pay their matchpoints over three.
        let second = equity(&[0, 0, 13, 13], me, [50, 40, 60, 95], &rules);
        assert_eq!(second, 2.0 / 3.0);
        let third = equity(&[0, 0, 13, 13], me, [50, 40, 30, 95], &rules);
        assert_eq!(third, 1.0 / 3.0);
        // Ties average: two crowns share 2½ matchpoints, four share 1½.
        let crowns = equity(&[0, 0, 13, 13], me, [50, 50, 60, 95], &rules);
        assert_eq!(crowns, 5.0 / 6.0);
        let level = equity(&[13, 7, 3, 3], me, [87, 93, 97, 97], &rules);
        assert_eq!(level, 0.5);
        // Exactly the target ends the game; one point short stays a round.
        assert_eq!(equity(&[13, 13, 0, 0], me, [87, 0, 0, 0], &rules), 0.0);
        let running = equity(&[13, 13, 0, 0], me, [86, 0, 0, 0], &rules);
        assert!((0.25..=0.75).contains(&running), "{running} is mid-game");
    }

    #[test]
    fn equity_orders_round_outcomes_mid_game() {
        let rules = Rules::new();
        let me = Seat::North;
        let level = [0u16; 4];
        let clean = equity(&[0, 13, 13, 0], me, level, &rules);
        let queen = equity(&[13, 13, 0, 0], me, level, &rules);
        let moon = equity(&[0, 26, 26, 26], me, level, &rules);
        let mooned = equity(&[26, 0, 26, 26], me, level, &rules);
        assert!(moon > clean && clean > queen);
        // Relative to the field, eating the queen and being mooned cost
        // the same 8⅔-point margin — the measure is zero-sum on purpose.
        assert!((queen - mooned).abs() < 1e-12);
        for value in [clean, queen, moon, mooned] {
            assert!((0.25..=0.75).contains(&value), "{value} is mid-game");
        }
    }

    #[test]
    fn beats_requires_a_clear_margin() {
        let base: Vec<f64> = (0..32).map(|i| f64::from(i % 5)).collect();
        let noisy: Vec<f64> = base
            .iter()
            .enumerate()
            .map(|(i, x)| x + if i % 2 == 0 { 1.05 } else { -0.95 })
            .collect();
        assert!(!beats(&noisy, &base, 1.5));
        // The same slim edge passes a gate loose enough to trust it.
        assert!(beats(&noisy, &base, 0.1));

        let better: Vec<f64> = base.iter().map(|x| x + 1.0).collect();
        assert!(beats(&better, &base, 1.5));
        assert!(!beats(&base, &better, 1.5));
        assert!(!beats(&base, &base, 1.5));
    }

    #[test]
    fn shoot_candidate_requires_a_strict_majority() {
        let candidates = [
            Candidate {
                label: "incumbent".into(),
                choice: Choice::Play(Card::TWO_OF_CLUBS),
                shoot: false,
            },
            Candidate {
                label: "shoot".into(),
                choice: Choice::Play(Card::TWO_OF_CLUBS),
                shoot: true,
            },
        ];
        let incumbent = vec![0.0; 4];
        let challenger = vec![1.0; 4];
        let mut scored = vec![(incumbent, 0.0, 0), (challenger, 0.0, 2)];

        assert_eq!(recommended(&scored, &candidates, 1.5), 0);
        scored[1].2 = 3;
        assert_eq!(recommended(&scored, &candidates, 1.5), 1);
    }

    #[test]
    fn only_an_unresolved_best_challenger_is_contested() {
        let base: Vec<f64> = (0..32).map(|i| f64::from(i % 5)).collect();
        let uncertain: Vec<f64> = base
            .iter()
            .enumerate()
            .map(|(i, x)| x + if i % 2 == 0 { 1.05 } else { -0.95 })
            .collect();
        let better: Vec<f64> = base.iter().map(|x| x + 1.0).collect();
        let worse: Vec<f64> = base.iter().map(|x| x - 1.0).collect();
        let scores = |challenger| vec![(base.clone(), 0.0, 0), (challenger, 0.0, 0)];

        assert!(contested(&scores(uncertain), &[1], 1.5));
        assert!(!contested(&scores(better), &[1], 1.5));
        assert!(!contested(&scores(worse), &[1], 1.5));
        assert!(!contested(&scores(base.clone()), &[], 1.5));
    }

    #[test]
    fn play_candidates_collapse_touching_ranks() {
        let table = fixed_table();
        // North leads trick 1: only the 2♣ is legal, no real choice.
        let north = table.turn().expect("a fresh hold deal has a leader");
        assert_eq!(play_candidates(&table.view(north)).len(), 0);
    }

    #[test]
    fn play_candidates_keep_queen_distinct_from_neighbors() {
        // South must follow a led spade holding only J♠ and Q♠. They are
        // rank-adjacent, but Q♠ is worth 13 penalty points and J♠ nothing —
        // they are not one candidate, and the solver must not refuse the read.
        let hand = |cards: &str| -> Hand {
            cards
                .split_whitespace()
                .map(|c| c.parse().expect("valid card"))
                .collect()
        };
        let hands = [
            hand("2C 6C 7C 8C 6D 7D 8D 9D 6H 7H 8H 3S 4S"), // North: leads 2♣
            hand("9C TC AC TD JD QD 9H TH JH 2S 5S 6S 7S"), // East: wins T1, leads a spade
            hand("3C 4C 5C 2D 3D 4D 5D 2H 3H 4H 5H JS QS"), // South: only spades are J♠ Q♠
            hand("JC QC KC KD AD QH KH AH 8S 9S TS KS AS"), // West
        ];
        let mut round = Round::from_deal(Rules::new(), PassDirection::Hold, hands)
            .expect("a full partition deals");
        for (seat, card) in [
            (Seat::North, "2C"),
            (Seat::East, "AC"),
            (Seat::South, "3C"),
            (Seat::West, "JC"),
            (Seat::East, "2S"), // East won trick 1; now it leads a low spade
        ] {
            round
                .play(seat, card.parse().expect("valid card"))
                .expect("a legal scripted play");
        }

        let table = Table::new(round);
        assert_eq!(
            table.turn(),
            Some(Seat::South),
            "South is to follow the spade"
        );
        let view = table.view(Seat::South);

        let cands = play_candidates(&view);
        assert_eq!(
            cands.iter().filter(|c| !c.shoot).count(),
            2,
            "Q♠ must not collapse into J♠"
        );
        assert!(
            cands
                .iter()
                .any(|c| matches!(c.choice, Choice::Play(card) if card == Card::QUEEN_OF_SPADES)),
            "Q♠ is weighed as its own play",
        );

        let mut solver = MonteCarloBot::new(StdRng::seed_from_u64(7)).samples(64);
        assert!(!solver.assess(&view).is_empty(), "the hint offers a read");
    }

    #[test]
    fn assess_lists_an_all_equivalent_class_rather_than_refusing() {
        // South follows a led spade holding only 5♠ and 7♠.  From the hand
        // they look like two separate plays; but 6♠ is gone in a completed
        // trick, so they are interchangeable — one class, one candidate.  A
        // human cannot read that off the hand, so the hint must still list
        // both tied instead of going blank.
        let hand = |cards: &str| -> Hand {
            cards
                .split_whitespace()
                .map(|c| c.parse().expect("valid card"))
                .collect()
        };
        let hands = [
            hand("2C 3C 4C 5C 2S 3S 4S 2D 3D 4D 5D 2H 3H"), // North: leads 2♣
            hand("AC KC QC 9S 6D 7D 8D 9D 5H 6H 7H 8H 9H"), // East: wins T1, leads 9♠
            hand("6C 7C 8C 9C TC JC 5S 7S TD JD QD KD AD"), // South: spades are 5♠ 7♠ only
            hand("6S 8S TS JS QS KS AS 4H TH JH QH KH AH"), // West: club-void, sheds 6♠
        ];
        let mut round = Round::from_deal(Rules::new(), PassDirection::Hold, hands)
            .expect("a full partition deals");
        for (seat, card) in [
            (Seat::North, "2C"),
            (Seat::East, "AC"),
            (Seat::South, "6C"),
            (Seat::West, "6S"), // the gap card, gone in a completed trick
            (Seat::East, "9S"), // East won trick 1; it leads above the 5-7 span
        ] {
            round
                .play(seat, card.parse().expect("valid card"))
                .expect("a legal scripted play");
        }

        let table = Table::new(round);
        assert_eq!(
            table.turn(),
            Some(Seat::South),
            "South is to follow the spade"
        );
        let view = table.view(Seat::South);

        // One equivalence class, so the bot weighs a single candidate...
        assert_eq!(
            play_candidates(&view).len(),
            1,
            "5♠ and 7♠ collapse across the played 6♠",
        );

        // ...yet the hint lists both legal plays, tied and flagged.
        let mut solver = MonteCarloBot::new(StdRng::seed_from_u64(11)).samples(64);
        let rows = solver.assess(&view);
        assert_eq!(rows.len(), 2, "both equivalent spades are shown");
        assert!(
            rows.iter().all(|r| r.recommended),
            "an equivalent class is all-tied",
        );
        assert!(
            rows[0].equity == rows[1].equity && rows[0].ev == rows[1].ev,
            "equivalent plays carry the same read",
        );
    }

    #[test]
    fn a_rank_in_the_live_trick_does_not_bridge_a_sequence() {
        // South follows a led J♦ holding 7♦ T♦ Q♦ K♦ A♦, with 8♦ and 9♦ gone
        // in trick 1.  The J♦ is committed but still contests THIS trick:
        // Q♦ K♦ A♦ take it and 7♦ T♦ do not, so the holding is two classes,
        // not one.  Counting the live J♦ as gone collapsed all five.
        let hand = |cards: &str| -> Hand {
            cards
                .split_whitespace()
                .map(|c| c.parse().expect("valid card"))
                .collect()
        };
        let hands = [
            hand("9D 2D 3D 4D 2S 3S 4S 5S 6S 2H 3H 4H 5H"), // North: club-void, sheds 9♦
            hand("TC JC QC KC AC JD QS KS AS JH QH KH AH"), // East: wins T1, leads J♦
            hand("2C 3C 4C 5C 6C 7C 8C 9C 7D TD QD KD AD"), // South: leads 2♣, follows the J♦
            hand("8D 5D 6D 7S 8S 9S TS JS 6H 7H 8H 9H TH"), // West: club-void, sheds 8♦
        ];
        let mut round = Round::from_deal(Rules::new(), PassDirection::Hold, hands)
            .expect("a full partition deals");
        for (seat, card) in [
            (Seat::South, "2C"),
            (Seat::West, "8D"),
            (Seat::North, "9D"),
            (Seat::East, "AC"),
            (Seat::East, "JD"), // East won trick 1; now it leads into the gap
        ] {
            round
                .play(seat, card.parse().expect("valid card"))
                .expect("a legal scripted play");
        }

        let table = Table::new(round);
        assert_eq!(
            table.turn(),
            Some(Seat::South),
            "South is to follow the diamond"
        );
        let view = table.view(Seat::South);

        let plays: Vec<Card> = play_candidates(&view)
            .iter()
            .filter(|c| !c.shoot)
            .filter_map(|c| match c.choice {
                Choice::Play(card) => Some(card),
                Choice::Pass(_) => None,
            })
            .collect();
        let jack: Card = "JD".parse().expect("valid card");
        assert!(
            plays.iter().any(|card| card.rank > jack.rank),
            "a play that takes the trick is weighed: {plays:?}",
        );
        assert!(
            plays.iter().any(|card| card.rank < jack.rank),
            "a play that ducks the trick is weighed: {plays:?}",
        );
    }

    #[test]
    fn void_respecting_worlds() {
        // Play until somebody shows a void, then check every sampled world
        // honors it.
        let mut rng = StdRng::seed_from_u64(21);
        let mut table = Table::deal(Rules::new(), PassDirection::Hold, &mut rng);
        let mut bot = crate::HeuristicBot::new();
        let (observer, void_seat, void_suit) = loop {
            table.step(&mut bot).expect("greedy plays are legal");
            let round = table.round();
            assert_eq!(round.phase(), Phase::Playing, "a void shows up first");
            let found = Seat::ALL.iter().find_map(|&seat| {
                let observer = round.turn()?;
                Suit::ASC
                    .into_iter()
                    .find(|&suit| observer != seat && table.view(observer).is_void(seat, suit))
                    .map(|suit| (observer, seat, suit))
            });
            if let Some(found) = found {
                break found;
            }
        };

        let view = table.view(observer);
        let mut mc = MonteCarloBot::new(StdRng::seed_from_u64(2));
        for world in mc.sample_worlds(&view, 16) {
            assert_eq!(world.hand(void_seat)[void_suit], Holding::EMPTY);
        }
    }

    #[test]
    fn assess_ranks_candidates_and_flags_the_bots_pick() {
        let table = mid_round();
        let me = table.turn().expect("a mid-round table has a mover");
        let view = table.view(me);

        let mut solver = MonteCarloBot::new(StdRng::seed_from_u64(7)).samples(64);
        let mut chooser = MonteCarloBot::new(StdRng::seed_from_u64(7)).samples(64);

        let rows = solver.assess(&view);
        if rows.is_empty() {
            // A forced spot: the chooser must agree it is forced.
            assert!(view.legal_plays().len() == 1 || play_candidates(&view).len() < 2);
            return;
        }
        for row in &rows {
            assert!((0.0..=1.0).contains(&row.equity));
            assert!(row.ev >= 0.0);
        }
        assert!(rows.windows(2).all(|w| w[0].equity >= w[1].equity));
        assert_eq!(rows.iter().filter(|r| r.recommended).count(), 1);

        let picked = rows.iter().find(|r| r.recommended).expect("a flagged pick");
        let played = chooser.play_card(&view);
        assert!(
            picked.action.ends_with(&format!("play {played}")),
            "{} is the card {played} was picked for",
            picked.action,
        );
    }

    #[test]
    fn pass_assessment_covers_twenty_triples() {
        let mut rng = StdRng::seed_from_u64(5);
        let table = Table::deal(Rules::new(), PassDirection::Left, &mut rng);
        let me = table.turn().expect("passing starts at North");
        let view = table.view(me);
        let cands = pass_candidates(&view, HeuristicConfig::default());
        assert!(cands.iter().filter(|c| !c.shoot).count() >= 20);
        assert_eq!(cands.iter().filter(|c| c.shoot).count(), 1, "the moon pass");

        let mut bot = MonteCarloBot::new(StdRng::seed_from_u64(6)).samples(16);
        let picks = bot.pass_cards(&view);
        assert_eq!(
            picks.into_iter().collect::<Hand>().len(),
            3,
            "three distinct cards"
        );
        for card in picks {
            assert!(view.hand().contains(card));
        }
        assert_eq!(bot.moon_passes(), 0);
    }

    #[test]
    fn pass_candidate_zero_is_the_greedy_incumbent() {
        // The invariant the significance gate defends: candidate 0 is the
        // pass the policy would make unaided.  It used to hold by accident,
        // two sorts of the same key agreeing; the sequential policy rescores
        // against the shrinking hand and agrees only on the first card, so
        // the incumbent is now built rather than inferred.  This is the hand
        // where they part: flat ranks J♠ third, sequential takes 6♥.
        fn hand(text: &str) -> Hand {
            text.parse().expect("a valid hand")
        }
        let mine = hand("234679.2K.6A.68J");
        let mut hands = [mine, Hand::EMPTY, Hand::EMPTY, Hand::EMPTY];
        for (i, card) in (Hand::ALL - mine).into_iter().enumerate() {
            hands[1 + i % 3].insert(card);
        }
        let round = Round::from_deal(Rules::new(), PassDirection::Left, hands).unwrap();
        let table = Table::new(round);
        let config = HeuristicConfig::default();

        let incumbent = greedy_pass(mine, config);
        let cands = pass_candidates(&table.view(Seat::North), config);
        assert!(
            matches!(cands[0].choice, Choice::Pass(triple) if triple == incumbent),
            "candidate 0 is not the greedy pass: {}",
            cands[0].label,
        );

        // And it is offered once: the pool regenerates the same set, since
        // every card of it ranks inside the top six.
        let set: Hand = incumbent.into_iter().collect();
        assert_eq!(
            cands
                .iter()
                .filter(|candidate| !candidate.shoot)
                .filter(|candidate| matches!(candidate.choice, Choice::Pass(triple)
                    if triple.into_iter().collect::<Hand>() == set))
                .count(),
            1,
            "the pool re-rolled the incumbent as a second candidate",
        );
    }

    #[test]
    fn pass_candidates_can_create_a_void_outside_the_top_six() {
        fn hand(text: &str) -> Hand {
            text.parse().expect("a valid hand")
        }
        let mine = hand("234.QKA.QKA.JQKA");
        let mut hands = [mine, Hand::EMPTY, Hand::EMPTY, Hand::EMPTY];
        for (i, card) in (Hand::ALL - mine).into_iter().enumerate() {
            hands[1 + i % 3].insert(card);
        }
        let round = Round::from_deal(Rules::new(), PassDirection::Left, hands).unwrap();
        let table = Table::new(round);
        let void = hand("234...");
        assert!(
            pass_candidates(&table.view(Seat::North), HeuristicConfig::default())
                .iter()
                .any(|candidate| matches!(candidate.choice, Choice::Pass(cards)
                if cards.into_iter().collect::<Hand>() == void))
        );
    }

    #[test]
    fn a_hand_that_cannot_win_a_trick_does_not_try() {
        // The guard against the opposite failure.  A moon candidate is on
        // offer at every pass, so it has to lose on a hand with no length,
        // no ace and nothing above a five: the shot dies on trick two and
        // the rollout charges us for having kept the queen.
        fn hand(text: &str) -> Hand {
            text.parse().expect("a valid hand")
        }
        let hands = [
            hand("234.234.234.2345"), // North: the worst hand in the deck
            hand("5678.567.567.678"),
            hand("9TJ.89TJ.89T.9TJ"),
            hand("QKA.QKA.JQKA.QKA"),
        ];
        let round = Round::from_deal(Rules::new(), PassDirection::Left, hands)
            .expect("a full partition deals");
        let table = Table::new(round);
        let view = table.view(Seat::North);

        let mut bot = MonteCarloBot::new(StdRng::seed_from_u64(0xC0DE)).samples(128);
        let picked = bot
            .assess(&view)
            .into_iter()
            .find(|row| row.recommended)
            .expect("a flagged pick");
        assert!(
            !picked.action.starts_with("shoot"),
            "shot the moon holding nothing: {}",
            picked.action,
        );
    }

    #[test]
    fn seeded_pick_is_identical_across_serial_and_parallel_builds() {
        // The `parallel` feature must not change a single decision: batch
        // results are collected in world order and reduced sequentially in
        // both builds.  CI runs the suite with and without the feature; a
        // failure in only one build means the parallel reduce stopped
        // being order-exact.  Re-pin the expected card whenever sampling
        // logic changes.
        let table = mid_round();
        let me = table.turn().expect("a mid-round table has a mover");
        let mut bot = MonteCarloBot::new(StdRng::seed_from_u64(11)).samples(64);
        let first = bot.play_card(&table.view(me));
        let mut again = MonteCarloBot::new(StdRng::seed_from_u64(11)).samples(64);
        assert_eq!(first, again.play_card(&table.view(me)));
    }

    #[test]
    fn breaks_a_moon_the_rollouts_never_see() {
        // The rollout policy models every opponent as a greedy ducker who
        // never shoots, so a moon in progress is invisible to the search.
        // Drive a position where East has already swept the Q♠ (13 points),
        // then confirm the MC bot takes the trick to break the sweep instead
        // of ducking the way its own rollout policy would.
        fn card(text: &str) -> Card {
            text.parse().expect("a valid card")
        }
        fn hand(cards: &[&str]) -> Hand {
            cards.iter().map(|c| card(c)).collect()
        }
        struct Fixed(Card);
        impl Strategy for Fixed {
            fn pass_cards(&mut self, _: &View<'_>) -> [Card; 3] {
                unreachable!()
            }
            fn play_card(&mut self, _: &View<'_>) -> Card {
                self.0
            }
        }

        let hands = [
            // North: leads 2♣, and ends holding S3 (duck) vs SK (break).
            hand(&[
                "2♣", "3♣", "3♠", "K♠", "A♦", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "T♥",
            ]),
            // East: wins two club tricks and scoops the dumped Q♠.
            hand(&[
                "A♣", "K♣", "Q♣", "J♣", "5♠", "6♠", "7♠", "8♠", "9♠", "T♠", "J♠", "A♠", "2♦",
            ]),
            // South: void in clubs, dumps Q♠ into East's second trick.
            hand(&[
                "Q♠", "2♠", "4♠", "J♥", "Q♥", "K♥", "A♥", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦",
            ]),
            // West: void in spades, never overtakes the sweep.
            hand(&[
                "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "T♣", "9♦", "T♦", "J♦", "Q♦", "K♦", "2♥",
            ]),
        ];
        let round = Round::from_deal(Rules::new(), PassDirection::Hold, hands).unwrap();
        let mut table = Table::new(round);
        let script = [
            (Seat::North, "2♣"), // trick 1: clubs, East wins with A♣
            (Seat::East, "A♣"),
            (Seat::South, "3♦"), // void in clubs, sheds a diamond (no T1 penalties)
            (Seat::West, "4♣"),
            (Seat::East, "K♣"), // trick 2: East leads, South dumps the queen
            (Seat::South, "Q♠"),
            (Seat::West, "5♣"),
            (Seat::North, "3♣"),
            (Seat::East, "5♠"), // trick 3: East leads a low spade into North
            (Seat::South, "4♠"),
            (Seat::West, "2♥"), // keep the trick costly, so plain greedy ducks
        ];
        for (seat, text) in script {
            assert_eq!(table.turn(), Some(seat));
            table.step(&mut Fixed(card(text))).unwrap();
        }

        let view = table.view(Seat::North);
        assert_eq!(view.points_taken(Seat::East), 13, "East swept the queen");
        // Greedy — the rollout policy — would duck with the 3♠; the moon
        // defense overrides it to break the sweep with the cheapest winner.
        assert_eq!(
            greedy_play(
                view.legal_plays(),
                view.current_trick().unwrap(),
                view.played()
            ),
            card("3♠")
        );
        let mut bot = MonteCarloBot::new(StdRng::seed_from_u64(1)).samples(64);
        assert_eq!(bot.play_card(&view), card("K♠"));
        assert_eq!(bot.moon_attempts(), 0);

        // East already holds points, so the moon is dead for North and the
        // shoot candidate is not even generated.
        assert!(
            !play_candidates(&view).iter().any(|c| c.shoot),
            "a dead moon is not a candidate",
        );
    }

    #[test]
    fn a_laydown_moon_is_rolled_as_one() {
        fn card(text: &str) -> Card {
            text.parse().expect("a valid card")
        }
        fn hand(text: &str) -> Hand {
            text.parse().expect("a valid hand")
        }

        // North holds the top of every suit but diamonds, and never loses
        // the lead long enough for anyone to lead one.  East, void in
        // spades, discards on every spade lead — a heart under the greedy
        // policy, a diamond once the table knows it is defending a moon.
        let hands = [
            hand("QKA..QKA.29TJQKA"), // North: the monster, and the 2♠ ballast
            hand("2345.234567.234."), // East: holds the deuce, void in spades
            hand("6789.89T.567.456"),
            hand("TJ.JQKA.89TJ.378"),
        ];
        let mut round = Round::from_deal(Rules::new(), PassDirection::Hold, hands)
            .expect("a full partition deals");
        for (seat, text) in [(Seat::East, "2♣"), (Seat::South, "6♣"), (Seat::West, "T♣")] {
            round
                .play(seat, card(text))
                .expect("a legal scripted play, North last to the deuce");
        }

        let ace = Choice::Play(card("A♣"));
        let mut continuation = round.clone();
        assert_eq!(
            ace.roll(&mut continuation, Seat::North, true).shooter(),
            Some(Seat::North),
            "the shooting continuation takes all thirteen tricks",
        );
        // End to end, against three bots that defend: the search has to find
        // the shot *and* carry it across all thirteen tricks.  The point-aware
        // greedy policy can also take this clean opening trick now, so the
        // fixture no longer requires the non-shoot continuation to fail.
        let mut bot = MonteCarloBot::new(StdRng::seed_from_u64(3)).samples(64);
        let mut east = HeuristicBot::new();
        let mut south = HeuristicBot::new();
        let mut west = HeuristicBot::new();
        let result = Table::new(round)
            .play([&mut bot, &mut east, &mut south, &mut west])
            .expect("no seat cheats");
        assert_eq!(result.shooter(), Some(Seat::North), "the bot shot it");
        assert_eq!(bot.moon_attempts(), 1);
    }
}