rs_poker 5.0.0

A library to help with any Rust code dealing with poker. This includes card values, suits, hands, hand ranks, 5 card hand strength calculation, 7 card hand strength calulcation, and monte carlo game simulation helpers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
use core::fmt;
use std::fmt::Display;

use approx::abs_diff_eq;
use rand::rng;
use thiserror::Error;

use smallvec::{SmallVec, smallvec};

use crate::core::{Card, CardBitSet, Hand, PlayerBitSet};

use super::errors::GameStateError;

/// Maximum number of players supported (based on PlayerBitSet using u16).
pub const MAX_PLAYERS: usize = 16;

/// Per-player inline vector. Sized to [`MAX_PLAYERS`], so it never spills to the
/// heap (a table holds at most 16 players — the `PlayerBitSet` capacity). This
/// makes cloning a `GameState`, which CFR does once per fast-forward sample,
/// allocation-free.
pub type PlayerVec<T> = SmallVec<[T; MAX_PLAYERS]>;

/// Inline vector for the community board. Hold'em/Omaha boards are at most 5
/// cards, so this never spills either.
pub type BoardVec = SmallVec<[Card; 5]>;

/// Errors that can occur when building a GameState.
#[derive(Debug, Clone, PartialEq, Error)]
pub enum GameStateBuilderError {
    #[error("stacks are required")]
    MissingStacks,

    #[error("big_blind is required")]
    MissingBigBlind,

    #[error("num_players must be between 2 and {max}, got {actual}", max = MAX_PLAYERS)]
    InvalidPlayerCount { actual: usize },

    #[error("dealer_idx {dealer_idx} must be less than num_players {num_players}")]
    InvalidDealerIndex {
        dealer_idx: usize,
        num_players: usize,
    },

    #[error("big_blind must be positive, got {0}")]
    InvalidBigBlind(f32),

    #[error("small_blind must be non-negative, got {0}")]
    InvalidSmallBlind(f32),

    #[error("ante must be non-negative, got {0}")]
    InvalidAnte(f32),

    #[error("stack at index {index} must be non-negative, got {value}")]
    InvalidStack { index: usize, value: f32 },

    #[error("at least 2 players must have positive stacks")]
    InsufficientActivePlayers,

    #[error("hands length {hands_len} must equal num_players {num_players}")]
    HandsLengthMismatch {
        hands_len: usize,
        num_players: usize,
    },

    #[error("player_bet length {bet_len} must equal num_players {num_players}")]
    PlayerBetLengthMismatch { bet_len: usize, num_players: usize },

    #[error("board must have 0, 3, 4, or 5 cards, got {0}")]
    InvalidBoardSize(usize),

    #[error("duplicate card found: {0}")]
    DuplicateCard(Card),
}

/// Builder for constructing `GameState` with validation.
///
/// # Example
///
/// ```
/// use rs_poker::arena::GameStateBuilder;
///
/// let game_state = GameStateBuilder::new()
///     .stacks(vec![100.0, 100.0])
///     .big_blind(10.0)
///     .build()
///     .unwrap();
///
/// assert_eq!(game_state.num_players, 2);
/// assert_eq!(game_state.big_blind, 10.0);
/// assert_eq!(game_state.small_blind, 5.0); // defaults to big_blind / 2
/// ```
#[derive(Default, Clone)]
pub struct GameStateBuilder {
    // Required (no defaults)
    stacks: Option<PlayerVec<f32>>,
    big_blind: Option<f32>,

    // Optional with defaults
    small_blind: Option<f32>,                 // Default: big_blind / 2
    ante: Option<f32>,                        // Default: 0.0
    dealer_idx: Option<usize>,                // Default: 0
    max_raises_per_round: Option<Option<u8>>, // Default: Some(3)

    // For mid-game states (defaults for new games)
    round: Option<Round>,               // Default: Round::Starting
    board: Option<BoardVec>,            // Default: empty
    hands: Option<PlayerVec<Hand>>,     // Default: Hand::default() per player
    player_bet: Option<PlayerVec<f32>>, // Default: 0.0 per player
    round_data: Option<RoundData>,      // Default: computed
}

impl GameStateBuilder {
    /// Create a new `GameStateBuilder`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the stack sizes for each player. Required.
    ///
    /// Accepts any slice-like source (`&[f32]`, `[f32; N]`, `Vec<f32>`); the
    /// values are copied straight into inline storage, so callers never need to
    /// heap-allocate a `Vec` just to hand it over.
    pub fn stacks(mut self, stacks: impl AsRef<[f32]>) -> Self {
        self.stacks = Some(SmallVec::from_slice(stacks.as_ref()));
        self
    }

    /// Set the big blind size. Required.
    pub fn big_blind(mut self, bb: f32) -> Self {
        self.big_blind = Some(bb);
        self
    }

    /// Set the small blind size. Defaults to `big_blind / 2`.
    pub fn small_blind(mut self, sb: f32) -> Self {
        self.small_blind = Some(sb);
        self
    }

    /// Set the ante size. Defaults to `0.0`.
    pub fn ante(mut self, ante: f32) -> Self {
        self.ante = Some(ante);
        self
    }

    /// Set the dealer index. Defaults to `0`.
    pub fn dealer_idx(mut self, idx: usize) -> Self {
        self.dealer_idx = Some(idx);
        self
    }

    /// Set the maximum raises per round. Defaults to `Some(3)`.
    /// Use `None` for unlimited raises.
    pub fn max_raises_per_round(mut self, max: Option<u8>) -> Self {
        self.max_raises_per_round = Some(max);
        self
    }

    /// Convenience method to create stacks with `n` players, each with `stack` chips.
    pub fn num_players_with_stack(mut self, n: usize, stack: f32) -> Self {
        self.stacks = Some(smallvec![stack; n]);
        self
    }

    /// Convenience method to set both big and small blinds at once.
    pub fn blinds(mut self, big: f32, small: f32) -> Self {
        self.big_blind = Some(big);
        self.small_blind = Some(small);
        self
    }

    /// Set the current round. Defaults to `Round::Starting`.
    pub fn round(mut self, round: Round) -> Self {
        self.round = Some(round);
        self
    }

    /// Set the board cards. Defaults to empty.
    pub fn board(mut self, board: impl AsRef<[Card]>) -> Self {
        self.board = Some(SmallVec::from_slice(board.as_ref()));
        self
    }

    /// Set the hands for each player.
    pub fn hands(mut self, hands: impl AsRef<[Hand]>) -> Self {
        self.hands = Some(SmallVec::from_slice(hands.as_ref()));
        self
    }

    /// Set the player bets.
    pub fn player_bet(mut self, bets: impl AsRef<[f32]>) -> Self {
        self.player_bet = Some(SmallVec::from_slice(bets.as_ref()));
        self
    }

    /// Set the round data directly.
    pub fn round_data(mut self, rd: RoundData) -> Self {
        self.round_data = Some(rd);
        self
    }

    /// Build the `GameState`, validating all inputs.
    pub fn build(self) -> Result<GameState, GameStateBuilderError> {
        // Check required fields
        let stacks = self.stacks.ok_or(GameStateBuilderError::MissingStacks)?;
        let big_blind = self
            .big_blind
            .ok_or(GameStateBuilderError::MissingBigBlind)?;

        let num_players = stacks.len();

        // Validate player count
        if !(2..=MAX_PLAYERS).contains(&num_players) {
            return Err(GameStateBuilderError::InvalidPlayerCount {
                actual: num_players,
            });
        }

        // Validate big blind
        if big_blind <= 0.0 || big_blind.is_nan() {
            return Err(GameStateBuilderError::InvalidBigBlind(big_blind));
        }

        // Set defaults and validate small blind
        let small_blind = self.small_blind.unwrap_or(big_blind / 2.0);
        if small_blind < 0.0 || small_blind.is_nan() {
            return Err(GameStateBuilderError::InvalidSmallBlind(small_blind));
        }

        // Validate ante
        let ante = self.ante.unwrap_or(0.0);
        if ante < 0.0 || ante.is_nan() {
            return Err(GameStateBuilderError::InvalidAnte(ante));
        }

        // Validate stacks
        let round = self.round.unwrap_or(Round::Starting);
        let player_bet_ref = self.player_bet.as_deref();
        let mut active_count = 0;
        for (index, &value) in stacks.iter().enumerate() {
            if value < 0.0 || value.is_nan() {
                return Err(GameStateBuilderError::InvalidStack { index, value });
            }
            // A player is "active" if they have chips OR if they're all-in
            // (0 stack but has bet in a non-Starting round)
            let bet = player_bet_ref
                .and_then(|bets| bets.get(index).copied())
                .unwrap_or(0.0);
            if value > 0.0 || (bet > 0.0 && round != Round::Starting) {
                active_count += 1;
            }
        }
        // Only enforce minimum active players for new games (Starting round)
        // Mid-game states or tournament continuations may have fewer active players
        if active_count < 2 && round == Round::Starting {
            return Err(GameStateBuilderError::InsufficientActivePlayers);
        }

        // Validate dealer index
        let dealer_idx = self.dealer_idx.unwrap_or(0);
        if dealer_idx >= num_players {
            return Err(GameStateBuilderError::InvalidDealerIndex {
                dealer_idx,
                num_players,
            });
        }

        // Validate hands length if provided
        if let Some(ref hands) = self.hands
            && hands.len() != num_players
        {
            return Err(GameStateBuilderError::HandsLengthMismatch {
                hands_len: hands.len(),
                num_players,
            });
        }

        // Validate player_bet length if provided
        if let Some(ref bets) = self.player_bet
            && bets.len() != num_players
        {
            return Err(GameStateBuilderError::PlayerBetLengthMismatch {
                bet_len: bets.len(),
                num_players,
            });
        }

        // Validate board size
        let board = self.board.unwrap_or_default();
        let board_len = board.len();
        if board_len != 0 && board_len != 3 && board_len != 4 && board_len != 5 {
            return Err(GameStateBuilderError::InvalidBoardSize(board_len));
        }

        // Check for duplicate cards within the board only
        // We don't check hands because they can legitimately contain board cards
        // (e.g., when using 7-card hands for ranking purposes in tests)
        let mut card_set = CardBitSet::new();
        for card in &board {
            if card_set.contains(*card) {
                return Err(GameStateBuilderError::DuplicateCard(*card));
            }
            card_set.insert(*card);
        }

        let hands = self
            .hands
            .unwrap_or_else(|| smallvec![Hand::default(); num_players]);

        // Build defaults (round was already computed during validation)
        let player_bet = self
            .player_bet
            .unwrap_or_else(|| smallvec![0.0; num_players]);
        let max_raises_per_round = self.max_raises_per_round.unwrap_or(Some(3));

        let round_data = self.round_data.unwrap_or_else(|| {
            RoundData::new(
                num_players,
                big_blind,
                PlayerBitSet::new(num_players),
                dealer_idx,
            )
        });

        // Compute player_active, player_all_in, and total_pot
        let mut player_active = PlayerBitSet::new(num_players);
        let mut player_all_in = PlayerBitSet::default();
        let mut total_pot = 0.0;

        for (idx, (stack, bet)) in stacks.iter().zip(player_bet.iter()).enumerate() {
            total_pot += *bet;

            if *stack <= 0.0 {
                if *bet > 0.0 && round != Round::Starting {
                    // Player is out of money but has bet - they're all in
                    player_all_in.enable(idx);
                } else {
                    // Player has no money and can't play - sitting out
                    player_active.disable(idx);
                }
            }
        }

        Ok(GameState {
            num_players,
            // All vector fields are already inline (the builder copied them into
            // SmallVec storage when set), so the hot path — cloning a GameState
            // per CFR sample — never touches the heap.
            starting_stacks: stacks.clone(),
            stacks,
            big_blind,
            small_blind,
            ante,
            player_active,
            player_all_in,
            player_bet,
            player_winnings: smallvec![0.0; num_players],
            dealer_idx,
            total_pot,
            hands,
            round,
            round_before: round,
            round_data,
            board,
            bb_posted: round != Round::Starting,
            sb_posted: round != Round::Starting,
            max_raises_per_round,
        })
    }
}

/// The round of the game.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Round {
    #[default]
    Starting,
    Ante,

    DealPreflop,
    Preflop,

    DealFlop,
    Flop,

    DealTurn,
    Turn,

    DealRiver,
    River,

    Showdown,
    Complete,
}

impl Display for Round {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Round::Starting => write!(f, "Starting"),

            Round::Ante => write!(f, "Ante"),

            Round::DealPreflop => write!(f, "Deal Preflop"),
            Round::Preflop => write!(f, "Preflop"),

            Round::DealFlop => write!(f, "Deal Flop"),
            Round::Flop => write!(f, "Flop"),

            Round::DealTurn => write!(f, "Deal Turn"),
            Round::Turn => write!(f, "Turn"),

            Round::DealRiver => write!(f, "Deal River"),
            Round::River => write!(f, "River"),

            Round::Showdown => write!(f, "Showdown"),
            Round::Complete => write!(f, "Complete"),
        }
    }
}

impl Round {
    pub fn advance(&self) -> Self {
        match *self {
            Round::Starting => Round::Ante,
            Round::Ante => Round::DealPreflop,
            Round::DealPreflop => Round::Preflop,
            Round::Preflop => Round::DealFlop,
            Round::DealFlop => Round::Flop,
            Round::Flop => Round::DealTurn,
            Round::DealTurn => Round::Turn,
            Round::Turn => Round::DealRiver,
            Round::DealRiver => Round::River,
            Round::River => Round::Showdown,
            Round::Showdown => Round::Complete,

            Round::Complete => Round::Complete,
        }
    }
}

#[derive(Clone, PartialEq, Debug)]
pub struct RoundData {
    // Which players were active starting this round.
    pub starting_player_active: PlayerBitSet,
    pub needs_action: PlayerBitSet,
    // The minimum allowed raise.
    pub min_raise: f32,
    // The value to be called.
    pub bet: f32,
    // How much each player has put in so far.
    pub player_bet: PlayerVec<f32>,
    // The number of times anyone has put in money
    pub total_bet_count: u8,
    // The number of times anyone has increased the bet non-forced.
    pub total_raise_count: u8,
    // The number of forced bets (blinds, antes, straddles).
    pub forced_bet_count: u8,
    // The index of the next player to act.
    pub to_act_idx: usize,
}

impl RoundData {
    pub fn new(num_players: usize, min_raise: f32, active: PlayerBitSet, to_act: usize) -> Self {
        RoundData {
            needs_action: active,
            starting_player_active: active,
            min_raise,
            bet: 0.0,
            player_bet: smallvec![0.0; num_players],
            total_bet_count: 0,
            total_raise_count: 0,
            forced_bet_count: 0,
            to_act_idx: to_act,
        }
    }

    /// Create a new round data with the given bets.
    /// This is useful for creating a new round data that represents
    /// a round that is halfway through. For example, if we're trying
    /// to simulate a choosing to call an all in on the river.
    ///
    /// # Arguments
    ///
    /// * `num_players` - The number of players in the game.
    /// * `min_raise` - The minimum raise allowed in the round.
    /// * `active` - The players that are active in the round.
    /// * `to_act` - The index of the player that is next to act.
    /// * `player_bet` - The amount each player has bet so far.
    ///
    /// # Returns
    ///
    /// A new round data with the given bets, the bets are
    /// used to assume other values of the round.
    ///
    /// # Example
    ///
    /// ```
    /// use rs_poker::arena::game_state::RoundData;
    /// use rs_poker::core::PlayerBitSet;
    ///
    /// let num_players = 3;
    /// let min_raise = 10.0;
    /// let active = PlayerBitSet::new(num_players);
    ///
    /// let player_bet = vec![0.0, 10.0, 20.0];
    /// let to_act = 0;
    ///
    /// let round_data = RoundData::new_with_bets(min_raise, active, to_act, player_bet);
    ///
    /// assert_eq!(round_data.bet, 20.0);
    ///
    /// assert_eq!(round_data.total_bet_count, 2);
    ///
    /// assert_eq!(round_data.total_raise_count, 2);
    /// ```
    pub fn new_with_bets(
        min_raise: f32,
        active: PlayerBitSet,
        to_act: usize,
        player_bet: impl Into<PlayerVec<f32>>,
    ) -> Self {
        // Accept any inline-vector source (slice, Vec, SmallVec) and move it
        // straight into the round data — no extra copy when the caller already
        // has a `PlayerVec`.
        let player_bet = player_bet.into();
        let bet: f32 = player_bet.iter().fold(0.0, |acc, &x| acc.max(x));

        let total_raise_count = player_bet.iter().filter(|&&x| x > 0.0).count() as u8;

        RoundData {
            needs_action: active,
            starting_player_active: active,
            min_raise,
            bet,
            player_bet,
            // bet_count,
            total_bet_count: total_raise_count,
            // raise_count,
            total_raise_count,
            // When creating from existing bets, we don't know which were forced
            forced_bet_count: 0,
            to_act_idx: to_act,
        }
    }

    pub fn advance_action(&mut self) {
        loop {
            // Here we use the length of the player bet vector
            // for the number of seats in the table. This assumes that
            // that the vector is always pre-initialized to the correct length.
            self.to_act_idx = (self.to_act_idx + 1) % self.player_bet.len();
            if self.needs_action.empty() || self.needs_action.get(self.to_act_idx) {
                break;
            }
        }
    }

    pub fn do_bet(&mut self, extra_amount: f32, is_forced: bool) {
        self.player_bet[self.to_act_idx] += extra_amount;
        self.total_bet_count += 1;

        if is_forced {
            self.forced_bet_count += 1;
        }

        // The amount to be called is
        // the maximum anyone has wagered.
        let previous_bet = self.bet;
        let player_bet = self.player_bet[self.to_act_idx];
        self.bet = previous_bet.max(player_bet);

        if !is_forced && player_bet > previous_bet {
            self.total_raise_count += 1;
        }

        let raise_amount = self.bet - previous_bet;
        self.min_raise = self.min_raise.max(raise_amount);
    }

    pub fn num_players_need_action(&self) -> usize {
        self.needs_action.count()
    }

    /// Returns true if no player has voluntarily put money in the pot this round.
    /// This means only forced bets (blinds, antes, straddles) have been posted.
    /// Returns true if no voluntary bets have been made yet (only forced bets like blinds/antes).
    pub fn is_action_unopened(&self) -> bool {
        self.total_bet_count == self.forced_bet_count
    }

    pub fn current_player_bet(&self) -> f32 {
        self.player_bet[self.to_act_idx]
    }
}

#[derive(Clone, PartialEq, Debug)]
pub struct GameState {
    /// The number of players that started
    pub num_players: usize,
    /// Which players are still active in the game.
    pub player_active: PlayerBitSet,
    pub player_all_in: PlayerBitSet,
    /// The total amount in all pots
    pub total_pot: f32,
    /// How much is left in each player's stack
    pub stacks: PlayerVec<f32>,
    // The amount at the start of the game (or creation of the gamestate).
    pub starting_stacks: PlayerVec<f32>,
    pub player_bet: PlayerVec<f32>,
    pub player_winnings: PlayerVec<f32>,
    /// The big blind size
    pub big_blind: f32,
    /// The small blind size
    pub small_blind: f32,
    /// The ante size
    pub ante: f32,
    /// The hands for each player. We keep hands
    /// even if the player is not currently active.
    pub hands: PlayerVec<Hand>,
    /// The index of the player who's the dealer
    pub dealer_idx: usize,
    // What round this is currently
    pub round: Round,
    /// This is the round before we completed the game.
    /// Sometimes the game completes because of
    /// all the players fold in the preflop.
    pub round_before: Round,
    // ALl the current state of the round.
    pub round_data: RoundData,
    // The community cards.
    pub board: BoardVec,
    // Have the blinds been posted.
    // This is used to not double post blinds
    // on sim restarts.
    pub bb_posted: bool,
    pub sb_posted: bool,
    /// Maximum raises allowed per betting round. None = unlimited.
    /// Default is Some(3). When exceeded, raises are converted to calls.
    pub max_raises_per_round: Option<u8>,
}

impl GameState {
    /// Check if raises are capped for the current round.
    /// Returns true if the number of raises has reached the max limit.
    pub fn is_raise_capped(&self) -> bool {
        self.max_raises_per_round
            .is_some_and(|max| self.round_data.total_raise_count >= max)
    }

    pub fn num_active_players(&self) -> usize {
        self.player_active.count()
    }

    pub fn num_all_in_players(&self) -> usize {
        self.player_all_in.count()
    }

    pub fn is_complete(&self) -> bool {
        self.num_active_players() == 1 || self.round == Round::Complete
    }

    pub fn to_act_idx(&self) -> usize {
        self.round_data.to_act_idx
    }

    pub fn current_player_stack(&self) -> f32 {
        *self.stacks.get(self.to_act_idx()).unwrap_or(&0.0)
    }

    pub fn current_player_starting_stack(&self) -> f32 {
        *self.starting_stacks.get(self.to_act_idx()).unwrap_or(&0.0)
    }

    pub fn current_round_current_player_bet(&self) -> f32 {
        *self
            .round_data
            .player_bet
            .get(self.to_act_idx())
            .unwrap_or(&0.0)
    }

    pub fn current_round_bet(&self) -> f32 {
        self.round_data.bet
    }

    pub fn current_round_player_bet(&self, idx: usize) -> f32 {
        self.round_data.player_bet.get(idx).copied().unwrap_or(0.0)
    }

    pub fn current_round_num_active_players(&self) -> usize {
        self.round_data.num_players_need_action()
    }

    pub fn current_round_min_raise(&self) -> f32 {
        self.round_data.min_raise
    }

    pub fn advance_round(&mut self) {
        match self.round {
            Round::Complete => (),
            _ => self.advance_normal(),
        }
    }

    fn advance_normal(&mut self) {
        // We're advancing (not completing) so
        // keep advanding the round_before field as well.
        self.round_before = self.round;

        self.round = self.round.advance();

        let mut round_data = RoundData::new(
            self.num_players,
            self.big_blind,
            self.player_active,
            self.dealer_idx,
        );
        round_data.advance_action();
        if self.round == Round::Preflop && self.num_players == 2 {
            // With only two players, it is the dealer that has
            // to post the small blind, so pass the action back.
            round_data.advance_action();
        }
        self.round_data = round_data;
    }

    pub fn complete(&mut self) {
        if self.round == Round::Complete {
            return;
        }

        self.round_before = self.round;
        self.round = Round::Complete;
        self.round_data = RoundData::new(
            self.num_players,
            self.big_blind,
            PlayerBitSet::new(0),
            self.dealer_idx,
        );
    }

    pub fn fold(&mut self) {
        // Which player is next to act
        let idx = self.round_data.to_act_idx;
        // We are going to change the current round since this player is out.
        self.round_data.needs_action.disable(idx);
        self.player_active.disable(idx);

        // They fold ending the turn.
        self.round_data.advance_action();
    }

    pub fn do_bet(&mut self, amount: f32, is_forced: bool) -> Result<f32, GameStateError> {
        // Which player is next to act
        let idx = self.to_act_idx();

        // This is the amount extra that the player is putting into the round's betting
        // pot
        //
        // We need to validate it before making anychanges to the game state. This
        // allows us to return an error before getting into any bad gamestate.
        //
        // It also allows agents to be punished for putting in bad bet types.
        //
        // Make sure the bet is a correct amount and if not
        // then cap it at the maximum the player can bet (Their stacks usually)
        let extra_amount = if is_forced {
            self.validate_forced_bet_amount(amount)
        } else {
            self.validate_bet_amount(amount)?
        };

        let prev_bet = self.round_data.bet;
        // At this point we start making changes.
        // Take the money out.
        self.stacks[idx] -= extra_amount;

        self.round_data.do_bet(extra_amount, is_forced);

        self.player_bet[idx] += extra_amount;

        self.total_pot += extra_amount;

        let is_betting_reopened = prev_bet < self.round_data.bet;

        if is_betting_reopened {
            // This is a new max bet. We need to reset who can act in the round
            self.round_data.needs_action = self.player_active;
        }

        // If they put money into the pot then they are done this turn.
        if !is_forced {
            self.round_data.needs_action.disable(idx);
        }

        // We're out and can't continue
        // Use epsilon comparison to handle floating-point precision issues
        // (e.g., when stack is 1.19e-7 instead of exactly 0)
        if abs_diff_eq!(self.stacks[idx], 0.0) {
            // Keep track of who's still active.
            self.player_active.disable(idx);
            // Keep track of going all in. We'll use that later on
            // to determine who's worth ranking.
            self.player_all_in.enable(idx);
            // It doesn' matter if this is a forced
            // bet if the player is out of money.
            self.round_data.needs_action.disable(idx);
        }

        // Advance the next to act.
        self.round_data.advance_action();

        Ok(extra_amount)
    }

    pub fn award(&mut self, player_idx: usize, amount: f32) {
        self.stacks[player_idx] += amount;
        self.player_winnings[player_idx] += amount;
    }

    /// Get the total reward for a player.
    /// This is the change in stack from the start of the game
    /// to the now.
    ///
    /// # Arguments
    /// * `player_idx` - The index of the player to get the reward for.
    pub fn player_reward(&self, player_idx: usize) -> f32 {
        // The reward is the change in stack from the start of the game
        // to the end of the game.
        self.stacks[player_idx] - self.starting_stacks[player_idx]
    }

    fn validate_forced_bet_amount(&self, amount: f32) -> f32 {
        // Which player is next to act. Map the optional into the to_act_index or 0.
        let idx = self.to_act_idx();

        self.stacks[idx].min(amount)
    }

    fn validate_bet_amount(&self, amount: f32) -> Result<f32, GameStateError> {
        // Which player is next to act
        let idx = self.to_act_idx();

        // Use a scaled epsilon for floating point comparisons.
        // We scale by the magnitude of the values being compared to handle
        // both small and large bet amounts correctly.
        let magnitude = amount.abs().max(self.round_data.bet.abs()).max(1.0);
        let epsilon = magnitude * f32::EPSILON * 1000.0; // Scale epsilon for practical tolerance

        if amount.is_sign_negative() || amount.is_nan() {
            // You can't bet negative numbers.
            // You can't be a NaN.
            Err(GameStateError::BetInvalidSize)
        } else if self.round_data.player_bet[idx] > amount + epsilon {
            // We've already bet more than this. No takes backs.
            Err(GameStateError::BetSizeDoesntCallSelf)
        } else {
            // How much extra are we putting in.
            let extra = amount - self.round_data.player_bet[idx];

            // How much more are we putting in this time. Capped at the stack
            let capped_extra = self.stacks[idx].min(extra);
            // What our new player bet will be
            let capped_new_player_bet = self.round_data.player_bet[idx] + capped_extra;
            let current_bet = self.round_data.bet;
            // How much this is a raise.
            let raise = (capped_new_player_bet - current_bet).max(0.0);
            // Use relative epsilon for all-in check to handle floating point precision
            let stack_epsilon = self.stacks[idx].abs().max(1.0) * f32::EPSILON * 1000.0;
            let is_all_in = (capped_extra - self.stacks[idx]).abs() < stack_epsilon;
            let is_raise = raise > epsilon;
            // Use epsilon tolerance for call check to handle floating point precision
            if capped_new_player_bet + epsilon < self.round_data.bet && !is_all_in {
                // If we're not even calling and it's not an all in.
                Err(GameStateError::BetSizeDoesntCall)
            } else if is_raise && !is_all_in && raise + epsilon < self.round_data.min_raise {
                // There's a raise the raise is less than the min bet and it's not an all in
                Err(GameStateError::RaiseSizeTooSmall)
            } else {
                // Yeah this looks ok.
                Ok(capped_extra)
            }
        }
    }
}

pub trait GameStateGenerator: Iterator<Item = GameState> {}

/// This is a simple generator that just clones the game state
/// every time it's called.
///
/// This holds the dealt cards constant and the stack sizes constant.
pub struct CloneGameStateGenerator {
    game_state: GameState,
}

impl CloneGameStateGenerator {
    pub fn new(game_state: GameState) -> CloneGameStateGenerator {
        CloneGameStateGenerator { game_state }
    }
}

impl Iterator for CloneGameStateGenerator {
    type Item = GameState;

    fn next(&mut self) -> Option<Self::Item> {
        Some(self.game_state.clone())
    }
}

/// This `GameStateGenerator` generates a random game state with no cards dealt
/// and random stack sizes. The dealer button is also randomly placed.
pub struct RandomGameStateGenerator {
    num_players: usize,
    min_stack: f32,
    max_stack: f32,
    big_blind: f32,
    small_blind: f32,
    ante: f32,
    /// Optional seeded RNG for deterministic generation
    seeded_rng: Option<rand::rngs::StdRng>,
}

impl RandomGameStateGenerator {
    pub fn new(
        num_players: usize,
        min_stack: f32,
        max_stack: f32,
        big_blind: f32,
        small_blind: f32,
        ante: f32,
    ) -> RandomGameStateGenerator {
        RandomGameStateGenerator {
            num_players,
            min_stack,
            max_stack,
            big_blind,
            small_blind,
            ante,
            seeded_rng: None,
        }
    }

    /// Create a new generator with a specific seed for deterministic results
    pub fn with_seed(
        num_players: usize,
        min_stack: f32,
        max_stack: f32,
        big_blind: f32,
        small_blind: f32,
        ante: f32,
        seed: u64,
    ) -> RandomGameStateGenerator {
        use rand::SeedableRng;
        RandomGameStateGenerator {
            num_players,
            min_stack,
            max_stack,
            big_blind,
            small_blind,
            ante,
            seeded_rng: Some(rand::rngs::StdRng::seed_from_u64(seed)),
        }
    }
}

impl Iterator for RandomGameStateGenerator {
    type Item = GameState;

    fn next(&mut self) -> Option<Self::Item> {
        use rand::RngExt;

        // Use seeded RNG if available, otherwise use global RNG
        let (stacks, dealer_idx) = if let Some(ref mut seeded) = self.seeded_rng {
            let stacks: Vec<f32> = (0..self.num_players)
                .map(|_| {
                    if self.min_stack == self.max_stack {
                        self.min_stack
                    } else {
                        seeded.random_range(self.min_stack..self.max_stack)
                    }
                })
                .collect();
            let dealer_idx = seeded.random_range(0..self.num_players);
            (stacks, dealer_idx)
        } else {
            let mut unseeded = rng();
            let stacks: Vec<f32> = (0..self.num_players)
                .map(|_| {
                    if self.min_stack == self.max_stack {
                        self.min_stack
                    } else {
                        unseeded.random_range(self.min_stack..self.max_stack)
                    }
                })
                .collect();
            let dealer_idx = unseeded.random_range(0..self.num_players);
            (stacks, dealer_idx)
        };

        Some(
            GameStateBuilder::new()
                .stacks(stacks)
                .big_blind(self.big_blind)
                .small_blind(self.small_blind)
                .ante(self.ante)
                .dealer_idx(dealer_idx)
                .build()
                .expect("RandomGameStateGenerator produced invalid game state"),
        )
    }
}

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

    /// Test helper to create a game state with standard defaults
    fn test_game_state(
        stacks: Vec<f32>,
        big_blind: f32,
        small_blind: f32,
        ante: f32,
        dealer_idx: usize,
    ) -> GameState {
        GameStateBuilder::new()
            .stacks(stacks)
            .big_blind(big_blind)
            .small_blind(small_blind)
            .ante(ante)
            .dealer_idx(dealer_idx)
            .build()
            .unwrap()
    }

    #[test]
    fn test_fold_around_call() {
        let stacks = vec![100.0; 4];
        let mut game_state = test_game_state(stacks, 10.0, 5.0, 0.0, 1);

        // starting
        game_state.advance_round();
        // Ante
        game_state.advance_round();
        // Deal Preflop
        game_state.advance_round();

        // Preflop
        // 0 player, 1 dealer, 2 small blind, 3 big blind
        // Game state doesn't force the small blind and big blind
        assert_eq!(2, game_state.to_act_idx());

        // Do the blinds now
        game_state.do_bet(5.0, true).unwrap();
        game_state.do_bet(10.0, true).unwrap();

        // The blinds posting wraps around when needed
        assert_eq!(0, game_state.to_act_idx());

        // Posted blinds can then fold
        game_state.fold();
        game_state.fold();

        game_state.do_bet(10.0, false).unwrap();
        game_state.do_bet(10.0, false).unwrap();
        assert_eq!(0, game_state.current_round_num_active_players());
        assert_eq!(2, game_state.num_active_players());

        // Deal  Flop
        game_state.advance_round();

        // Flop
        game_state.advance_round();
        assert_eq!(2, game_state.to_act_idx());
        game_state.do_bet(0.0, false).unwrap();
        assert_eq!(3, game_state.to_act_idx());
        game_state.do_bet(0.0, false).unwrap();
        assert_eq!(0, game_state.current_round_num_active_players());
        assert_eq!(2, game_state.num_active_players());

        // Deal Turn
        game_state.advance_round();

        // Turn
        game_state.advance_round();
        assert_eq!(2, game_state.to_act_idx());
        assert_eq!(2, game_state.current_round_num_active_players());
        game_state.do_bet(0.0, false).unwrap();
        game_state.do_bet(0.0, false).unwrap();
        assert_eq!(0, game_state.current_round_num_active_players());
        assert_eq!(2, game_state.num_active_players());

        // Deal River
        game_state.advance_round();

        // River
        game_state.advance_round();
        game_state.do_bet(0.0, false).unwrap();
        game_state.do_bet(0.0, false).unwrap();
        assert_eq!(0, game_state.current_round_num_active_players());
        assert_eq!(2, game_state.num_active_players());

        game_state.advance_round();
        assert_eq!(Round::Showdown, game_state.round);
    }

    #[test]
    fn test_cant_bet_less_0() {
        let stacks = vec![100.0; 5];
        let mut game_state = test_game_state(stacks, 2.0, 1.0, 0.0, 0);
        game_state.advance_round();
        game_state.advance_round();

        game_state.do_bet(33.0, false).unwrap();
        game_state.fold();
        let res = game_state.do_bet(20.0, false);

        assert_eq!(res.err(), Some(GameStateError::BetSizeDoesntCall));
    }

    #[test]
    fn test_cant_bet_less_with_all_in() {
        let stacks = vec![100.0, 50.0, 50.0, 100.0, 10.0];
        let mut game_state = test_game_state(stacks, 2.0, 1.0, 0.0, 0);
        // Do the start and ante rounds and setup next to act
        game_state.advance_round();
        game_state.advance_round();

        // UTG raises to 10
        game_state.do_bet(10.0, false).unwrap();

        // UTG+1 has 10 remaining so betting 100 is overbetting
        // into an all in.
        game_state.do_bet(100.0, false).unwrap();

        // Dealer gets out of the way
        game_state.fold();

        // Small Blind raises to 20
        game_state.do_bet(20.0, false).unwrap();

        // Big Blind can't call the previous value.
        let res = game_state.do_bet(10.0, false);
        assert_eq!(res.err(), Some(GameStateError::BetSizeDoesntCall));
    }

    #[test]
    fn test_cant_under_minraise_bb() {
        let stacks = vec![500.0; 5];
        let mut game_state = test_game_state(stacks, 20.0, 10.0, 0.0, 0);
        // Do the start and ante rounds and setup next to act
        game_state.advance_round();
        game_state.advance_round();
        game_state.advance_round();

        game_state.do_bet(10.0, true).unwrap();
        game_state.do_bet(20.0, true).unwrap();

        // UTG raises to 33
        //
        // However the min raise is the big blind
        // so since the bb has already posted
        // we're not able to raise 13
        assert_eq!(
            Err(GameStateError::RaiseSizeTooSmall),
            game_state.do_bet(33.0, false)
        );
    }

    #[test]
    fn test_gamestate_keeps_round_before_complete() {
        let stacks = vec![100.0; 3];
        let mut game_state = test_game_state(stacks, 10.0, 5.0, 0.0, 0);
        // Simulate a game where everyone folds and the big blind wins
        game_state.advance_round();
        game_state.advance_round();
        game_state.advance_round();
        game_state.fold();
        game_state.fold();
        game_state.complete();
        assert_eq!(Round::Complete, game_state.round);
        assert_eq!(Round::Preflop, game_state.round_before);
    }

    #[test]
    fn test_can_create_starting_round_data() {
        let num_players = 3;
        let min_raise = 10.0;
        let active = PlayerBitSet::new(num_players);

        let round_data = RoundData::new(num_players, min_raise, active, 0);

        assert_eq!(round_data.bet, 0.0);

        assert_eq!(round_data.total_bet_count, 0);

        assert_eq!(round_data.total_raise_count, 0);
    }

    #[test]
    fn test_can_create_inprogress_round_data() {
        let num_players = 3;
        let min_raise = 10.0;
        let active = PlayerBitSet::new(num_players);

        let player_bet = vec![0.0, 10.0, 20.0];
        let to_act = 0;

        let round_data = RoundData::new_with_bets(min_raise, active, to_act, player_bet);

        assert_eq!(round_data.bet, 20.0);

        assert_eq!(round_data.total_bet_count, 2);

        assert_eq!(round_data.total_raise_count, 2);
    }

    /// Verifies that each Round variant displays the expected string representation.
    #[test]
    fn test_round_display() {
        assert_eq!(format!("{}", Round::Starting), "Starting");
        assert_eq!(format!("{}", Round::Ante), "Ante");
        assert_eq!(format!("{}", Round::DealPreflop), "Deal Preflop");
        assert_eq!(format!("{}", Round::Preflop), "Preflop");
        assert_eq!(format!("{}", Round::DealFlop), "Deal Flop");
        assert_eq!(format!("{}", Round::Flop), "Flop");
        assert_eq!(format!("{}", Round::DealTurn), "Deal Turn");
        assert_eq!(format!("{}", Round::Turn), "Turn");
        assert_eq!(format!("{}", Round::DealRiver), "Deal River");
        assert_eq!(format!("{}", Round::River), "River");
        assert_eq!(format!("{}", Round::Showdown), "Showdown");
        assert_eq!(format!("{}", Round::Complete), "Complete");
    }

    /// Verifies is_action_unopened correctly tracks whether voluntary bets have been made.
    #[test]
    fn test_is_action_unopened() {
        let num_players = 3;
        let active = PlayerBitSet::new(num_players);
        let mut round_data = RoundData::new(num_players, 10.0, active, 0);

        // Initially, no bets have been made
        assert!(round_data.is_action_unopened());

        // After a forced bet (like a blind), still unopened
        round_data.do_bet(5.0, true);
        assert!(round_data.is_action_unopened());

        // After a voluntary bet, no longer unopened
        round_data.advance_action();
        round_data.do_bet(10.0, false);
        assert!(!round_data.is_action_unopened());
    }

    /// Verifies current_player_bet returns the actual bet amount for the current player.
    #[test]
    fn test_current_player_bet() {
        let num_players = 3;
        let active = PlayerBitSet::new(num_players);
        let mut round_data = RoundData::new(num_players, 10.0, active, 0);

        // Initially 0
        assert_eq!(round_data.current_player_bet(), 0.0);

        // After betting
        round_data.do_bet(25.0, false);
        assert_eq!(round_data.current_player_bet(), 25.0);

        // Move to next player and check their bet
        round_data.advance_action();
        assert_eq!(round_data.current_player_bet(), 0.0);

        round_data.do_bet(50.0, false);
        assert_eq!(round_data.current_player_bet(), 50.0);
    }

    /// Verifies num_all_in_players correctly counts players who have gone all-in.
    #[test]
    fn test_num_all_in_players() {
        let stacks = vec![100.0, 100.0, 100.0];
        let mut game_state = test_game_state(stacks, 10.0, 5.0, 0.0, 0);

        // Initially no one is all-in
        assert_eq!(game_state.num_all_in_players(), 0);

        // Advance to preflop
        game_state.advance_round();
        game_state.advance_round();
        game_state.advance_round();
        assert_eq!(game_state.num_all_in_players(), 0);

        // Go all-in
        game_state.do_bet(100.0, false).unwrap();
        assert_eq!(game_state.num_all_in_players(), 1);

        // Another player goes all-in
        game_state.do_bet(100.0, false).unwrap();
        assert_eq!(game_state.num_all_in_players(), 2);
    }

    /// Verifies is_complete correctly identifies when a game has ended.
    #[test]
    fn test_is_complete() {
        let stacks = vec![100.0, 100.0, 100.0];
        let mut game_state = test_game_state(stacks, 10.0, 5.0, 0.0, 0);

        // Not complete at start
        assert!(!game_state.is_complete());

        // Advance through rounds
        game_state.advance_round();
        game_state.advance_round();
        game_state.advance_round();
        assert!(!game_state.is_complete());

        // After two folds, only one player remains - should be complete
        game_state.fold();
        game_state.fold();
        assert!(game_state.is_complete());
    }

    /// Verifies do_bet correctly updates bet amounts, counts, and min_raise tracking.
    #[test]
    fn test_do_bet_arithmetic() {
        let num_players = 3;
        let active = PlayerBitSet::new(num_players);
        let mut round_data = RoundData::new(num_players, 10.0, active, 0);

        // First bet of 20
        round_data.do_bet(20.0, false);
        assert_eq!(round_data.player_bet[0], 20.0);
        assert_eq!(round_data.bet, 20.0);
        assert_eq!(round_data.total_bet_count, 1);
        assert_eq!(round_data.total_raise_count, 1);

        // Advance and second player raises to 40
        round_data.advance_action();
        round_data.do_bet(40.0, false);
        assert_eq!(round_data.player_bet[1], 40.0);
        assert_eq!(round_data.bet, 40.0);
        assert_eq!(round_data.total_bet_count, 2);
        assert_eq!(round_data.total_raise_count, 2);
        assert_eq!(round_data.min_raise, 20.0); // 40 - 20 = 20

        // Test forced bet tracking
        let mut round_data2 = RoundData::new(num_players, 10.0, active, 0);
        round_data2.do_bet(5.0, true); // forced
        assert_eq!(round_data2.forced_bet_count, 1);
        assert_eq!(round_data2.total_raise_count, 0); // forced bets don't count as raises
    }

    /// Verifies current_player_starting_stack returns the player's initial stack amount.
    #[test]
    fn test_current_player_starting_stack() {
        let stacks = vec![100.0, 200.0, 300.0];
        let game_state = test_game_state(stacks.clone(), 10.0, 5.0, 0.0, 0);

        // Player 1 is to act first (after dealer at position 0)
        let starting_stack = game_state.current_player_starting_stack();
        // Starting stacks should be what we passed in
        assert_eq!(starting_stack, stacks[game_state.to_act_idx()]);
        assert!(starting_stack > 1.0, "Starting stack should be > 1.0");
        assert!(starting_stack > 0.0, "Starting stack should be > 0.0");
    }

    /// Verifies current_round_current_player_bet returns what the current player has bet this round.
    #[test]
    fn test_current_round_current_player_bet() {
        let stacks = vec![100.0, 200.0];
        let mut game_state = test_game_state(stacks, 10.0, 5.0, 0.0, 0);
        game_state.advance_round(); // Move to preflop

        // Post blinds
        let _ = game_state.do_bet(5.0, true); // Small blind
        let _player_bet = game_state.current_round_current_player_bet();

        // After posting SB, player should have bet 5.0
        // Now it's BB's turn
        let bb_bet = game_state.current_round_current_player_bet();
        // BB hasn't bet yet
        assert_eq!(bb_bet, 0.0);

        // Post BB
        let _ = game_state.do_bet(10.0, true);
        // Now SB's turn again
        let sb_current_bet = game_state.current_round_current_player_bet();
        // SB has bet 5.0
        assert_eq!(sb_current_bet, 5.0);
    }

    /// Verifies advance_round is a no-op when the game is already complete.
    #[test]
    fn test_advance_round_when_complete() {
        let stacks = vec![100.0, 100.0];
        let mut game_state = test_game_state(stacks, 10.0, 5.0, 0.0, 0);
        game_state.complete();

        assert_eq!(game_state.round, Round::Complete);
        let round_before = game_state.round_before;

        // Calling advance_round when complete should be a no-op
        game_state.advance_round();

        assert_eq!(game_state.round, Round::Complete);
        assert_eq!(game_state.round_before, round_before);
    }

    /// Verifies validate_bet_amount rejects negative and NaN amounts.
    #[test]
    fn test_validate_bet_amount_negative() {
        let stacks = vec![100.0, 100.0];
        let mut game_state = test_game_state(stacks, 10.0, 5.0, 0.0, 0);
        game_state.advance_round();

        let result = game_state.validate_bet_amount(-10.0);
        assert!(result.is_err());

        let nan_result = game_state.validate_bet_amount(f32::NAN);
        assert!(nan_result.is_err());
    }

    /// Verifies RandomGameStateGenerator produces game states with valid properties.
    #[test]
    fn test_random_game_state_generator() {
        let mut generator = RandomGameStateGenerator::new(3, 50.0, 150.0, 10.0, 5.0, 0.0);

        for _ in 0..10 {
            let gs = generator.next().unwrap();
            assert_eq!(gs.num_players, 3);
            assert!(gs.dealer_idx < gs.num_players);
            for stack in &gs.stacks {
                assert!(*stack >= 50.0 && *stack <= 150.0);
            }
        }
    }

    // ==================== GameStateBuilder Tests ====================

    #[test]
    fn test_builder_minimal_valid() {
        let gs = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .build()
            .unwrap();

        assert_eq!(gs.num_players, 2);
        assert_eq!(gs.big_blind, 10.0);
        assert_eq!(gs.small_blind, 5.0); // defaults to bb/2
        assert_eq!(gs.ante, 0.0);
        assert_eq!(gs.dealer_idx, 0);
        assert_eq!(gs.round, Round::Starting);
        assert_eq!(gs.max_raises_per_round, Some(3));
    }

    #[test]
    fn test_builder_small_blind_computed_from_big_blind() {
        let gs = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(20.0)
            .build()
            .unwrap();

        assert_eq!(gs.small_blind, 10.0);
    }

    #[test]
    fn test_builder_num_players_with_stack_convenience() {
        let gs = GameStateBuilder::new()
            .num_players_with_stack(4, 500.0)
            .big_blind(10.0)
            .build()
            .unwrap();

        assert_eq!(gs.num_players, 4);
        assert_eq!(gs.stacks.to_vec(), vec![500.0; 4]);
    }

    #[test]
    fn test_builder_max_raises_default_is_three() {
        let gs = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .build()
            .unwrap();

        assert_eq!(gs.max_raises_per_round, Some(3));
    }

    #[test]
    fn test_builder_max_raises_unlimited() {
        let gs = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .max_raises_per_round(None)
            .build()
            .unwrap();

        assert_eq!(gs.max_raises_per_round, None);
    }

    #[test]
    fn test_builder_blinds_convenience_method() {
        let gs = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .blinds(20.0, 10.0)
            .build()
            .unwrap();

        assert_eq!(gs.big_blind, 20.0);
        assert_eq!(gs.small_blind, 10.0);
    }

    #[test]
    fn test_builder_error_missing_stacks() {
        let result = GameStateBuilder::new().big_blind(10.0).build();

        assert_eq!(result.unwrap_err(), GameStateBuilderError::MissingStacks);
    }

    #[test]
    fn test_builder_error_missing_big_blind() {
        let result = GameStateBuilder::new().stacks(vec![100.0, 100.0]).build();

        assert_eq!(result.unwrap_err(), GameStateBuilderError::MissingBigBlind);
    }

    #[test]
    fn test_builder_error_too_few_players() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0])
            .big_blind(10.0)
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidPlayerCount { actual: 1 }
        );
    }

    #[test]
    fn test_builder_error_too_many_players() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0; 17])
            .big_blind(10.0)
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidPlayerCount { actual: 17 }
        );
    }

    #[test]
    fn test_builder_error_invalid_dealer_index() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .dealer_idx(5)
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidDealerIndex {
                dealer_idx: 5,
                num_players: 2
            }
        );
    }

    #[test]
    fn test_builder_error_negative_big_blind() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(-10.0)
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidBigBlind(-10.0)
        );
    }

    #[test]
    fn test_builder_error_zero_big_blind() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(0.0)
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidBigBlind(0.0)
        );
    }

    #[test]
    fn test_builder_error_nan_big_blind() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(f32::NAN)
            .build();

        // NaN != NaN, so we check the variant
        assert!(matches!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidBigBlind(_)
        ));
    }

    #[test]
    fn test_builder_error_negative_small_blind() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .small_blind(-5.0)
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidSmallBlind(-5.0)
        );
    }

    #[test]
    fn test_builder_error_negative_ante() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .ante(-1.0)
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidAnte(-1.0)
        );
    }

    #[test]
    fn test_builder_error_negative_stack() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, -50.0])
            .big_blind(10.0)
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidStack {
                index: 1,
                value: -50.0
            }
        );
    }

    #[test]
    fn test_builder_error_insufficient_active_players() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 0.0])
            .big_blind(10.0)
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InsufficientActivePlayers
        );
    }

    #[test]
    fn test_builder_error_hands_length_mismatch() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .hands(vec![Hand::default()])
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::HandsLengthMismatch {
                hands_len: 1,
                num_players: 2
            }
        );
    }

    #[test]
    fn test_builder_error_player_bet_length_mismatch() {
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .player_bet(vec![0.0, 0.0, 0.0])
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::PlayerBetLengthMismatch {
                bet_len: 3,
                num_players: 2
            }
        );
    }

    #[test]
    fn test_builder_error_invalid_board_size_one() {
        use crate::core::{Card, Suit, Value};
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .board(vec![Card::new(Value::Ace, Suit::Spade)])
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidBoardSize(1)
        );
    }

    #[test]
    fn test_builder_error_invalid_board_size_two() {
        use crate::core::{Card, Suit, Value};
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .board(vec![
                Card::new(Value::Ace, Suit::Spade),
                Card::new(Value::King, Suit::Spade),
            ])
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::InvalidBoardSize(2)
        );
    }

    #[test]
    fn test_builder_error_duplicate_card_in_board() {
        use crate::core::{Card, Suit, Value};
        let card = Card::new(Value::Ace, Suit::Spade);
        let result = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .board(vec![card, Card::new(Value::King, Suit::Spade), card])
            .build();

        assert_eq!(
            result.unwrap_err(),
            GameStateBuilderError::DuplicateCard(card)
        );
    }

    #[test]
    fn test_builder_heads_up_valid() {
        let gs = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .build()
            .unwrap();

        assert_eq!(gs.num_players, 2);
    }

    #[test]
    fn test_builder_ten_players_valid() {
        let gs = GameStateBuilder::new()
            .stacks(vec![100.0; 10])
            .big_blind(10.0)
            .build()
            .unwrap();

        assert_eq!(gs.num_players, 10);
    }

    #[test]
    fn test_builder_all_but_two_players_zero_stack() {
        let mut stacks = vec![0.0; 6];
        stacks[2] = 100.0;
        stacks[5] = 100.0;

        let gs = GameStateBuilder::new()
            .stacks(stacks)
            .big_blind(10.0)
            .build()
            .unwrap();

        assert_eq!(gs.num_players, 6);
        assert_eq!(gs.player_active.count(), 2);
    }

    #[test]
    fn test_builder_with_ante() {
        let gs = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .ante(1.0)
            .build()
            .unwrap();

        assert_eq!(gs.ante, 1.0);
    }

    #[test]
    fn test_builder_with_dealer_idx() {
        let gs = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0, 100.0])
            .big_blind(10.0)
            .dealer_idx(2)
            .build()
            .unwrap();

        assert_eq!(gs.dealer_idx, 2);
    }

    #[test]
    fn test_builder_with_round() {
        let gs = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .round(Round::Preflop)
            .build()
            .unwrap();

        assert_eq!(gs.round, Round::Preflop);
        // When not Starting, blinds are marked as posted
        assert!(gs.bb_posted);
        assert!(gs.sb_posted);
    }

    #[test]
    fn test_builder_with_valid_board() {
        use crate::core::{Card, Suit, Value};
        let board = vec![
            Card::new(Value::Ace, Suit::Spade),
            Card::new(Value::King, Suit::Spade),
            Card::new(Value::Queen, Suit::Spade),
        ];

        let gs = GameStateBuilder::new()
            .stacks(vec![100.0, 100.0])
            .big_blind(10.0)
            .board(board.clone())
            .build()
            .unwrap();

        assert_eq!(gs.board.to_vec(), board);
    }
}