Files
pokerfreak/src/preflop/button.rs
T
mmintel 836c16c9a4 Sprechweise geputzt: kein echt/live/Mensch mehr in Kommentaren und Texten
Die Welt soll nichts verraten. Woerter, die ein Gegenueber implizieren (echt vs. unecht,
live, Mensch), sind aus Prosa, Kommentaren und Docstrings raus; Namen (mensch_ruhe_s,
echte_chops) und der Altwert "echt" im Spielplan bleiben. Nur Kommentare und Docstrings
angefasst, damit keine Einrueckung faellt.
2026-11-01 22:11:05 +01:00

76 lines
2.7 KiB
Rust

//! The dealer button: given explicitly, or inferred from the posted blinds.
use crate::game::{SeenAction, Spot, Street};
const MIN_SEATS: u8 = 2;
const MAX_SEATS: u8 = 10;
/// Seat of the button.
///
/// 1. An explicit `buttonSeat` always wins (even if the history disagrees).
/// 2. Otherwise the first preflop `POST_SB` in `history`: the button sits one seat before
/// the small blind; heads-up the small blind *is* the button.
/// 3. Otherwise the first preflop `POST_BB`: two seats before it; heads-up the other seat.
/// 4. Otherwise `None`.
///
/// Seat numbers may start at 0 or at 1. The numbering is read off every seat that appears
/// (hero, villains, history): a seat `0` means 0-based, a seat `seats` means 1-based. If
/// both readings stay possible and they disagree (the answer wraps around the table),
/// the result is `None`. If every seat is out of range for both readings, `None` too.
/// Needs `2 <= seats <= 10`.
pub fn button_seat(spot: &Spot) -> Option<u8> {
spot.button_seat.or_else(|| inferred(spot))
}
fn first_post(spot: &Spot, kind: SeenAction) -> Option<u8> {
spot.history
.iter()
.find(|r| r.street == Street::Preflop && r.action == kind)
.map(|r| r.seat)
}
/// Seats in play, as far as the request tells.
fn seats_seen(spot: &Spot) -> impl Iterator<Item = u8> + '_ {
std::iter::once(spot.hero_seat)
.chain(spot.villains.iter().map(|v| v.seat))
.chain(spot.history.iter().map(|r| r.seat))
}
/// Numbering bases (0 or 1) under which every seat in play is a valid seat number.
fn possible_bases(spot: &Spot) -> Vec<u8> {
[0u8, 1]
.into_iter()
.filter(|base| {
seats_seen(spot).all(|s| s >= *base && (s as u16) < *base as u16 + spot.seats as u16)
})
.collect()
}
/// `steps` seats counter-clockwise from `seat`, wrapping inside `base..base + seats`.
fn step_back(seat: u8, steps: u8, seats: u8, base: u8) -> u8 {
(seat - base + seats - steps) % seats + base
}
fn inferred(spot: &Spot) -> Option<u8> {
if !(MIN_SEATS..=MAX_SEATS).contains(&spot.seats) {
return None;
}
let heads_up = spot.seats == 2;
let (anchor, steps) = match (
first_post(spot, SeenAction::PostSmallBlind),
first_post(spot, SeenAction::PostBigBlind),
) {
(Some(sb), _) => (sb, if heads_up { 0 } else { 1 }),
(None, Some(bb)) => (bb, if heads_up { 1 } else { 2 }),
(None, None) => return None,
};
let answers: Vec<u8> = possible_bases(spot)
.into_iter()
.map(|base| step_back(anchor, steps, spot.seats, base))
.collect();
match answers.split_first() {
Some((first, rest)) if rest.iter().all(|a| a == first) => Some(*first),
_ => None,
}
}