//! Opponents that look at their cards: fixed rules on equity against a random hand. Not a model //! of real players, but they punish what card-blind opponents cannot, such as paying off strong //! hands or bluffing into a caller. use super::opponent::{Act, View}; use crate::card::{deck, Card}; use crate::hand::fast7::{mask, score}; use crate::rng::Rng; /// Boards drawn per strength estimate. const STRENGTH_SAMPLES: usize = 200; /// Up to this price (bb) a preflop raise is an open, above it a re-raise. pub(crate) const OPEN_PRICE: f64 = 1.0; /// Only hands below this strength bluff. pub(crate) const BLUFF_BELOW: f64 = 0.45; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Style { Reg, Nit, Maniac, Fish, /// Near-GTO range player; its real play lives in `sim::gto` (needs the table context). Gto, /// A second balanced range player: small-ball, half-pot, value-leaning (`sim::gto`, `Shape::SMALL`). /// An independent balanced target so a defensive fix must generalise, not overfit the one `Gto`. GtoSmall, } /// What the table tells a reading opponent besides the `View`. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Reading { /// Equity against one random hand, 0 to 1. pub strength: f64, pub pot: f64, /// Already in front of it on this street. pub own_bet: f64, pub preflop: bool, } /// The six thresholds that define a reading opponent's whole play. A named `Style` is one point in /// this space; the synthetic population (`sim::population`) draws arbitrary points around the style /// means, so one ruleset = one individual opponent. All fields public so the population can build them. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Rules { /// Equity it wants above the pot odds to call. pub call_margin: f64, /// Raises a bet from here. pub raise_at: f64, /// Bets when checked to from here. pub bet_at: f64, /// Opens preflop from here. pub open_at: f64, /// How often it bluffs a weak hand when checked to. pub bluff: f64, /// Bet size as a share of the pot after calling. pub size: f64, } impl Style { pub const ALL: [Style; 4] = [Style::Reg, Style::Nit, Style::Maniac, Style::Fish]; pub fn name(self) -> &'static str { match self { Style::Reg => "reg", Style::Nit => "nit", Style::Maniac => "maniac", Style::Fish => "fish", Style::Gto => "gto", Style::GtoSmall => "gto-small", } } /// The betting shape for the two balanced range players, `None` for the threshold styles. pub fn gto_shape(self) -> Option { match self { Style::Gto => Some(super::gto::Shape::BALANCED), Style::GtoSmall => Some(super::gto::Shape::SMALL), _ => None, } } pub fn rules(self) -> Rules { match self { Style::Reg => Rules { call_margin: 0.22, raise_at: 0.82, bet_at: 0.68, open_at: 0.62, bluff: 0.08, size: 2.0 / 3.0 }, Style::Nit => Rules { call_margin: 0.32, raise_at: 0.90, bet_at: 0.78, open_at: 0.70, bluff: 0.0, size: 0.5 }, Style::Maniac => Rules { call_margin: 0.08, raise_at: 0.70, bet_at: 0.55, open_at: 0.52, bluff: 0.35, size: 1.0 }, // Stand-in only: the table plays `Gto`/`GtoSmall` through `sim::gto`, not these thresholds. Style::Gto | Style::GtoSmall => Rules { call_margin: 0.22, raise_at: 0.82, bet_at: 0.68, open_at: 0.62, bluff: 0.08, size: 2.0 / 3.0 }, Style::Fish => Rules { call_margin: 0.02, raise_at: 0.92, bet_at: 0.80, open_at: 0.78, bluff: 0.02, size: 0.4 }, } } pub fn act(self, view: View, reading: Reading, rng: Rng) -> (Act, Rng) { act_with(self.rules(), view, reading, rng) } } /// Play an explicit ruleset (the behaviour of any drawn opponent, not only a named `Style`). Same /// threshold logic `Style::act` used; lifted out so the population can drive arbitrary `Rules`. pub fn act_with(rules: Rules, view: View, reading: Reading, rng: Rng) -> (Act, Rng) { let s = reading.strength; let to_call = view.to_call; let raise_from = match (reading.preflop && to_call <= OPEN_PRICE, to_call > 0.0) { (true, _) => rules.open_at, (false, true) => rules.raise_at, (false, false) => rules.bet_at, }; let raise = Act::RaiseTo(reading.own_bet + to_call + rules.size * (reading.pot + to_call)); let can_raise = view.min_raise_to.is_some(); if can_raise && s >= raise_from { return (raise, rng); } if to_call > 0.0 { let price = to_call / (reading.pot + to_call); return (if s >= price + rules.call_margin { Act::Call } else { Act::Fold }, rng); } if can_raise && !reading.preflop && s < BLUFF_BELOW { let (u, rng) = rng.unit(); return (if u < rules.bluff { raise } else { Act::Check }, rng); } (Act::Check, rng) } /// Equity of `hole` against one random hand, ties counting half; seed-fixed Monte Carlo. pub fn strength(hole: &[Card], board: &[Card], rng: Rng) -> f64 { let mut known = hole.to_vec(); known.extend_from_slice(board); let pool = deck::without(&known); let missing = 5 - board.len(); let mut rng = rng; let mut won = 0.0; for _ in 0..STRENGTH_SAMPLES { let (cards, next) = deck::draw(&pool, 2 + missing, rng).expect("enough cards"); rng = next; let mut full = board.to_vec(); full.extend_from_slice(&cards[2..]); // `fast7::score` orders hands exactly like `best` (its spec), so the result is bit-identical. let b = mask(&full); let (mine, theirs) = (score(mask(hole) | b), score(mask(&cards[..2]) | b)); won += match mine.cmp(&theirs) { std::cmp::Ordering::Greater => 1.0, std::cmp::Ordering::Equal => 0.5, std::cmp::Ordering::Less => 0.0, }; } won / STRENGTH_SAMPLES as f64 } /// Equity of `hole` against `others` random hands at once (ties split), seed-fixed Monte Carlo: the /// multiway generalisation of `strength`, so a threshold keeps its meaning ("a favourite against the /// FIELD") in a pot with several players. Used by the mixed 6-max table postflop (`sim::sechsmax`). pub fn strength_field(hole: &[Card], board: &[Card], others: usize, rng: Rng) -> f64 { let mut known = hole.to_vec(); known.extend_from_slice(board); let pool = deck::without(&known); let missing = 5 - board.len(); let mut rng = rng; let mut won = 0.0; for _ in 0..STRENGTH_SAMPLES { let (cards, next) = deck::draw(&pool, 2 * others + missing, rng).expect("enough cards"); rng = next; let mut full = board.to_vec(); full.extend_from_slice(&cards[2 * others..]); let b = mask(&full); let mine = score(mask(hole) | b); let mut ties = 1.0; let mut lost = false; for o in 0..others { let theirs = score(mask(&cards[2 * o..2 * o + 2]) | b); if theirs > mine { lost = true; break; } if theirs == mine { ties += 1.0; } } if !lost { won += 1.0 / ties; } } won / STRENGTH_SAMPLES as f64 }