Files
pokerfreak/src/cfr/hand_class.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

120 lines
4.0 KiB
Rust

//! The 169 hold'em starting-hand classes on a fixed 13 x 13 grid.
//!
//! Class id = `row * 13 + col` with rank indices `Two = 0.. Ace = 12`:
//! pairs on the diagonal, suited above it (`row` = high rank, `col` = low rank),
//! offsuit below it (`row` = low rank, `col` = high rank). The grid is the same for every deck;
//! a deck without the low ranks (short deck) simply leaves those cells empty
//! (`class_exists` is false), so the generated tables keep one layout for all variants.
use crate::card::{Card, Rank, Suit};
use crate::variant::DeckRule;
pub const CLASS_COUNT: usize = 169;
/// Dense class index `0..CLASS_COUNT`.
pub type ClassId = usize;
const RANKS: usize = 13;
/// Class of a hand with the given ranks (any order). `suited` is ignored for pairs.
pub fn class_id(a: Rank, b: Rank, suited: bool) -> ClassId {
let (high, low) = if a >= b { (a as usize, b as usize) } else { (b as usize, a as usize) };
if high == low {
high * RANKS + high
} else if suited {
high * RANKS + low
} else {
low * RANKS + high
}
}
/// `None` when both cards are the same card.
pub fn class_of(a: Card, b: Card) -> Option<ClassId> {
(a != b).then(|| class_id(a.rank, b.rank, a.suit == b.suit))
}
/// `(high, low, suited)` rank indices of a class; pairs report `suited = false`.
fn decode(id: ClassId) -> Option<(usize, usize, bool)> {
(id < CLASS_COUNT).then(|| {
let (row, col) = (id / RANKS, id % RANKS);
match row.cmp(&col) {
std::cmp::Ordering::Equal => (row, row, false),
std::cmp::Ordering::Greater => (row, col, true),
std::cmp::Ordering::Less => (col, row, false),
}
})
}
/// `"AA"`, `"AKs"`, `"T9o"`.
pub fn class_name(id: ClassId) -> Option<String> {
decode(id).map(|(high, low, suited)| {
let kind = if high == low {
""
} else if suited {
"s"
} else {
"o"
};
format!("{}{}{}", Rank::ALL[high].symbol(), Rank::ALL[low].symbol(), kind)
})
}
/// Inverse of `class_name`. Pairs take no suffix, other classes need `s` or `o`.
pub fn class_parse(name: &str) -> Option<ClassId> {
let chars: Vec<char> = name.trim().chars().collect();
let rank = |c: char| Rank::parse(&c.to_string());
match chars.as_slice() {
[a, b] => {
let (a, b) = (rank(*a)?, rank(*b)?);
(a == b).then(|| class_id(a, b, false))
}
[a, b, kind] => {
let (a, b) = (rank(*a)?, rank(*b)?);
if a == b {
return None;
}
match kind {
's' | 'S' => Some(class_id(a, b, true)),
'o' | 'O' => Some(class_id(a, b, false)),
_ => None,
}
}
_ => None,
}
}
/// Every two-card combination of the class in a full 52-card deck (6 / 4 / 12).
pub fn class_combos(id: ClassId) -> Vec<[Card; 2]> {
let Some((high, low, suited)) = decode(id) else {
return Vec::new();
};
let (high, low) = (Rank::ALL[high], Rank::ALL[low]);
let suits = Suit::ALL;
if high == low {
(0..4)
.flat_map(|i| (i + 1..4).map(move |j| (i, j)))
.map(|(i, j)| [Card::new(high, suits[i]), Card::new(low, suits[j])])
.collect()
} else if suited {
suits.iter().map(|s| [Card::new(high, *s), Card::new(low, *s)]).collect()
} else {
suits
.iter()
.flat_map(|a| suits.iter().filter(move |b| *b != a).map(move |b| (*a, *b)))
.map(|(a, b)| [Card::new(high, a), Card::new(low, b)])
.collect()
}
}
/// True when both ranks of the class are in the deck.
pub fn class_exists(deck: DeckRule, id: ClassId) -> bool {
decode(id).is_some_and(|(high, low, _)| {
deck.contains(Rank::ALL[high]) && deck.contains(Rank::ALL[low])
})
}
/// All classes that exist in the deck, in id order.
pub fn classes_in(deck: DeckRule) -> Vec<ClassId> {
(0..CLASS_COUNT).filter(|id| class_exists(deck, *id)).collect()
}