native-solver 0.1.0

Heads-up postflop No-Limit Hold'em solver: hand evaluator, ranges, betting trees, and combo-vectorized CFR / CFR+ / DCFR.
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
//! Betting tree spanning one or more streets, heads-up. OOP (out of
//! position) acts first on every street. Once a betting round ends without
//! a fold, a `Chance` node deals the next community card (unless the board
//! is already complete) before a fresh betting round starts; on the river
//! it resolves straight to showdown. Bet/raise sizes are configured as
//! fractions of the pot and clipped to the smaller of the two stacks (no
//! side pots — both players start each street with equal remaining stacks,
//! so this is exact, not an approximation). Chip amounts are exact integers
//! ([`Chips`]), so an all-in leaves exactly zero behind at any input scale.

use super::chips::Chips;
use crate::card::{mask_has, mask_of, Card, CardSet};

/// Setup for betting starting from a given (partial or complete) board.
///
/// Build one with [`GameConfig::new`] and the `with_*` methods. Without
/// them the game has no bet sizes, so every street is checked down.
///
/// ```
/// use native_solver::{build_tree, Card, Chips, GameConfig};
///
/// let board: Vec<Card> = ["Ks", "Th", "7s", "4d", "2s"]
///     .iter()
///     .map(|s| s.parse())
///     .collect::<Result<_, _>>()?;
/// let config = GameConfig::new(board, Chips::whole(100), Chips::whole(100))
///     .with_bet_sizes([0.5, 1.0])
///     .with_max_raises(1);
/// assert_eq!(build_tree(&config)?.num_decision_nodes, 8);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct GameConfig {
    pub(crate) board: Vec<Card>,
    pub(crate) pot: Chips,
    pub(crate) effective_stack: Chips,
    pub(crate) bet_sizes: Vec<f64>,
    pub(crate) max_raises: u32,
    pub(crate) suit_isomorphism: bool,
}

impl GameConfig {
    /// A game on `board` with `pot` in the middle and `effective_stack` behind.
    ///
    /// `board` is the flop, turn, or river (3, 4, or 5 cards). The game starts with
    /// no bet sizes, no raises, and no suit-isomorphism reduction.
    pub fn new(board: Vec<Card>, pot: Chips, effective_stack: Chips) -> GameConfig {
        GameConfig {
            board,
            pot,
            effective_stack,
            bet_sizes: Vec::new(),
            max_raises: 0,
            suit_isomorphism: false,
        }
    }

    /// Sets the bet and raise sizes, as fractions of the pot, for every street.
    ///
    /// Each amount is rounded half-to-even to a whole [`Chips`] unit, clipped to
    /// the available stack, and deduplicated. No all-in size is added: include a
    /// large enough fraction for one.
    pub fn with_bet_sizes(mut self, bet_sizes: impl Into<Vec<f64>>) -> GameConfig {
        self.bet_sizes = bet_sizes.into();
        self
    }

    /// Sets the maximum number of raises per street (the count resets each
    /// street).
    pub fn with_max_raises(mut self, max_raises: u32) -> GameConfig {
        self.max_raises = max_raises;
        self
    }

    /// Groups strategically identical chance cards to shrink the tree.
    ///
    /// Cards of the same rank whose suits are both absent from the board are
    /// interchangeable, so the tree recurses once per such class and the trainer
    /// derives the other members' values by relabeling suits. Results match the
    /// unreduced tree (see [the conventions](crate::conventions)). Off by
    /// default.
    pub fn with_suit_isomorphism(mut self, suit_isomorphism: bool) -> GameConfig {
        self.suit_isomorphism = suit_isomorphism;
        self
    }

    /// The board the first street starts on.
    pub fn board(&self) -> &[Card] {
        &self.board
    }

    /// Chips already in the pot at the start of the first street.
    pub fn pot(&self) -> Chips {
        self.pot
    }

    /// Each player's remaining stack at the start of the first street.
    pub fn effective_stack(&self) -> Chips {
        self.effective_stack
    }

    /// Bet and raise sizes, as fractions of the pot.
    pub fn bet_sizes(&self) -> &[f64] {
        &self.bet_sizes
    }

    /// Maximum number of raises per street.
    pub fn max_raises(&self) -> u32 {
        self.max_raises
    }

    /// Whether chance nodes are grouped by suit isomorphism.
    pub fn suit_isomorphism(&self) -> bool {
        self.suit_isomorphism
    }
}

/// A seat in the heads-up hand.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Player {
    /// Out of position: acts first on every street. Index 0.
    Oop,
    /// In position: acts last on every street. Index 1.
    Ip,
}

impl Player {
    /// Both players in index order, so `Player::ALL[i].index() == i`.
    pub const ALL: [Player; 2] = [Player::Oop, Player::Ip];

    /// The opponent.
    pub fn other(self) -> Player {
        match self {
            Player::Oop => Player::Ip,
            Player::Ip => Player::Oop,
        }
    }

    /// `0` for OOP and `1` for IP: the index into every `[_; 2]` per-player array.
    pub fn index(self) -> usize {
        match self {
            Player::Oop => 0,
            Player::Ip => 1,
        }
    }
}

/// A betting action at a decision node.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Action {
    /// Pass with no bet to call.
    Check,
    /// Open the betting with this many chips.
    Bet(Chips),
    /// Match the outstanding bet.
    Call,
    /// Give up the pot.
    Fold,
    /// Call and add this many chips on top (not the raise-to total).
    Raise(Chips),
}

/// How a hand ends, with the payoffs the solver uses.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Terminal {
    /// A player folded and `winner` takes the pot.
    ///
    /// `invested` is each player's total contribution to the pot at this point
    /// (including the pre-street `pot`, split evenly as each player's
    /// baseline), in input chips. Built trees derive these from exact
    /// [`Chips`]; they stay `f64` because they are the payoffs the solver
    /// kernels consume.
    Fold {
        /// The player who did not fold.
        winner: Player,
        /// `[OOP, IP]` total contribution, in input chips.
        invested: [f64; 2],
    },
    /// Both players reach showdown on a complete board; the better hand wins
    /// the opponent's `invested` and a tie splits the pot.
    Showdown {
        /// `[OOP, IP]` total contribution, in input chips.
        invested: [f64; 2],
    },
}

/// A node of the betting tree.
#[derive(Debug, Clone, PartialEq)]
pub enum Node {
    /// A player chooses one of `actions`.
    Decision {
        /// Dense id in `0..Tree::num_decision_nodes`, assigned in depth-first
        /// preorder.
        /// Strategies and evaluations are keyed by it.
        id: usize,
        /// The player to act.
        player: Player,
        /// The legal actions, in a fixed order.
        actions: Vec<Action>,
        /// One subtree per action, in the same order as `actions`.
        children: Vec<Node>,
    },
    /// Deals the next community card.
    ///
    /// Cards are grouped into suit-isomorphism classes (see
    /// [`GameConfig::suit_isomorphism`]): each entry is one class's member
    /// cards plus the subtree built from its representative (`members[0]`).
    /// With isomorphism disabled every class is a singleton, so this is
    /// equivalent to one child per undealt card (52 minus however many board
    /// cards have been dealt so far along this path).
    Chance {
        /// `(class members, subtree)` per suit-isomorphism class.
        children: Vec<(Vec<Card>, Node)>,
    },
    /// The hand is over.
    Terminal(Terminal),
}

/// Why a board was rejected.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum BoardError {
    /// The board must have 3 (flop), 4 (turn), or 5 (river) cards.
    WrongSize {
        /// The number of cards supplied.
        len: usize,
    },
    /// The same card appears more than once.
    DuplicateCard {
        /// The repeated card.
        card: Card,
    },
}

impl std::fmt::Display for BoardError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::WrongSize { len } => write!(f, "board has {len} cards; expected 3, 4, or 5"),
            Self::DuplicateCard { card } => {
                write!(f, "board card {card} appears more than once")
            }
        }
    }
}

impl std::error::Error for BoardError {}

/// Checks that `board` is a legal flop, turn, or river.
pub(crate) fn validate_board(board: &[Card]) -> Result<(), BoardError> {
    if !(3..=5).contains(&board.len()) {
        return Err(BoardError::WrongSize { len: board.len() });
    }
    let mut seen = CardSet::EMPTY;
    for &card in board {
        if !seen.insert(card) {
            return Err(BoardError::DuplicateCard { card });
        }
    }
    Ok(())
}

/// Why a betting tree was not built.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum TreeError {
    /// [`GameConfig::board`] is not a legal flop, turn, or river.
    Board(BoardError),
    /// [`GameConfig::pot`] is zero or negative.
    NonPositivePot,
    /// [`GameConfig::effective_stack`] is negative.
    NegativeStack,
    /// A bet size is zero, negative, or not finite.
    InvalidBetSize {
        /// Position of the size in [`GameConfig::bet_sizes`].
        index: usize,
    },
    /// The tree would need more decision nodes than the supplied budget.
    TooManyDecisionNodes {
        /// The decision-node budget that was exceeded.
        limit: usize,
    },
}

impl std::fmt::Display for TreeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Board(error) => write!(f, "invalid board: {error}"),
            Self::NonPositivePot => write!(f, "pot must be positive"),
            Self::NegativeStack => write!(f, "effective stack must not be negative"),
            Self::InvalidBetSize { index } => write!(
                f,
                "bet size at index {index} must be a positive, finite pot fraction"
            ),
            Self::TooManyDecisionNodes { limit } => write!(
                f,
                "game tree exceeds {limit} decision nodes; reduce bet sizes, raises, or start on a later street"
            ),
        }
    }
}

impl std::error::Error for TreeError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Board(error) => Some(error),
            _ => None,
        }
    }
}

impl From<BoardError> for TreeError {
    fn from(error: BoardError) -> Self {
        Self::Board(error)
    }
}

/// Rejects a configuration that does not describe a legal game.
fn validate_config(cfg: &GameConfig) -> Result<(), TreeError> {
    validate_board(&cfg.board)?;
    if cfg.pot <= Chips::ZERO {
        return Err(TreeError::NonPositivePot);
    }
    if cfg.effective_stack < Chips::ZERO {
        return Err(TreeError::NegativeStack);
    }
    if let Some(index) = cfg
        .bet_sizes
        .iter()
        .position(|&size| !size.is_finite() || size <= 0.0)
    {
        return Err(TreeError::InvalidBetSize { index });
    }
    Ok(())
}

/// A betting tree and the board it starts from.
#[derive(Debug, Clone, PartialEq)]
pub struct Tree {
    /// The board the first street starts on: 3, 4, or 5 cards.
    pub board: Vec<Card>,
    /// The first node: OOP's first decision, unless no stack is left to bet.
    pub root: Node,
    /// The number of decision nodes; their ids are `0..num_decision_nodes`.
    pub num_decision_nodes: usize,
}

/// Builds the full betting tree for `cfg`.
///
/// The tree can grow very large; use [`build_tree_bounded`] for untrusted
/// input.
///
/// # Examples
///
/// ```
/// use native_solver::{build_tree, Card, Chips, GameConfig, Node};
///
/// let board = ["Ks", "Th", "7s", "4d"]
///     .iter()
///     .map(|card| card.parse())
///     .collect::<Result<Vec<Card>, _>>()?;
/// let config = GameConfig::new(board, Chips::whole(100), Chips::whole(200)).with_bet_sizes([0.5]);
/// let tree = build_tree(&config)?;
///
/// // OOP acts first, and decision ids run from 0.
/// assert!(matches!(tree.root, Node::Decision { id: 0, .. }));
/// assert!(tree.num_decision_nodes > 0);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
/// Returns an error if the board is not a legal flop, turn, or river, the
/// pot is not positive, the stack is negative, or a bet size is not a
/// positive finite number.
pub fn build_tree(cfg: &GameConfig) -> Result<Tree, TreeError> {
    build_tree_bounded(cfg, usize::MAX)
}

/// Builds the betting tree, stopping once it would exceed `max_decision_nodes`.
///
/// Callers must also bound input size and recursion depth (notably
/// `max_raises`). Successful trees have the same node ids and actions as
/// [`build_tree`].
///
/// # Errors
/// Returns the same configuration errors as [`build_tree`], or
/// [`TreeError::TooManyDecisionNodes`] when the budget is exceeded.
pub fn build_tree_bounded(cfg: &GameConfig, max_decision_nodes: usize) -> Result<Tree, TreeError> {
    validate_config(cfg)?;
    let mut budget = BuildBudget {
        next_id: 0,
        max_decision_nodes,
    };
    let stack = [cfg.effective_stack, cfg.effective_stack];
    let root = build(
        cfg,
        &cfg.board,
        Player::Oop,
        [Chips::ZERO; 2],
        stack,
        None,
        false,
        0,
        &mut budget,
    )?;
    Ok(Tree {
        board: cfg.board.clone(),
        root,
        num_decision_nodes: budget.next_id,
    })
}

struct BuildBudget {
    next_id: usize,
    max_decision_nodes: usize,
}

impl BuildBudget {
    fn allocate_id(&mut self) -> Result<usize, TreeError> {
        if self.next_id >= self.max_decision_nodes {
            return Err(TreeError::TooManyDecisionNodes {
                limit: self.max_decision_nodes,
            });
        }
        let id = self.next_id;
        self.next_id += 1;
        Ok(id)
    }
}

/// Suits that appear nowhere in `board` — any permutation among them maps
/// the board to itself, so cards of the same rank in different free suits
/// are strategically interchangeable at this point in the tree.
fn free_suits(board: &[Card]) -> [bool; 4] {
    let mut used = [false; 4];
    for &c in board {
        used[c.suit() as usize] = true;
    }
    [!used[0], !used[1], !used[2], !used[3]]
}

/// Groups the undealt cards into suit-isomorphism classes: cards of the
/// same rank whose suits are both free are one class (any one of them can
/// be trained and the rest derived by relabeling — see `cfr.rs`); a card
/// in a suit already pinned by the board is always its own singleton
/// class. With `suit_isomorphism` off, every class is a singleton
/// (equivalent to one child per undealt card).
/// Not tied to [`GameConfig`] (only needs the flag, not the rest of the
/// setup) so `cfr.rs` can also call it directly for its own internal
/// purposes — see `cfr::avg_score`, which always groups this way
/// regardless of whether the *tree* was built with isomorphism, since
/// grouping same-rank free-suit cards is always a valid equivalence.
pub(crate) fn deal_classes(suit_isomorphism: bool, board: &[Card]) -> Vec<Vec<Card>> {
    let used = mask_of(board);
    let free = if suit_isomorphism {
        free_suits(board)
    } else {
        [false; 4]
    };

    let mut classes: Vec<Vec<Card>> = Vec::new();
    let mut free_rank_class: [Option<usize>; 13] = [None; 13];
    for c in Card::all() {
        if mask_has(used, c) {
            continue;
        }
        if free[c.suit() as usize] {
            let r = c.rank() as usize;
            match free_rank_class[r] {
                Some(idx) => classes[idx].push(c),
                None => {
                    free_rank_class[r] = Some(classes.len());
                    classes.push(vec![c]);
                }
            }
        } else {
            classes.push(vec![c]);
        }
    }
    classes
}

/// Ends the current betting round: showdown if the board is already
/// complete (5 cards), otherwise a `Chance` node dealing the next card
/// into a fresh betting round (OOP acts first again, raise count resets).
fn end_of_street(
    cfg: &GameConfig,
    board: &[Card],
    added: [Chips; 2],
    stack: [Chips; 2],
    budget: &mut BuildBudget,
) -> Result<Node, TreeError> {
    if board.len() >= 5 {
        return Ok(Node::Terminal(Terminal::Showdown {
            invested: invested(cfg, added),
        }));
    }
    let mut children = Vec::new();
    for members in deal_classes(cfg.suit_isomorphism, board) {
        let rep = members[0];
        let mut next_board = board.to_vec();
        next_board.push(rep);
        let child = build(
            cfg,
            &next_board,
            Player::Oop,
            added,
            stack,
            None,
            false,
            0,
            budget,
        )?;
        children.push((members, child));
    }
    Ok(Node::Chance { children })
}

/// Terminal payoffs: half the starting pot each plus everything added since.
/// The half may be a half-unit, so it is applied only after leaving [`Chips`].
fn invested(cfg: &GameConfig, added: [Chips; 2]) -> [f64; 2] {
    let baseline = cfg.pot.to_f64() / 2.0;
    [baseline + added[0].to_f64(), baseline + added[1].to_f64()]
}

/// `added` is each player's contribution beyond their half of `cfg.pot`.
#[allow(clippy::too_many_arguments)]
fn build(
    cfg: &GameConfig,
    board: &[Card],
    player: Player,
    added: [Chips; 2],
    stack: [Chips; 2],
    facing: Option<Chips>,
    prev_checked: bool,
    raises_so_far: u32,
    budget: &mut BuildBudget,
) -> Result<Node, TreeError> {
    // Once an all-in is called, neither player has a betting decision left.
    // Keep every remaining public-card chance (and its terminal contribution),
    // but do not allocate two single-action check nodes on each later street.
    // In particular this must precede allocate_id: forced checks must not use
    // the admission budget or leave holes in the trainer's decision IDs.
    if facing.is_none() && stack == [Chips::ZERO; 2] {
        return end_of_street(cfg, board, added, stack, budget);
    }
    let id = budget.allocate_id()?;

    let mut actions = Vec::new();
    let mut children = Vec::new();

    if let Some(to_call) = facing {
        actions.push(Action::Fold);
        children.push(Node::Terminal(Terminal::Fold {
            winner: player.other(),
            invested: invested(cfg, added),
        }));

        let mut call_added = added;
        call_added[player.index()] += to_call;
        let mut call_stack = stack;
        call_stack[player.index()] -= to_call;
        actions.push(Action::Call);
        children.push(end_of_street(cfg, board, call_added, call_stack, budget)?);

        if raises_so_far < cfg.max_raises && stack[player.index()] > to_call {
            let opponent = player.other();
            let pot_after_call = cfg.pot + added[0] + added[1] + to_call;
            let own_room = stack[player.index()] - to_call;
            let opponent_room = stack[opponent.index()];
            let raise_extras =
                sized_amounts(&cfg.bet_sizes, pot_after_call, own_room.min(opponent_room));

            for extra in raise_extras {
                let mut new_added = added;
                new_added[player.index()] += to_call + extra;
                let mut new_stack = stack;
                new_stack[player.index()] -= to_call + extra;
                actions.push(Action::Raise(extra));
                children.push(build(
                    cfg,
                    board,
                    opponent,
                    new_added,
                    new_stack,
                    Some(extra),
                    false,
                    raises_so_far + 1,
                    budget,
                )?);
            }
        }
    } else {
        let check_child = if prev_checked {
            end_of_street(cfg, board, added, stack, budget)?
        } else {
            build(
                cfg,
                board,
                player.other(),
                added,
                stack,
                None,
                true,
                raises_so_far,
                budget,
            )?
        };
        actions.push(Action::Check);
        children.push(check_child);

        if stack[player.index()] > Chips::ZERO {
            let opponent = player.other();
            let pot_now = cfg.pot + added[0] + added[1];
            let room = stack[player.index()].min(stack[opponent.index()]);
            let bet_amounts = sized_amounts(&cfg.bet_sizes, pot_now, room);

            for amt in bet_amounts {
                let mut new_added = added;
                new_added[player.index()] += amt;
                let mut new_stack = stack;
                new_stack[player.index()] -= amt;
                actions.push(Action::Bet(amt));
                children.push(build(
                    cfg,
                    board,
                    opponent,
                    new_added,
                    new_stack,
                    Some(amt),
                    false,
                    raises_so_far,
                    budget,
                )?);
            }
        }
    }

    Ok(Node::Decision {
        id,
        player,
        actions,
        children,
    })
}

/// Pot-fraction amounts clipped to `room`, ascending, without duplicates or
/// non-positive amounts.
fn sized_amounts(fractions: &[f64], pot: Chips, room: Chips) -> Vec<Chips> {
    let mut amounts: Vec<Chips> = fractions
        .iter()
        .map(|&frac| pot.fraction(frac).min(room))
        .filter(|&amt| amt > Chips::ZERO)
        .collect();
    amounts.sort_unstable();
    amounts.dedup();
    amounts
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::card::Card;

    fn river_board() -> Vec<Card> {
        ["2h", "7c", "9d", "Jc", "4s"]
            .iter()
            .map(|s| s.parse::<Card>().unwrap())
            .collect()
    }

    fn turn_board() -> Vec<Card> {
        ["2h", "7c", "9d", "Jc"]
            .iter()
            .map(|s| s.parse::<Card>().unwrap())
            .collect()
    }

    /// Public history as `(player, actions)` per decision plus terminal
    /// investments, with amounts divided by `scale` (which must divide them).
    fn shape(node: &Node, scale: i64, out: &mut Vec<String>) {
        let unscale = |amount: Chips| {
            assert_eq!(
                amount.0 % scale,
                0,
                "{amount:?} is not a multiple of {scale}"
            );
            Chips(amount.0 / scale)
        };
        match node {
            Node::Decision {
                player,
                actions,
                children,
                ..
            } => {
                let actions: Vec<Action> = actions
                    .iter()
                    .map(|action| match *action {
                        Action::Bet(amount) => Action::Bet(unscale(amount)),
                        Action::Raise(amount) => Action::Raise(unscale(amount)),
                        other => other,
                    })
                    .collect();
                out.push(format!("{player:?} {actions:?}"));
                children.iter().for_each(|child| shape(child, scale, out));
            }
            Node::Chance { children } => children
                .iter()
                .for_each(|(_, child)| shape(child, scale, out)),
            Node::Terminal(Terminal::Fold { invested, .. } | Terminal::Showdown { invested }) => {
                out.push(format!("{:?}", invested.map(|chips| chips / scale as f64)));
            }
        }
    }

    /// Replays every path's stacks; asserts amounts are positive, stacks never
    /// go negative, and nobody acts again once an all-in has been called.
    fn check_stacks(node: &Node, stack: [Chips; 2], facing: Option<Chips>, all_in: bool) {
        match node {
            Node::Decision {
                player,
                actions,
                children,
                ..
            } => {
                assert!(!all_in, "decision after a called all-in");
                let p = player.index();
                for (action, child) in actions.iter().zip(children) {
                    let mut next = stack;
                    let (paid, next_facing) = match *action {
                        Action::Check | Action::Fold => (Chips::ZERO, None),
                        Action::Call => (facing.unwrap(), None),
                        Action::Bet(amount) => (amount, Some(amount)),
                        Action::Raise(extra) => (facing.unwrap() + extra, Some(extra)),
                    };
                    if let Action::Bet(amount) | Action::Raise(amount) = *action {
                        assert!(amount > Chips::ZERO);
                    }
                    next[p] -= paid;
                    assert!(next[p] >= Chips::ZERO, "stack went negative");
                    let called_all_in = matches!(action, Action::Call) && next == [Chips::ZERO; 2];
                    check_stacks(child, next, next_facing, called_all_in);
                }
            }
            Node::Chance { children } => children
                .iter()
                .for_each(|(_, child)| check_stacks(child, stack, None, all_in)),
            Node::Terminal(_) => {}
        }
    }

    fn config(
        board: Vec<Card>,
        pot: Chips,
        stack: Chips,
        bet_sizes: Vec<f64>,
        max_raises: u32,
    ) -> GameConfig {
        GameConfig {
            board,
            pot,
            effective_stack: stack,
            bet_sizes,
            max_raises,
            suit_isomorphism: true,
        }
    }

    fn scaled(cfg: &GameConfig, scale: i64) -> GameConfig {
        GameConfig {
            pot: Chips(cfg.pot.0 * scale),
            effective_stack: Chips(cfg.effective_stack.0 * scale),
            ..cfg.clone()
        }
    }

    #[test]
    fn all_in_leaves_no_residual_stack_at_any_scale() {
        // With f64 chips, pot 5.5 / stack 9 left an 8.88e-16 stack after an
        // all-in and built `Raise(8.88e-16)`; scaling by 10 hid it.
        for (pot, stack) in [(5500, 9000), (1500, 3900)] {
            for board in [river_board(), turn_board()] {
                let cfg = config(board, Chips(pot), Chips(stack), vec![0.33, 0.75, 1.5], 3);
                let base = build_tree(&cfg).unwrap().num_decision_nodes;
                for scale in [1, 10, 100] {
                    let tree = build_tree(&scaled(&cfg, scale)).unwrap();
                    assert_eq!(
                        tree.num_decision_nodes, base,
                        "pot {pot} stack {stack} x{scale}"
                    );
                    check_stacks(
                        &tree.root,
                        [cfg.effective_stack; 2].map(|s| Chips(s.0 * scale)),
                        None,
                        false,
                    );
                }
            }
        }
        let river = config(
            river_board(),
            Chips(5500),
            Chips(9000),
            vec![0.33, 0.75, 1.5],
            3,
        );
        assert_eq!(build_tree(&river).unwrap().num_decision_nodes, 26);
    }

    #[test]
    fn tree_shape_is_scale_invariant_when_sizes_need_no_rounding() {
        // Whole-chip pots and stacks with half-pot multiples stay integral in
        // 0.001 units for three halvings (bet + two raises on one street), so
        // every scaled tree must be the base tree with amounts multiplied.
        let mut seed = 0x2545_f491_4f6c_dd1du64;
        let mut next = |bound: u64| {
            seed ^= seed << 13;
            seed ^= seed >> 7;
            seed ^= seed << 17;
            seed % bound
        };
        for _ in 0..200 {
            let pot = Chips::whole(1 + next(40) as i64);
            let stack = Chips::whole(1 + next(120) as i64);
            let sizes: Vec<f64> = (0..1 + next(3))
                .map(|_| [0.5, 1.0, 1.5, 2.0][next(4) as usize])
                .collect();
            let cfg = config(river_board(), pot, stack, sizes, next(3) as u32);
            let mut base = Vec::new();
            shape(&build_tree(&cfg).unwrap().root, 1, &mut base);
            for scale in [10, 100] {
                let mut other = Vec::new();
                shape(
                    &build_tree(&scaled(&cfg, scale)).unwrap().root,
                    scale,
                    &mut other,
                );
                assert_eq!(base, other, "{cfg:?} x{scale}");
            }
        }
    }

    #[test]
    fn arbitrary_thousandth_amounts_keep_stacks_exact() {
        let mut seed = 0x9e37_79b9_7f4a_7c15u64;
        let mut next = |bound: u64| {
            seed ^= seed << 13;
            seed ^= seed >> 7;
            seed ^= seed << 17;
            seed % bound
        };
        for _ in 0..300 {
            let pot = Chips(1 + next(20_000) as i64);
            let stack = Chips(1 + next(60_000) as i64);
            let sizes: Vec<f64> = (0..1 + next(4))
                .map(|_| (1 + next(300)) as f64 / 100.0)
                .collect();
            let cfg = config(river_board(), pot, stack, sizes, next(4) as u32);
            check_stacks(&build_tree(&cfg).unwrap().root, [stack; 2], None, false);
        }
    }

    #[test]
    fn bounded_tree_preserves_ids_actions_and_exact_boundary() {
        let cfg = GameConfig {
            board: turn_board(),
            pot: Chips::whole(100),
            effective_stack: Chips::whole(200),
            bet_sizes: vec![0.5, 1.0],
            max_raises: 1,
            suit_isomorphism: true,
        };
        let original = build_tree(&cfg).unwrap();
        let bounded = build_tree_bounded(&cfg, original.num_decision_nodes).unwrap();
        assert_eq!(
            format!("{:?}", original.root),
            format!("{:?}", bounded.root)
        );
        assert_eq!(original.num_decision_nodes, bounded.num_decision_nodes);
        assert!(build_tree_bounded(&cfg, original.num_decision_nodes - 1).is_err());
        assert!(build_tree_bounded(&cfg, 0).is_err());
    }

    #[test]
    fn bounded_tree_aborts_large_flop_construction() {
        let cfg = GameConfig {
            board: turn_board()[..3].to_vec(),
            pot: Chips::whole(100),
            effective_stack: Chips::whole(100000),
            bet_sizes: vec![0.25, 0.5, 1.0, 2.0],
            max_raises: 4,
            suit_isomorphism: false,
        };
        let error = build_tree_bounded(&cfg, 100).expect_err("must exceed construction budget");
        assert_eq!(error, TreeError::TooManyDecisionNodes { limit: 100 });
        assert!(error.to_string().contains("100 decision nodes"));
    }

    #[test]
    fn check_check_ends_in_showdown_on_the_river() {
        let cfg = GameConfig {
            board: river_board(),
            pot: Chips::whole(100),
            effective_stack: Chips::whole(200),
            bet_sizes: vec![1.0],
            max_raises: 0,
            suit_isomorphism: false,
        };
        let tree = build_tree(&cfg).unwrap();
        let Node::Decision {
            actions, children, ..
        } = &tree.root
        else {
            panic!("expected decision")
        };
        assert_eq!(actions.len(), 2);
        let Node::Decision {
            player: ip_player,
            actions: ip_actions,
            children: ip_children,
            ..
        } = &children[0]
        else {
            panic!("expected IP decision after check")
        };
        assert_eq!(*ip_player, Player::Ip);
        assert_eq!(ip_actions.len(), 2);
        assert!(matches!(
            ip_children[0],
            Node::Terminal(Terminal::Showdown { .. })
        ));
    }

    #[test]
    fn bet_size_is_pot_fraction_and_call_ends_in_showdown_on_the_river() {
        let cfg = GameConfig {
            board: river_board(),
            pot: Chips::whole(100),
            effective_stack: Chips::whole(200),
            bet_sizes: vec![1.0],
            max_raises: 0,
            suit_isomorphism: false,
        };
        let tree = build_tree(&cfg).unwrap();
        let Node::Decision {
            actions, children, ..
        } = &tree.root
        else {
            panic!("expected decision")
        };
        let Action::Bet(amount) = actions[1] else {
            panic!("expected a bet action")
        };
        assert_eq!(amount, Chips::whole(100));

        let Node::Decision {
            actions: ip_actions,
            children: ip_children,
            ..
        } = &children[1]
        else {
            panic!("expected IP decision facing the bet")
        };
        assert_eq!(ip_actions.len(), 2);
        assert!(matches!(
            ip_children[0],
            Node::Terminal(Terminal::Fold { .. })
        ));
        assert!(matches!(
            ip_children[1],
            Node::Terminal(Terminal::Showdown { .. })
        ));
    }

    #[test]
    fn raises_are_capped_by_max_raises_per_street() {
        let cfg = GameConfig {
            board: river_board(),
            pot: Chips::whole(100),
            effective_stack: Chips::whole(1000),
            bet_sizes: vec![1.0],
            max_raises: 1,
            suit_isomorphism: false,
        };
        let tree = build_tree(&cfg).unwrap();
        let Node::Decision { children, .. } = &tree.root else {
            panic!("expected decision")
        };
        let Node::Decision {
            actions,
            children: ip_children,
            ..
        } = &children[1]
        else {
            panic!("expected IP decision facing the bet")
        };
        assert_eq!(actions.len(), 3);
        let Node::Decision {
            actions: oop_actions,
            ..
        } = &ip_children[2]
        else {
            panic!("expected OOP decision facing the raise")
        };
        assert_eq!(oop_actions.len(), 2);
    }

    #[test]
    fn check_check_on_the_turn_deals_a_chance_node_with_48_river_cards() {
        let cfg = GameConfig {
            board: turn_board(),
            pot: Chips::whole(100),
            effective_stack: Chips::whole(200),
            bet_sizes: vec![1.0],
            max_raises: 0,
            suit_isomorphism: false,
        };
        let tree = build_tree(&cfg).unwrap();
        let Node::Decision { children, .. } = &tree.root else {
            panic!("expected decision")
        };
        let Node::Decision {
            children: ip_children,
            ..
        } = &children[0]
        else {
            panic!("expected IP decision after check")
        };
        // Check-check on the turn (4-card board) should deal the river, not show down directly.
        let Node::Chance {
            children: river_deals,
        } = &ip_children[0]
        else {
            panic!("expected a chance node dealing the river")
        };
        // With isomorphism disabled, every class is a singleton: one per undealt card.
        assert_eq!(river_deals.len(), 48); // 52 - 4 board cards
        for (members, _) in river_deals {
            assert_eq!(members.len(), 1);
        }

        // Every river deal leads into a fresh betting round (OOP decision), not straight to showdown.
        for (_, node) in river_deals {
            assert!(matches!(
                node,
                Node::Decision {
                    player: Player::Oop,
                    ..
                }
            ));
        }
    }

    #[test]
    fn river_deals_never_repeat_an_existing_board_card() {
        let cfg = GameConfig {
            board: turn_board(),
            pot: Chips::whole(100),
            effective_stack: Chips::whole(200),
            bet_sizes: vec![1.0],
            max_raises: 0,
            suit_isomorphism: false,
        };
        let tree = build_tree(&cfg).unwrap();
        let Node::Decision { children, .. } = &tree.root else {
            panic!("expected decision")
        };
        let Node::Decision {
            children: ip_children,
            ..
        } = &children[0]
        else {
            panic!("expected IP decision after check")
        };
        let Node::Chance {
            children: river_deals,
        } = &ip_children[0]
        else {
            panic!("expected a chance node dealing the river")
        };
        let board_set = mask_of(&turn_board());
        for (members, _) in river_deals {
            for &card in members {
                assert!(!mask_has(board_set, card));
            }
        }
    }

    #[test]
    fn suit_isomorphism_groups_free_suit_cards_of_the_same_rank() {
        // Board uses only clubs and diamonds -> hearts and spades are free.
        let board: Vec<Card> = ["2c", "7d", "9c"]
            .iter()
            .map(|s| s.parse::<Card>().unwrap())
            .collect();
        let cfg = GameConfig {
            board: board.clone(),
            pot: Chips::whole(100),
            effective_stack: Chips::whole(200),
            bet_sizes: vec![1.0],
            max_raises: 0,
            suit_isomorphism: true,
        };
        let tree = build_tree(&cfg).unwrap();
        let Node::Decision { children, .. } = &tree.root else {
            panic!("expected decision")
        };
        let Node::Decision {
            children: ip_children,
            ..
        } = &children[0]
        else {
            panic!("expected IP decision after check")
        };
        let Node::Chance { children: deals } = &ip_children[0] else {
            panic!("expected a chance node dealing the turn")
        };

        // 49 undealt cards; hearts and spades are free. Rather than
        // hand-derive the exact class breakdown, check the structural
        // invariants directly: every class shares one rank, only free-suit
        // (h/s) cards are ever grouped into a class of size > 1, and every
        // undealt card appears in exactly one class.
        let used = mask_of(&board);
        let mut seen = std::collections::HashSet::new();
        let mut total_members = 0;
        for (members, _) in deals {
            assert!(!members.is_empty());
            let ranks: std::collections::HashSet<u8> = members.iter().map(|&c| c.rank()).collect();
            assert_eq!(
                ranks.len(),
                1,
                "every class member must share the same rank"
            );
            if members.len() > 1 {
                for &c in members {
                    let s = c.suit();
                    assert!(
                        s == 2 || s == 3,
                        "grouped members must use a free suit (h=2, s=3)"
                    );
                }
            }
            for &c in members {
                assert!(
                    !mask_has(used, c),
                    "class must not include an already-used card"
                );
                assert!(
                    seen.insert(c),
                    "each undealt card must appear in exactly one class"
                );
            }
            total_members += members.len();
        }
        assert_eq!(total_members, 52 - board.len());
        // At least one class should actually group 2 free-suit cards together
        // (that's the whole point of isomorphism reduction).
        assert!(deals.iter().any(|(members, _)| members.len() == 2));
        // No class can exceed the number of free suits (2 here).
        assert!(deals.iter().all(|(members, _)| members.len() <= 2));
    }
}