mod spot_fixture; use rechner::card::deck; use rechner::engine::from_plan::from_plan; use rechner::engine::postflop::choose_postflop; use rechner::equity::estimate::Estimate; use rechner::exploit::thresholds::Thresholds; use rechner::game::{ActionKind, Choice, Decision, Spot, Street, Villain}; use rechner::guard::enforce::enforce; use rechner::guard::settle::settle; use rechner::preflop::plan::{ChartAction, PreflopPlan}; use rechner::read::draw::Draw; use rechner::read::hand_read::HandRead; use rechner::read::made::Made; use rechner::read::suit_pattern::SuitPattern; use rechner::rng::Rng; use rechner::variant::{ActionShape, Variant}; const CASES: usize = 3000; const POSTFLOP_CASES: usize = 4000; /// Tiny generator monad over the seeded Rng: each draw returns the value and the next Rng. fn unit(rng: Rng) -> (f64, Rng) { rng.unit() } fn below(rng: Rng, bound: u64) -> (u64, Rng) { rng.below(bound) } fn chips(rng: Rng, scale: f64) -> (f64, Rng) { let (kind, rng) = below(rng, 12); let (x, rng) = unit(rng); let value = match kind { 0 => 0.0, 1 => 0.01 * x, _ => (x * scale * 100.0).round() / 100.0, }; (value, rng) } fn subset_of_legal(rng: Rng) -> (Vec, Rng) { let (mask, rng) = below(rng, 15); let kinds: Vec = ActionKind::ALL .iter() .enumerate() .filter(|(i, _)| (mask + 1) >> i & 1 == 1) .map(|(_, k)| *k) .collect(); (kinds, rng) } fn allowed_list(rng: Rng) -> (Option>, Rng) { let (on, rng) = below(rng, 2); if on == 0 { return (None, rng); } let (n, rng) = below(rng, 5); let (values, rng) = (0..n).fold((vec![], rng), |(mut acc, rng), _| { let (v, rng) = chips(rng, 6.0); acc.push(v); (acc, rng) }); (Some(values), rng) } fn random_spot(rng: Rng) -> (Spot, Rng) { let (v, rng) = below(rng, Variant::ALL.len() as u64); let variant = Variant::ALL[v as usize]; let (s, rng) = below(rng, 4); let street = Street::ALL[s as usize]; let (hole, rng) = (spot_fixture::cards("Ah 5h"), rng); let (pot, rng) = chips(rng, 20.0); let (to_call, rng) = chips(rng, 5.0); let (stack, rng) = chips(rng, 10.0); let (min_on, rng) = below(rng, 2); let (min, rng) = chips(rng, 2.0); let (max_on, rng) = below(rng, 2); let (max, rng) = chips(rng, 15.0); let (legal, rng) = subset_of_legal(rng); let (allowed, rng) = allowed_list(rng); let (nv, rng) = below(rng, 6); let (folded_mask, rng) = below(rng, 64); let spot = Spot { variant, street, board: deck::full().into_iter().skip(10).take(street.board_len()).collect(), hero_cards: hole, pot, to_call, hero_stack: stack, min_raise_to: (min_on == 1).then_some(min), max_raise_to: (max_on == 1).then_some(max), legal, allowed_sizes: allowed, villains: (0..nv) .map(|i| Villain { seat: i as u8, folded: i < 5 && folded_mask >> i & 1 == 1 && folded_mask % 3 == 0, ..spot_fixture::villain(0) }) .collect(), ..spot_fixture::flop() }; (spot, rng) } fn random_estimate(rng: Rng) -> (Estimate, Rng) { let (share, rng) = unit(rng); let (se, rng) = unit(rng); (spot_fixture::estimate(share, se * 0.05), rng) } const MADES: [Made; 16] = [ Made::Air, Made::BoardOnly, Made::Underpair, Made::BottomPair, Made::MiddlePair, Made::TopPairWeakKicker, Made::TopPairGoodKicker, Made::Overpair, Made::TwoPair, Made::Set, Made::Trips, Made::Straight, Made::Flush, Made::FullHouse, Made::Quads, Made::StraightFlush, ]; const DRAWS: [Draw; 6] = [Draw::None, Draw::Backdoor, Draw::Gutshot, Draw::OpenEnded, Draw::Flush, Draw::Combo]; const SUITS: [SuitPattern; 4] = [SuitPattern::Rainbow, SuitPattern::TwoTone, SuitPattern::Monotone, SuitPattern::FourFlush]; fn random_read(rng: Rng) -> (HandRead, Rng) { let (m, rng) = below(rng, MADES.len() as u64); let (d, rng) = below(rng, DRAWS.len() as u64); let (outs, rng) = below(rng, 16); let (s, rng) = below(rng, SUITS.len() as u64); let (wet, rng) = unit(rng); let (odd, rng) = below(rng, 20); let wetness = if odd == 0 { f64::NAN } else { wet }; ( spot_fixture::read_of(MADES[m as usize], DRAWS[d as usize], outs as u8, SUITS[s as usize], wetness), rng, ) } fn random_plan(rng: Rng) -> (PreflopPlan, Rng) { let (a, rng) = below(rng, 4); let (certainty, rng) = unit(rng); let action = [ChartAction::Fold, ChartAction::Call, ChartAction::Raise, ChartAction::Jam][a as usize]; (spot_fixture::plan(action, certainty), rng) } fn random_thresholds(rng: Rng) -> (Thresholds, Rng) { let (a, rng) = unit(rng); let (b, rng) = unit(rng); let (c, rng) = unit(rng); let (d, rng) = unit(rng); ( Thresholds { call_margin: a * 0.1, value_raise: 0.4 + b * 0.5, abstain_band: c * 0.05, fold_equity_scale: d * 2.0, range_width_scale: 1.0, }, rng, ) } fn assert_sound(spot: &Spot, decision: &Decision, label: &str) { let ctx = format!("{label}: {spot:?} -> {decision:?}"); if let Some(kind) = decision.choice.kind() { assert!(spot.is_legal(kind), "illegal kind. {ctx}"); if kind == ActionKind::Fold { assert!(!spot.is_legal(ActionKind::Check), "fold with check legal. {ctx}"); } } assert!((0.0..=1.0).contains(&decision.confidence.value()), "{ctx}"); if let Choice::Raise { to } = decision.choice { assert!(to.is_finite() && to > 0.0, "{ctx}"); assert!(to <= spot.hero_stack, "unaffordable. {ctx}"); if let Some(allowed) = &spot.allowed_sizes { assert!(allowed.contains(&to), "not from allowedSizes. {ctx}"); } if let Some(min) = spot.min_raise_to { assert!(to >= min || to == spot.hero_stack, "below min and not all-in. {ctx}"); } if let Some(max) = spot.max_raise_to { assert!(to <= max, "above max. {ctx}"); } if spot.rules().shape == ActionShape::Binary { assert!(to == spot.hero_stack || Some(to) == spot.max_raise_to || spot.allowed_sizes.is_some(), "{ctx}"); } } let after = settle(spot, decision.choice); assert!(after.stack >= 0.0, "negative stack. {ctx}"); assert!( (after.stack + after.pot - (spot.hero_stack + spot.pot)).abs() < 1e-9, "chips not conserved. {ctx}" ); } #[test] fn postflop_choices_survive_enforcement_on_random_spots() { let rng = Rng::seeded(0xD1CE); let _ = (0..POSTFLOP_CASES).fold(rng, |rng, _| { let (spot, rng) = random_spot(rng); let (est, rng) = random_estimate(rng); let (th, rng) = random_thresholds(rng); let (read, rng) = random_read(rng); let raw = choose_postflop(&spot, &est, &th, &read); let enforced = enforce(&spot, raw); assert_sound(&spot, &enforced, "postflop"); if enforced.choice == Choice::Abstain { assert!(enforced.confidence.value() <= 0.3, "abstain too confident: {enforced:?}"); } rng }); } #[test] fn preflop_plans_survive_enforcement_on_random_spots() { let rng = Rng::seeded(0xBEEF); let _ = (0..CASES).fold(rng, |rng, _| { let (spot, rng) = random_spot(rng); let (plan, rng) = random_plan(rng); let raw = from_plan(&spot, &plan); assert_sound(&spot, &enforce(&spot, raw), "from_plan"); rng }); } #[test] fn enforce_is_sound_for_arbitrary_decisions() { let rng = Rng::seeded(0xFACE); let _ = (0..CASES).fold(rng, |rng, _| { let (spot, rng) = random_spot(rng); let (k, rng) = below(rng, 6); let (size, rng) = chips(rng, 30.0); let choice = match k { 0 => Choice::Fold, 1 => Choice::Check, 2 => Choice::Call, 3 => Choice::Raise { to: size }, 4 => Choice::Raise { to: f64::NAN }, _ => Choice::Abstain, }; assert_sound(&spot, &enforce(&spot, Decision::new(choice, 0.5, "arbitrary")), "enforce"); rng }); } #[test] fn enforce_is_idempotent() { let rng = Rng::seeded(0xABCD); let _ = (0..CASES).fold(rng, |rng, _| { let (spot, rng) = random_spot(rng); let (est, rng) = random_estimate(rng); let (read, rng) = random_read(rng); let once = enforce(&spot, choose_postflop(&spot, &est, &Thresholds::BASE, &read)); let twice = enforce(&spot, once.clone()); assert_eq!(once.choice, twice.choice, "{spot:?}"); rng }); } #[test] fn postflop_raise_sizes_come_from_the_menu_and_confidence_is_bounded_before_enforcement() { let rng = Rng::seeded(0x5EED); let _ = (0..POSTFLOP_CASES).fold(rng, |rng, _| { let (spot, rng) = random_spot(rng); let (est, rng) = random_estimate(rng); let (th, rng) = random_thresholds(rng); let (read, rng) = random_read(rng); let raw = choose_postflop(&spot, &est, &th, &read); assert!((0.0..=1.0).contains(&raw.confidence.value()), "{raw:?}"); if raw.choice == Choice::Abstain { assert!(raw.confidence.value() <= 0.3, "{raw:?}"); } if let (Choice::Raise { to }, Some(menu)) = (raw.choice, &spot.allowed_sizes) { assert!(menu.contains(&to), "raw raise {to} not in {menu:?}"); } rng }); } #[test] fn nothing_beats_nothing_never_raises_on_the_board_alone() { let rng = Rng::seeded(0xB0A2D); let _ = (0..CASES).fold(rng, |rng, _| { let (spot, rng) = random_spot(rng); let (est, rng) = random_estimate(rng); let (th, rng) = random_thresholds(rng); let (read, rng) = random_read(rng); let read = HandRead { made: Made::BoardOnly, ..read }; let raw = choose_postflop(&spot, &est, &th, &read); assert!(!matches!(raw.choice, Choice::Raise { .. }), "{spot:?} {raw:?}"); rng }); }