use rechner::card::Card; use rechner::exploit::thresholds::Thresholds; use rechner::game::Spot; use rechner::game::{ActionKind, Street, Villain}; use rechner::preflop::advise::advise; use rechner::preflop::hand_percentile::percentile; use rechner::preflop::plan::{ChartAction, PreflopPlan}; use rechner::variant::Variant; const EARLY: u8 = 0; const MIDDLE: u8 = 1; const CUTOFF: u8 = 2; const BUTTON: u8 = 3; const SMALL_BLIND: u8 = 4; const BIG_BLIND: u8 = 5; fn cards(text: &str) -> Vec { Card::parse_list(text).expect("valid cards") } fn villain(stack: f64) -> Villain { Villain { seat: 0, stack, folded: false, profile: None, } } /// Six-max, button on seat 3, big blind = 1.0. fn spot_with( variant: Variant, hole: Vec, hero_seat: u8, pot: f64, to_call: f64, stack: f64, ) -> Spot { Spot { variant, big_blind: 1.0, seats: 6, hero_seat, button_seat: Some(3), hero_cards: hole, board: vec![], street: Street::Preflop, pot, to_call, hero_stack: stack, min_raise_to: Some(2.0), max_raise_to: Some(stack), legal: vec![ActionKind::Fold, ActionKind::Call, ActionKind::Raise], allowed_sizes: None, villains: vec![villain(100.0)], seed: 1, history: Vec::new(), } } fn spot(hole: &str, hero_seat: u8, pot: f64, to_call: f64) -> Spot { spot_with(Variant::Nlhe, cards(hole), hero_seat, pot, to_call, 100.0) } fn plan(hole: &str, hero_seat: u8, pot: f64, to_call: f64) -> PreflopPlan { advise(&spot(hole, hero_seat, pot, to_call), &Thresholds::BASE) } fn all_combos() -> Vec> { (0..52u8) .flat_map(|i| (i + 1..52u8).map(move |j| (i, j))) .filter_map(|(i, j)| Some(vec![Card::from_index(i)?, Card::from_index(j)?])) .collect() } /// Share of the 1326 combos for which the chart answers `wanted`. fn share(base: &Spot, thresholds: &Thresholds, wanted: ChartAction) -> f64 { let hits = all_combos() .into_iter() .filter(|hole| { let s = Spot { hero_cards: hole.clone(), ..base.clone() }; advise(&s, thresholds).action == wanted }) .count(); hits as f64 / 1326.0 } fn open_share(hero_seat: u8, thresholds: &Thresholds) -> f64 { let to_call = if hero_seat == SMALL_BLIND { 0.5 } else { 1.0 }; let base = spot("Ah Ad", hero_seat, 1.5, to_call); share(&base, thresholds, ChartAction::Raise) } #[test] fn premium_hands_open_everywhere_and_trash_never_does() { for seat in [EARLY, MIDDLE, CUTOFF, BUTTON, SMALL_BLIND] { let to_call = if seat == SMALL_BLIND { 0.5 } else { 1.0 }; assert_eq!(plan("Ah Ad", seat, 1.5, to_call).action, ChartAction::Raise); assert_eq!(plan("7c 2d", seat, 1.5, to_call).action, ChartAction::Fold); } } #[test] fn opening_range_widens_towards_the_button() { let neutral = Thresholds::BASE; let (e, m, c, b) = ( open_share(EARLY, &neutral), open_share(MIDDLE, &neutral), open_share(CUTOFF, &neutral), open_share(BUTTON, &neutral), ); assert!(e < m && m < c && c < b, "{e} {m} {c} {b}"); // NLHE 6-max opens come from the CFR charts. Early position is wider there than in // real 6-max solver charts (one modelled defender, see `generated_charts`). assert!((0.10..0.35).contains(&e), "early {e}"); assert!((0.33..0.60).contains(&b), "button {b}"); } #[test] fn unknown_position_uses_the_early_chart() { let neutral = Thresholds::BASE; let known = Spot { button_seat: Some(3), ..spot("Ah Ad", EARLY, 1.5, 1.0) }; let unknown = Spot { button_seat: None, hero_seat: BUTTON, ..known.clone() }; let early = share(&known, &neutral, ChartAction::Raise); assert_eq!(share(&unknown, &neutral, ChartAction::Raise), early); } #[test] fn range_width_scale_widens_and_narrows() { let base = Thresholds::BASE; let wide = Thresholds { range_width_scale: 1.3, ..base }; let narrow = Thresholds { range_width_scale: 0.7, ..base }; let (n, b, w) = ( open_share(BUTTON, &narrow), open_share(BUTTON, &base), open_share(BUTTON, &wide), ); assert!(n < b && b < w, "{n} {b} {w}"); let broken = Thresholds { range_width_scale: f64::NAN, ..base }; assert_eq!(open_share(BUTTON, &broken), b); } #[test] fn heads_up_button_opens_most_hands() { let mut s = spot("Ah Ad", BUTTON, 1.5, 0.5); s.seats = 2; s.hero_seat = 0; s.button_seat = Some(0); assert!(share(&s, &Thresholds::BASE, ChartAction::Raise) > 0.6); } #[test] fn big_blind_checks_its_option_with_anything() { let p = plan("7c 2d", BIG_BLIND, 2.5, 0.0); assert_eq!(p.action, ChartAction::Call); } #[test] fn facing_a_limp_isolates_with_strong_hands() { assert_eq!(plan("Ah Ad", BUTTON, 2.5, 1.0).action, ChartAction::Raise); assert_eq!(plan("7c 2d", BUTTON, 2.5, 1.0).action, ChartAction::Fold); } #[test] fn facing_an_open_three_bets_calls_or_folds() { assert_eq!(plan("Ah Ad", BUTTON, 4.5, 3.0).action, ChartAction::Raise); assert_eq!(plan("Ah Kd", BUTTON, 4.5, 3.0).action, ChartAction::Raise); assert_eq!(plan("Jh Th", BUTTON, 4.5, 3.0).action, ChartAction::Call); assert_eq!(plan("7c 2d", BUTTON, 4.5, 3.0).action, ChartAction::Fold); // big blind defends wider than an early seat against the same price. let bb_calls = share( &spot("Ah Ad", BIG_BLIND, 4.5, 2.0), &Thresholds::BASE, ChartAction::Call, ); let ep_calls = share( &spot("Ah Ad", EARLY, 4.5, 3.0), &Thresholds::BASE, ChartAction::Call, ); assert!(bb_calls > ep_calls); } #[test] fn facing_a_three_bet_continues_only_with_the_top() { assert_eq!(plan("Ah Ad", BUTTON, 13.5, 9.0).action, ChartAction::Raise); assert_eq!(plan("Kh Jd", BUTTON, 13.5, 9.0).action, ChartAction::Fold); let continuing = 1.0 - share( &spot("Ah Ad", BUTTON, 13.5, 9.0), &Thresholds::BASE, ChartAction::Fold, ); assert!(continuing < 0.12, "continue share {continuing}"); } #[test] fn short_stacks_turn_raises_into_jams() { let short = spot_with(Variant::Nlhe, cards("Ah Ad"), BUTTON, 4.5, 3.0, 15.0); assert_eq!(advise(&short, &Thresholds::BASE).action, ChartAction::Jam); let deep = spot_with(Variant::Nlhe, cards("Ah Ad"), BUTTON, 4.5, 3.0, 100.0); assert_eq!(advise(&deep, &Thresholds::BASE).action, ChartAction::Raise); } #[test] fn facing_an_all_in_depends_on_the_price() { let call_share = |pot: f64| { let s = spot_with(Variant::Nlhe, cards("Ah Ad"), BUTTON, pot, 100.0, 100.0); share(&s, &Thresholds::BASE, ChartAction::Call) }; let cheap = call_share(300.0); let dear = call_share(100.0); assert!(cheap > dear, "{cheap} {dear}"); assert!(dear < 0.11); let aces = spot_with(Variant::Nlhe, cards("Ah Ad"), BUTTON, 150.0, 100.0, 100.0); assert_eq!(advise(&aces, &Thresholds::BASE).action, ChartAction::Call); let trash = spot_with(Variant::Nlhe, cards("7c 2d"), BUTTON, 150.0, 100.0, 100.0); assert_eq!(advise(&trash, &Thresholds::BASE).action, ChartAction::Fold); } #[test] fn all_in_or_fold_only_ever_jams_or_folds() { let stacks = [3.0, 5.0, 10.0, 20.0, 40.0]; for stack in stacks { for seat in [EARLY, CUTOFF, BUTTON, SMALL_BLIND, BIG_BLIND] { for (pot, to_call) in [(1.5, 1.0), (2.5, 1.0), (4.5, 3.0), (stack + 1.5, stack)] { let base = spot_with(Variant::Aof, cards("Ah Ad"), seat, pot, to_call, stack); for hole in all_combos().into_iter().step_by(37) { let s = Spot { hero_cards: hole, ..base.clone() }; let action = advise(&s, &Thresholds::BASE).action; assert!( matches!(action, ChartAction::Jam | ChartAction::Fold), "{action:?}" ); } } } } } #[test] fn all_in_or_fold_gets_wider_when_shorter() { let jam_share = |stack: f64| { let base = spot_with(Variant::Aof, cards("Ah Ad"), BUTTON, 1.5, 1.0, stack); share(&base, &Thresholds::BASE, ChartAction::Jam) }; let (s5, s10, s20, s40) = ( jam_share(5.0), jam_share(10.0), jam_share(20.0), jam_share(40.0), ); assert!(s5 > s10 && s10 > s20 && s20 > s40, "{s5} {s10} {s20} {s40}"); let deep_aces = spot_with(Variant::Aof, cards("Ah Ad"), EARLY, 1.5, 1.0, 40.0); assert_eq!( advise(&deep_aces, &Thresholds::BASE).action, ChartAction::Jam ); let deep_trash = spot_with(Variant::Aof, cards("7c 2d"), EARLY, 1.5, 1.0, 40.0); assert_eq!( advise(&deep_trash, &Thresholds::BASE).action, ChartAction::Fold ); } #[test] fn all_in_or_fold_uses_the_effective_stack() { let mut covered = spot_with(Variant::Aof, cards("9h 8h"), BUTTON, 1.5, 1.0, 40.0); covered.villains = vec![villain(6.0)]; let mut covering = covered.clone(); covering.villains = vec![villain(80.0)]; let jam = |s: &Spot| share(s, &Thresholds::BASE, ChartAction::Jam); assert!(jam(&covered) > jam(&covering)); } #[test] fn plan_carries_percentile_certainty_and_reason() { let aces = plan("Ah Ad", BUTTON, 1.5, 1.0); assert_eq!(aces.percentile, percentile(Variant::Nlhe, &cards("Ah Ad"))); assert!((0.0..=1.0).contains(&aces.certainty)); assert!(aces.certainty > 0.9); assert!( aces.reason.contains("BTN") && aces.reason.contains("open"), "{}", aces.reason ); assert!( (aces.reason.contains("top") || aces.reason.contains("CFR")) && aces.reason.contains('%'), "{}", aces.reason ); let all_certainties_ok = all_combos().into_iter().step_by(11).all(|hole| { let s = Spot { hero_cards: hole, ..spot("Ah Ad", BUTTON, 1.5, 1.0) }; let p = advise(&s, &Thresholds::BASE); (0.0..=1.0).contains(&p.certainty) && !p.reason.is_empty() }); assert!(all_certainties_ok); } #[test] fn borderline_hands_are_less_certain_than_clear_ones() { // Nine-handed: the heuristic chart with its percentile border (the 6-max CFR charts // are almost pure strategies, their certainty is `2p - 1`). let base = Spot { seats: 9, ..spot("Ah Ad", BUTTON, 1.5, 1.0) }; let by_certainty: Vec = all_combos() .into_iter() .map(|hole| { advise( &Spot { hero_cards: hole, ..base.clone() }, &Thresholds::BASE, ) .certainty }) .collect(); assert!(by_certainty.iter().any(|c| *c < 0.2)); assert!(by_certainty.iter().any(|c| *c > 0.95)); } #[test] fn omaha_five_cards_use_the_same_charts() { let strong = spot_with( Variant::Plo5, cards("Ah Ad Kh Kd Jc"), BUTTON, 1.5, 1.0, 100.0, ); let junk = spot_with( Variant::Plo5, cards("2c 7d 4h 9s 3c"), BUTTON, 1.5, 1.0, 100.0, ); assert_eq!( advise(&strong, &Thresholds::BASE).action, ChartAction::Raise ); assert_eq!(advise(&junk, &Thresholds::BASE).action, ChartAction::Fold); } #[test] fn omaha_four_cards_use_the_same_charts_with_their_own_ranking() { let strong = spot_with(Variant::Plo4, cards("Ah Ad Kh Kd"), BUTTON, 1.5, 1.0, 100.0); let junk = spot_with(Variant::Plo4, cards("2c 7d 4h 9s"), BUTTON, 1.5, 1.0, 100.0); assert_eq!(advise(&strong, &Thresholds::BASE).action, ChartAction::Raise); assert_eq!(advise(&junk, &Thresholds::BASE).action, ChartAction::Fold); // Two or five cards are not a PLO4 hand. let five = spot_with(Variant::Plo4, cards("Ah Ad Kh Kd Jc"), BUTTON, 1.5, 1.0, 100.0); assert_eq!(advise(&five, &Thresholds::BASE).percentile, 1.0); } #[test] fn invalid_hole_cards_fold_without_panicking() { let one = spot_with(Variant::Nlhe, cards("Ah"), BUTTON, 1.5, 1.0, 100.0); let plan = advise(&one, &Thresholds::BASE); assert_eq!(plan.action, ChartAction::Fold); assert_eq!(plan.percentile, 1.0); let none = spot_with(Variant::Aof, vec![], BUTTON, 1.5, 1.0, 10.0); assert_eq!(advise(&none, &Thresholds::BASE).action, ChartAction::Fold); } #[test] fn degenerate_numbers_do_not_panic() { let weird = [ (f64::NAN, f64::NAN, f64::NAN), (0.0, 0.0, 0.0), (-1.0, -1.0, -1.0), ]; for (pot, to_call, stack) in weird { for variant in Variant::ALL { let hole = match variant { Variant::Plo5 => "Ah Ad Kh Kd Jc", Variant::Plo4 => "Ah Ad Kh Kd", _ => "Ah Ad", }; let s = spot_with(variant, cards(hole), BUTTON, pot, to_call, stack); let p = advise(&s, &Thresholds::BASE); assert!((0.0..=1.0).contains(&p.certainty)); } } }