read: board texture, hand read (made class, draws, outs, nut draw) and fast glance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 12:43:39 +01:00
co-authored by Claude Opus 5.5
parent bdf3e0d27c
commit 6614cf926b
22 changed files with 1384 additions and 8 deletions
+76
View File
@@ -0,0 +1,76 @@
use crate::card::Card;
/// Rank and suit tallies of hero's hole cards and the board.
/// Rank masks use the pip value (2..=14) as bit index, so an ace is bit 14.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Census {
hole: [u8; 15],
board: [u8; 15],
pub hole_mask: u16,
pub board_mask: u16,
/// Rank mask per suit (index = `Suit as usize`).
pub hole_suit: [u16; 4],
pub board_suit: [u16; 4],
pub board_len: usize,
}
/// The bit of one pip value (0 for anything outside the mask width).
pub fn bit(pip: u8) -> u16 {
1u16.checked_shl(pip as u32).unwrap_or(0)
}
/// Pip of the highest set bit, 0 for an empty mask.
pub fn top_pip(mask: u16) -> u8 {
if mask == 0 {
0
} else {
15 - mask.leading_zeros() as u8
}
}
impl Census {
pub fn of(hole: &[Card], board: &[Card]) -> Census {
let mut census = Census {
hole: [0; 15],
board: [0; 15],
hole_mask: 0,
board_mask: 0,
hole_suit: [0; 4],
board_suit: [0; 4],
board_len: board.len(),
};
for card in hole {
let (pip, suit) = (card.rank.value(), card.suit as usize);
census.hole[pip as usize] += 1;
census.hole_mask |= bit(pip);
census.hole_suit[suit] |= bit(pip);
}
for card in board {
let (pip, suit) = (card.rank.value(), card.suit as usize);
census.board[pip as usize] += 1;
census.board_mask |= bit(pip);
census.board_suit[suit] |= bit(pip);
}
census
}
pub fn hole_count(&self, pip: u8) -> u8 {
self.hole.get(pip as usize).copied().unwrap_or(0)
}
pub fn board_count(&self, pip: u8) -> u8 {
self.board.get(pip as usize).copied().unwrap_or(0)
}
pub fn total_count(&self, pip: u8) -> u8 {
self.hole_count(pip) + self.board_count(pip)
}
pub fn hole_suited(&self, suit: usize) -> u32 {
self.hole_suit.get(suit).map_or(0, |m| m.count_ones())
}
pub fn board_suited(&self, suit: usize) -> u32 {
self.board_suit.get(suit).map_or(0, |m| m.count_ones())
}
}
+31
View File
@@ -0,0 +1,31 @@
use crate::card::Card;
use crate::fault::Fault;
use crate::variant::Rules;
fn invalid(field: &str, detail: String) -> Fault {
Fault::Invalid { field: field.into(), detail }
}
/// Flop, turn or river; the variant's number of hole cards; every card in the deck; no duplicates.
pub fn check(rules: &Rules, hole: &[Card], board: &[Card]) -> Result<(), Fault> {
if !(3..=5).contains(&board.len()) {
return Err(invalid("board", format!("expected 3 to 5 cards, got {}", board.len())));
}
if hole.len() != rules.hole_cards {
return Err(invalid(
"hole",
format!("expected {} cards, got {}", rules.hole_cards, hole.len()),
));
}
hole.iter().chain(board).try_fold(0u64, |seen, card| {
let flag = 1u64 << card.index();
if !rules.deck.contains(card.rank) {
Err(invalid("cards", format!("{} is not in this deck", card.short())))
} else if seen & flag != 0 {
Err(Fault::Inconsistent(format!("duplicate card {}", card.short())))
} else {
Ok(seen | flag)
}
})?;
Ok(())
}
+35
View File
@@ -0,0 +1,35 @@
//! Strongest draw of hero's hand.
//!
//! No draw on the river and none once hero already has a straight or better. `Combo` is a flush draw
//! together with a straight draw or with a pair of hero's own (underpair up to trips). `Backdoor` exists on the flop only.
use super::census::Census;
use super::draw::Draw;
use super::flush_draw::{backdoor_flush, flush_draw_suits};
use super::made::Made;
use super::straight_draw::{backdoor_straight, completing_ranks};
use crate::variant::Rules;
pub fn draw_of(rules: &Rules, census: &Census, made: Made) -> Draw {
if census.board_len >= 5 || made >= Made::Straight {
return Draw::None;
}
let flush = flush_draw_suits(rules.hole_usage, census) != 0;
let ends = completing_ranks(rules, census);
let paired = (Made::Underpair..=Made::Trips).contains(&made);
match (flush, ends) {
(true, 0) if !paired => Draw::Flush,
(true, _) => Draw::Combo,
(false, 0) => backdoor(rules, census),
(false, 1) => Draw::Gutshot,
(false, _) => Draw::OpenEnded,
}
}
fn backdoor(rules: &Rules, census: &Census) -> Draw {
if backdoor_flush(rules.hole_usage, census) || backdoor_straight(rules, census) {
Draw::Backdoor
} else {
Draw::None
}
}
+34
View File
@@ -0,0 +1,34 @@
//! Flush draws and backdoor flush draws, per hole-card usage.
//!
//! Hold'em family: four cards of a suit among hole and board with at least one hole card.
//! Omaha family: exactly two hole cards of the suit and two board cards of it.
//! Backdoor (flop only): three suited cards that use hole cards in the same way.
use super::census::Census;
use crate::variant::HoleUsage;
fn draw_in(usage: HoleUsage, hole: u32, board: u32) -> bool {
match usage {
HoleUsage::BestFiveOfAll => hole >= 1 && hole + board == 4,
HoleUsage::ExactlyTwoHole => hole >= 2 && board == 2,
}
}
fn backdoor_in(usage: HoleUsage, hole: u32, board: u32) -> bool {
match usage {
HoleUsage::BestFiveOfAll => hole >= 1 && hole + board == 3,
HoleUsage::ExactlyTwoHole => hole >= 2 && board == 1,
}
}
/// Bit `s` is set when the suit with index `s` is a flush draw for hero.
pub fn flush_draw_suits(usage: HoleUsage, census: &Census) -> u8 {
(0..4usize)
.filter(|&s| draw_in(usage, census.hole_suited(s), census.board_suited(s)))
.fold(0, |mask, s| mask | 1 << s)
}
pub fn backdoor_flush(usage: HoleUsage, census: &Census) -> bool {
census.board_len == 3
&& (0..4usize).any(|s| backdoor_in(usage, census.hole_suited(s), census.board_suited(s)))
}
+12 -3
View File
@@ -1,12 +1,21 @@
use super::census::Census;
use super::check::check;
use super::draw::Draw;
use super::draw_class::draw_of;
use super::made::Made;
use super::made_class::classify;
use crate::card::Card;
use crate::fault::Fault;
use crate::hand::showdown::judge;
use crate::variant::Rules;
/// Fast path of `hand_read::read`: made class and draw only (no outs, no texture).
/// Called per sampled villain hand inside Monte Carlo range filters, so it must be cheap.
/// Called per sampled villain hand inside Monte Carlo range filters, so it is cheap:
/// one `judge` call, rank/suit masks, no per-card enumeration.
pub fn glance(rules: &Rules, hole: &[Card], board: &[Card]) -> Result<(Made, Draw), Fault> {
let _ = (rules, hole, board);
todo!("read::glance")
check(rules, hole, board)?;
let census = Census::of(hole, board);
let strength = judge(rules, hole, board)?;
let made = classify(rules, &census, &strength);
Ok((made, draw_of(rules, &census, made)))
}
+15 -3
View File
@@ -1,6 +1,10 @@
use super::census::Census;
use super::draw::Draw;
use super::glance::glance;
use super::made::Made;
use super::texture::Texture;
use super::nut_draw::nut_draw;
use super::outs::outs;
use super::texture::{texture, Texture};
use crate::card::Card;
use crate::fault::Fault;
use crate::variant::Rules;
@@ -18,7 +22,15 @@ pub struct HandRead {
}
/// Reads `hole` against a flop, turn or river `board` (3–5 cards).
/// `made` and `draw` are exactly what `glance` returns; see `outs` for what an out is.
pub fn read(rules: &Rules, hole: &[Card], board: &[Card]) -> Result<HandRead, Fault> {
let _ = (rules, hole, board);
todo!("read::hand_read")
let (made, draw) = glance(rules, hole, board)?;
let census = Census::of(hole, board);
Ok(HandRead {
made,
draw,
outs: outs(rules, hole, board, made),
nut_draw: nut_draw(rules, &census, draw),
texture: texture(rules, board),
})
}
+44
View File
@@ -0,0 +1,44 @@
//! Does a made straight / flush / straight flush really use one of hero's hole cards?
//! (A straight or flush that lies completely on the board is just the board.)
use super::census::{bit, Census};
use super::windows::window_of_high;
use crate::hand::category::Category;
use crate::hand::strength::Strength;
use crate::variant::{HoleUsage, Rules};
/// Omaha always plays exactly two hole cards. In the Hold'em family the hand needs a hole card
/// when its ranks (suit) are not all on the board.
pub fn uses_hole(rules: &Rules, census: &Census, strength: &Strength) -> bool {
match rules.hole_usage {
HoleUsage::ExactlyTwoHole => true,
HoleUsage::BestFiveOfAll => match strength.category {
Category::Straight => straight_needs_hole(rules, census, strength),
Category::Flush => flush_suit(census).is_none_or(|s| flush_needs_hole(census, s)),
Category::StraightFlush => flush_suit(census)
.is_none_or(|s| straight_flush_needs_hole(rules, census, strength, s)),
_ => true,
},
}
}
fn straight_needs_hole(rules: &Rules, census: &Census, strength: &Strength) -> bool {
window_of_high(rules.deck, strength.ranks[0]) & !census.board_mask != 0
}
fn straight_flush_needs_hole(rules: &Rules, census: &Census, strength: &Strength, suit: usize) -> bool {
let on_board = census.board_suit.get(suit).copied().unwrap_or(0);
window_of_high(rules.deck, strength.ranks[0]) & !on_board != 0
}
/// The suit with five or more cards among hole and board (at most one in Hold'em).
fn flush_suit(census: &Census) -> Option<usize> {
(0..4).find(|&s| census.hole_suited(s) + census.board_suited(s) >= 5)
}
/// Hero's flush needs a hole card when one of them is among the five highest cards of the suit.
fn flush_needs_hole(census: &Census, suit: usize) -> bool {
let hole = census.hole_suit[suit];
let all = hole | census.board_suit[suit];
(2..=14u8).rev().filter(|p| all & bit(*p) != 0).take(5).any(|p| hole & bit(p) != 0)
}
+43
View File
@@ -0,0 +1,43 @@
//! Made-hand class from the strength `judge` returned, relative to the board.
//!
//! A hand made of rank groups (pair, two pair, trips, full house, quads) is `BoardOnly` when the board
//! alone already holds every group (kickers do not count as an improvement). Straights and flushes are
//! `BoardOnly` when they lie completely on the board. Everything else is classified by what hero holds.
use super::census::Census;
use super::hole_use::uses_hole;
use super::made::Made;
use super::pair_class::improved_pair;
use crate::hand::category::Category;
use crate::hand::strength::Strength;
use crate::variant::Rules;
pub fn classify(rules: &Rules, census: &Census, strength: &Strength) -> Made {
let rank = |i: usize| strength.ranks.get(i).copied().unwrap_or(0);
let on_board = |i: usize| census.board_count(rank(i));
match strength.category {
Category::HighCard => Made::Air,
Category::Pair if on_board(0) >= 2 => Made::BoardOnly,
Category::Pair => improved_pair(census, rank(0)),
Category::TwoPair if on_board(0) >= 2 && on_board(1) >= 2 => Made::BoardOnly,
Category::TwoPair => Made::TwoPair,
Category::ThreeOfAKind if on_board(0) >= 3 => Made::BoardOnly,
Category::ThreeOfAKind if census.hole_count(rank(0)) >= 2 => Made::Set,
Category::ThreeOfAKind => Made::Trips,
Category::FullHouse if on_board(0) >= 3 && on_board(1) >= 2 => Made::BoardOnly,
Category::FullHouse => Made::FullHouse,
Category::FourOfAKind if on_board(0) >= 4 => Made::BoardOnly,
Category::FourOfAKind => Made::Quads,
Category::Straight => needs_hole(rules, census, strength, Made::Straight),
Category::Flush => needs_hole(rules, census, strength, Made::Flush),
Category::StraightFlush => needs_hole(rules, census, strength, Made::StraightFlush),
}
}
fn needs_hole(rules: &Rules, census: &Census, strength: &Strength, made: Made) -> Made {
if uses_hole(rules, census, strength) {
made
} else {
Made::BoardOnly
}
}
+12
View File
@@ -1,10 +1,22 @@
//! Reading a hand against the board: texture, made-hand class, draws.
pub mod census;
pub mod check;
pub mod draw;
pub mod draw_class;
pub mod flush_draw;
pub mod glance;
pub mod hand_read;
pub mod hole_use;
pub mod made;
pub mod made_class;
pub mod nut_draw;
pub mod outs;
pub mod pair_class;
pub mod pairing;
pub mod straight_draw;
pub mod suit_pattern;
pub mod texture;
pub mod texture_wetness;
pub mod tier;
pub mod windows;
+20
View File
@@ -0,0 +1,20 @@
//! Nut flush draw: hero holds the highest card of the suit that is not on the board
//! (the ace, or the next one down when the ace is on the board).
use super::census::{bit, top_pip, Census};
use super::draw::Draw;
use super::flush_draw::flush_draw_suits;
use super::windows::deck_mask;
use crate::variant::Rules;
pub fn nut_draw(rules: &Rules, census: &Census, draw: Draw) -> bool {
matches!(draw, Draw::Flush | Draw::Combo)
&& (0..4usize)
.filter(|s| flush_draw_suits(rules.hole_usage, census) >> s & 1 == 1)
.any(|s| holds_best_missing(rules, census, s))
}
fn holds_best_missing(rules: &Rules, census: &Census, suit: usize) -> bool {
let free = deck_mask(rules.deck) & !census.board_suit[suit];
census.hole_suit[suit] & bit(top_pip(free)) != 0
}
+48
View File
@@ -0,0 +1,48 @@
//! Outs: unseen cards of the deck (minus hole and board) after which hero's class improves.
//!
//! An unseen card counts when the class gets better and it is
//! - a straight, flush, full house, quads or straight flush (any hand), or
//! - for a hand with one pair of its own (underpair .. overpair): two pair, set or trips made with
//! a rank hero holds. A card that only pairs the board does not count.
//!
//! Whether the card also helps an opponent is not looked at. No outs on the river.
use super::census::Census;
use super::made::Made;
use super::made_class::classify;
use crate::card::{Card, Rank, Suit};
use crate::hand::showdown::judge;
use crate::variant::Rules;
pub fn outs(rules: &Rules, hole: &[Card], board: &[Card], current: Made) -> u8 {
if board.len() >= 5 {
return 0;
}
Rank::ALL
.iter()
.filter(|rank| rules.deck.contains(**rank))
.flat_map(|rank| Suit::ALL.iter().map(move |suit| Card::new(*rank, *suit)))
.filter(|card| !hole.contains(card) && !board.contains(card))
.filter(|card| improves(rules, hole, board, current, *card))
.count() as u8
}
fn improves(rules: &Rules, hole: &[Card], board: &[Card], current: Made, card: Card) -> bool {
let mut next = [card; 5];
next[..board.len()].copy_from_slice(board);
next[board.len()] = card;
let next = &next[..=board.len()];
let census = Census::of(hole, next);
judge(rules, hole, next).ok().is_some_and(|strength| {
let after = classify(rules, &census, &strength);
after > current
&& (after >= Made::Straight
|| (has_own_pair(current)
&& after >= Made::TwoPair
&& census.hole_count(card.rank.value()) > 0))
})
}
fn has_own_pair(made: Made) -> bool {
(Made::Underpair..=Made::Overpair).contains(&made)
}
+52
View File
@@ -0,0 +1,52 @@
//! Class of a single pair that hero's hole cards contribute to.
//!
//! Pocket pair: `Overpair` above every board rank, `Underpair` below every board rank, otherwise `MiddlePair`.
//! One hole card pairing a board rank, counting the distinct board ranks from the top (`n` of them):
//! the highest is the top pair; the lowest, or anything below the middle (`index > n / 2`), is the bottom pair;
//! the rest is the middle pair.
//! Top pair is `TopPairGoodKicker` when hero's other (highest non-paired) hole card is a queen or better.
use super::census::{bit, top_pip, Census};
use super::made::Made;
const QUEEN: u8 = 12;
pub fn improved_pair(census: &Census, pip: u8) -> Made {
if census.hole_count(pip) >= 2 {
pocket_pair(census, pip)
} else {
board_pair(census, pip)
}
}
fn pocket_pair(census: &Census, pip: u8) -> Made {
let board = census.board_mask;
if pip > top_pip(board) {
Made::Overpair
} else if board != 0 && pip < board.trailing_zeros() as u8 {
Made::Underpair
} else {
Made::MiddlePair
}
}
fn board_pair(census: &Census, pip: u8) -> Made {
let ranks = census.board_mask.count_ones();
let above = census.board_mask.checked_shr(pip as u32 + 1).unwrap_or(0).count_ones();
if above == 0 {
top_pair(census, pip)
} else if above + 1 == ranks || above > ranks / 2 {
Made::BottomPair
} else {
Made::MiddlePair
}
}
fn top_pair(census: &Census, pip: u8) -> Made {
let kicker = top_pip(census.hole_mask & !bit(pip));
if kicker >= QUEEN {
Made::TopPairGoodKicker
} else {
Made::TopPairWeakKicker
}
}
+61
View File
@@ -0,0 +1,61 @@
//! Straight draws from the deck's windows.
//!
//! A rank `x` is a completing rank when one more card of that rank gives hero a straight that
//! uses the hole cards: Hold'em family, the window is covered by hole + board + `x` and not by
//! board + `x` alone; Omaha family, exactly two hole ranks plus three board ranks (with `x`).
//! One completing rank is a gutshot, two or more are open-ended (a double gutshot included).
//! Backdoor (flop only): a window with three of its ranks held (hole cards contributing).
use super::census::{bit, Census};
use super::windows::windows;
use crate::variant::{HoleUsage, Rules};
/// How many different ranks complete a straight for hero.
pub fn completing_ranks(rules: &Rules, census: &Census) -> u32 {
(rules.deck.lowest.value()..=14u8)
.filter(|&pip| census.total_count(pip) < 4)
.filter(|&pip| windows(rules.deck).any(|w| completes(rules.hole_usage, census, w, bit(pip))))
.count() as u32
}
fn completes(usage: HoleUsage, census: &Census, window: u16, extra: u16) -> bool {
let (hole, board) = (census.hole_mask, census.board_mask);
let in_window = window & extra != 0;
match usage {
HoleUsage::BestFiveOfAll => {
in_window
&& window & !(hole | board | extra) == 0
&& window & !(board | extra) != 0
&& window & !(hole | board) != 0
}
HoleUsage::ExactlyTwoHole => {
let hole_in = window & hole;
let missing = window & !(board | extra);
in_window
&& hole_in.count_ones() >= 2
&& missing.count_ones() <= 2
&& missing & !hole_in == 0
}
}
}
pub fn backdoor_straight(rules: &Rules, census: &Census) -> bool {
census.board_len == 3 && windows(rules.deck).any(|w| backdoor_in(rules.hole_usage, census, w))
}
fn backdoor_in(usage: HoleUsage, census: &Census, window: u16) -> bool {
let (hole, board) = (census.hole_mask, census.board_mask);
match usage {
HoleUsage::BestFiveOfAll => {
(window & (hole | board)).count_ones() >= 3 && window & hole & !board != 0
}
HoleUsage::ExactlyTwoHole => {
let (hole_in, board_in) = (window & hole, window & board);
match hole_in.count_ones() {
0 | 1 => false,
2 => board_in & !hole_in != 0,
_ => board_in != 0,
}
}
}
}
+40 -2
View File
@@ -1,5 +1,8 @@
use super::census::Census;
use super::pairing::Pairing;
use super::suit_pattern::SuitPattern;
use super::texture_wetness::wetness;
use super::windows::windows;
use crate::card::{Card, Rank};
use crate::variant::Rules;
@@ -14,11 +17,46 @@ pub struct Texture {
/// Highest board rank, `None` without a board.
pub high: Option<Rank>,
/// 0.0 = dry (rainbow, unpaired, disconnected) … 1.0 = very wet.
/// Scoring table in `texture_wetness`.
pub wetness: f64,
}
/// Texture of `board` under the variant's deck (straight windows differ in short deck).
pub fn texture(rules: &Rules, board: &[Card]) -> Texture {
let _ = (rules, board);
todo!("read::texture")
let census = Census::of(&[], board);
let suits = suit_pattern(&census);
let pairing = pairing(&census);
let connectivity = windows(rules.deck)
.map(|w| (w & census.board_mask).count_ones() as u8)
.max()
.unwrap_or(0);
Texture {
suits,
pairing,
connectivity,
high: board.iter().map(|c| c.rank).max(),
wetness: wetness(suits, connectivity, pairing),
}
}
fn suit_pattern(census: &Census) -> SuitPattern {
match (0..4).map(|s| census.board_suited(s)).max().unwrap_or(0) {
0 | 1 => SuitPattern::Rainbow,
2 => SuitPattern::TwoTone,
3 => SuitPattern::Monotone,
_ => SuitPattern::FourFlush,
}
}
fn pairing(census: &Census) -> Pairing {
let groups = |size: u8| (2..=14u8).filter(|p| census.board_count(*p) == size).count();
let (pairs, trips, quads) = (groups(2), groups(3), groups(4));
match (quads, trips, pairs) {
(q, _, _) if q > 0 => Pairing::Heavy,
(_, t, p) if t > 0 && p > 0 => Pairing::Heavy,
(_, t, _) if t > 0 => Pairing::Trips,
(_, _, p) if p >= 2 => Pairing::TwoPaired,
(_, _, 1) => Pairing::Paired,
_ => Pairing::Unpaired,
}
}
+32
View File
@@ -0,0 +1,32 @@
//! Wetness score of a board: how many draws and strong hands it allows. Sum of three parts, capped at 1.
//!
//! | part | values |
//! |--------------|---------------------------------------------------------------------|
//! | suits | rainbow 0, two-tone 0.25, monotone 0.40, four-flush 0.50 |
//! | connectivity | <= 1: 0, 2: 0.10, 3: 0.25, 4: 0.40, 5: 0.50 |
//! | pairing | unpaired 0, paired 0.05, two pair / trips / heavy 0.10 |
use super::pairing::Pairing;
use super::suit_pattern::SuitPattern;
pub fn wetness(suits: SuitPattern, connectivity: u8, pairing: Pairing) -> f64 {
let suit_part: f64 = match suits {
SuitPattern::Rainbow => 0.0,
SuitPattern::TwoTone => 0.25,
SuitPattern::Monotone => 0.40,
SuitPattern::FourFlush => 0.50,
};
let connect_part = match connectivity {
0 | 1 => 0.0,
2 => 0.10,
3 => 0.25,
4 => 0.40,
_ => 0.50,
};
let pair_part = match pairing {
Pairing::Unpaired => 0.0,
Pairing::Paired => 0.05,
Pairing::TwoPaired | Pairing::Trips | Pairing::Heavy => 0.10,
};
(suit_part + connect_part + pair_part).min(1.0)
}
+36
View File
@@ -0,0 +1,36 @@
//! Straight windows of a deck as rank masks (bit = pip value).
//! Full deck: A-2-3-4-5 up to T-J-Q-K-A. Short deck: A-6-7-8-9 up to T-J-Q-K-A.
use super::census::bit;
use crate::variant::DeckRule;
const ACE: u8 = 14;
fn span(low: u8, high: u8) -> u16 {
(low..=high).fold(0, |mask, pip| mask | bit(pip))
}
/// Every five-rank window of the deck, ace-low window first.
pub fn windows(deck: DeckRule) -> impl Iterator<Item = u16> {
let low = deck.lowest.value();
let wheel = bit(ACE) | span(low, low + 3);
std::iter::once(wheel).chain((low + 4..=ACE).map(|high| span(high - 4, high)))
}
/// The window whose straight has the given high card (the ace-low window has high `lowest + 3`).
/// Empty (0) when no window has that high card.
pub fn window_of_high(deck: DeckRule, high: u8) -> u16 {
let low = deck.lowest.value();
if high == low + 3 {
bit(ACE) | span(low, low + 3)
} else if (low + 4..=ACE).contains(&high) {
span(high - 4, high)
} else {
0
}
}
/// All ranks of the deck as a mask.
pub fn deck_mask(deck: DeckRule) -> u16 {
span(deck.lowest.value(), ACE)
}
+187
View File
@@ -0,0 +1,187 @@
//! Draws: flush, straight (windows from the deck, ace low per deck), combo, backdoor, none.
use pokerfreak::card::Card;
use pokerfreak::read::draw::Draw;
use pokerfreak::read::hand_read::read;
use pokerfreak::variant::{rules, Variant};
fn cs(text: &str) -> Vec<Card> {
Card::parse_list(text).expect("cards")
}
fn draw(variant: Variant, hole: &str, board: &str) -> Draw {
read(&rules(variant), &cs(hole), &cs(board)).expect("read").draw
}
fn nl(hole: &str, board: &str) -> Draw {
draw(Variant::Nlhe, hole, board)
}
#[test]
fn flush_draw() {
assert_eq!(nl("Ah 5h", "Kh 9h 2c"), Draw::Flush);
assert_eq!(nl("Ah 5h", "Kh 9h 2c 7d"), Draw::Flush);
}
#[test]
fn flush_draw_with_one_hole_card_on_a_monotone_flop() {
assert_eq!(nl("Ah 5c", "Kh 9h 2h"), Draw::Flush);
}
#[test]
fn four_suited_board_cards_are_not_a_hero_draw() {
assert_eq!(nl("Ac 5d", "Kh 9h 2h 7h"), Draw::None);
}
#[test]
fn open_ended_straight_draw() {
assert_eq!(nl("9h 8c", "7d 6s 2c"), Draw::OpenEnded);
}
#[test]
fn double_gutshot_counts_as_open_ended() {
// 4 completes 3-7, 8 completes 5-9: two different ranks.
assert_eq!(nl("5h 7c", "3d 6s 9c"), Draw::OpenEnded);
}
#[test]
fn gutshot() {
assert_eq!(nl("9h 7c", "6d 5s Kc"), Draw::Gutshot);
}
#[test]
fn ace_high_end_draw_is_a_gutshot() {
assert_eq!(nl("Ah Kc", "Qd Js 2c"), Draw::Gutshot);
}
#[test]
fn ace_low_draws_in_full_deck() {
// A-2-3-4 only completes with the five.
assert_eq!(nl("Ah 2c", "3d 4s Kc"), Draw::Gutshot);
// 2-3-4-5 completes with the ace or the six.
assert_eq!(nl("2h 3c", "4d 5s Kc"), Draw::OpenEnded);
}
#[test]
fn board_straight_draw_without_hero_cards_is_no_draw() {
assert_eq!(nl("2h 2d", "6c 7s 8d 9h"), Draw::None);
}
#[test]
fn combo_flush_plus_straight() {
assert_eq!(nl("9h 8h", "7h 6c 2h"), Draw::Combo);
}
#[test]
fn combo_flush_plus_pair() {
assert_eq!(nl("Ah 9h", "9c 5h 2h"), Draw::Combo);
}
#[test]
fn backdoor_flush_on_the_flop() {
assert_eq!(nl("Ah Kh", "7h 5c 2d"), Draw::Backdoor);
}
#[test]
fn backdoor_straight_on_the_flop() {
assert_eq!(nl("9h 8c", "6d Kh 2c"), Draw::Backdoor);
}
#[test]
fn backdoor_only_exists_on_the_flop() {
assert_eq!(nl("9h 8c", "6d Kh 2c 3s"), Draw::None);
assert_eq!(nl("Ah Kh", "7h 5c 2d 9s"), Draw::None);
}
#[test]
fn no_draws_on_the_river() {
assert_eq!(nl("Ah 5h", "Kh 9h 2c 7d Jc"), Draw::None);
assert_eq!(nl("9h 8c", "7d 6s 2c Kd Kc"), Draw::None);
}
#[test]
fn made_straight_or_better_reports_no_draw() {
assert_eq!(nl("9h 8c", "7d 6s 5c"), Draw::None);
assert_eq!(nl("Ah 5h", "Kh 9h 2h"), Draw::None);
}
#[test]
fn dry_hand_has_no_draw() {
assert_eq!(nl("Ah Kd", "9c 6s 2d"), Draw::None);
}
#[test]
fn draw_order_is_weakest_first() {
assert!(Draw::None < Draw::Backdoor && Draw::Backdoor < Draw::Gutshot);
assert!(Draw::Gutshot < Draw::OpenEnded && Draw::OpenEnded < Draw::Flush && Draw::Flush < Draw::Combo);
}
// --- nut flush draw ---
fn nut(variant: Variant, hole: &str, board: &str) -> bool {
read(&rules(variant), &cs(hole), &cs(board)).expect("read").nut_draw
}
#[test]
fn nut_flush_draw_holds_the_ace_of_the_suit() {
assert!(nut(Variant::Nlhe, "Ah 5h", "Kh 9h 2c"));
}
#[test]
fn holding_the_highest_missing_card_is_the_nut_draw() {
// The ace of hearts is on the board, so the king is the nut card.
assert!(nut(Variant::Nlhe, "Kh 5h", "Ah 9h 2c"));
assert!(!nut(Variant::Nlhe, "Qh 5h", "Ah 9h 2c"));
}
#[test]
fn non_nut_flush_draw_and_no_draw() {
assert!(!nut(Variant::Nlhe, "Kh 5h", "Qh 9h 2c"));
assert!(!nut(Variant::Nlhe, "Ah Kd", "9c 6s 2d"));
}
#[test]
fn nut_draw_is_false_once_the_flush_is_made() {
assert!(!nut(Variant::Nlhe, "Ah 5h", "Kh 9h 2h"));
}
// --- Omaha-5 ---
#[test]
fn plo5_flush_draw_needs_exactly_two_hole_cards_with_two_board_cards() {
assert_eq!(draw(Variant::Plo5, "Ah 2h 3s Qc 8d", "Kh 7h 4c 9d"), Draw::Flush);
assert!(nut(Variant::Plo5, "Ah 2h 3s Qc 8d", "Kh 7h 4c 9d"));
}
#[test]
fn plo5_one_suited_hole_card_is_not_a_flush_draw() {
assert_eq!(draw(Variant::Plo5, "Ah 2c 2d 8s 8c", "Kh 7h 4h 9c"), Draw::None);
assert_ne!(draw(Variant::Plo5, "Ah 2c 2d 8s 8c", "Kh 7h 4c 9d"), Draw::Flush);
assert!(!nut(Variant::Plo5, "Ah 2c 2d 8s 8c", "Kh 7h 4h 9c"));
}
#[test]
fn plo5_straight_draw_needs_two_hole_ranks() {
// Hole 8 9 with board 6 7: the 5 or the T completes, each with exactly two hole cards.
assert_eq!(draw(Variant::Plo5, "8h 9d Ac Ad 2s", "6c 7s Kd"), Draw::OpenEnded);
// Only one hole rank in each window on this turn board: no draw.
assert_eq!(draw(Variant::Plo5, "8h 2d 2c Ad Ks", "6c 7s Qd 9h"), Draw::None);
}
// --- Short deck ---
#[test]
fn short_deck_ace_low_gutshot() {
// A-6-7-8-9 is the lowest straight: A 6 7 9 needs the 8.
assert_eq!(draw(Variant::ShortDeck, "As 9d", "6c 7h Kd"), Draw::Gutshot);
}
#[test]
fn short_deck_wheel_window_supplies_the_second_end() {
// 9 8 7 6: the T completes 6-T, the ace completes A-6-7-8-9 (no five in this deck).
assert_eq!(draw(Variant::ShortDeck, "9h 8c", "7d 6s Kc"), Draw::OpenEnded);
}
#[test]
fn short_deck_flush_draw() {
assert_eq!(draw(Variant::ShortDeck, "Ah Kh", "Qh 9h 7c"), Draw::Flush);
}
+119
View File
@@ -0,0 +1,119 @@
//! Invalid input becomes a `Fault`, never a panic. Same checks for `read` and `glance`.
use pokerfreak::card::Card;
use pokerfreak::fault::Fault;
use pokerfreak::read::glance::glance;
use pokerfreak::read::hand_read::read;
use pokerfreak::variant::{rules, Variant};
fn cs(text: &str) -> Vec<Card> {
Card::parse_list(text).expect("cards")
}
fn both(variant: Variant, hole: &str, board: &str) -> (Result<(), Fault>, Result<(), Fault>) {
let r = rules(variant);
(
read(&r, &cs(hole), &cs(board)).map(|_| ()),
glance(&r, &cs(hole), &cs(board)).map(|_| ()),
)
}
fn invalid(result: &Result<(), Fault>) -> bool {
matches!(result, Err(Fault::Invalid { .. }))
}
#[test]
fn board_with_two_cards_is_invalid() {
let (a, b) = both(Variant::Nlhe, "Ah Kd", "7c 2d");
assert!(invalid(&a) && invalid(&b));
}
#[test]
fn empty_board_is_invalid() {
let (a, b) = both(Variant::Nlhe, "Ah Kd", "");
assert!(invalid(&a) && invalid(&b));
}
#[test]
fn board_with_six_cards_is_invalid() {
let (a, b) = both(Variant::Nlhe, "Ah Kd", "7c 2d 3s 4h 9c Td");
assert!(invalid(&a) && invalid(&b));
}
#[test]
fn holdem_with_one_or_three_hole_cards_is_invalid() {
for hole in ["Ah", "Ah Kd Qs", ""] {
let (a, b) = both(Variant::Nlhe, hole, "7c 2d 3s");
assert!(invalid(&a) && invalid(&b), "{hole}");
}
}
#[test]
fn plo5_needs_exactly_five_hole_cards() {
for hole in ["Ah Kd Qs Jc", "Ah Kd Qs Jc Th 9h"] {
let (a, b) = both(Variant::Plo5, hole, "7c 2d 3s");
assert!(invalid(&a) && invalid(&b), "{hole}");
}
}
#[test]
fn holdem_hand_in_plo5_is_invalid() {
let (a, b) = both(Variant::Plo5, "Ah Kd", "7c 2d 3s");
assert!(invalid(&a) && invalid(&b));
}
#[test]
fn duplicate_between_hole_and_board_is_inconsistent() {
let (a, b) = both(Variant::Nlhe, "Ah Kd", "Ah 2d 3s");
assert!(matches!(a, Err(Fault::Inconsistent(_))));
assert!(matches!(b, Err(Fault::Inconsistent(_))));
}
#[test]
fn duplicate_inside_hole_or_board_is_inconsistent() {
let (a, b) = both(Variant::Nlhe, "Ah Ah", "7c 2d 3s");
assert!(matches!(a, Err(Fault::Inconsistent(_))) && matches!(b, Err(Fault::Inconsistent(_))));
let (a, b) = both(Variant::Nlhe, "Ah Kd", "7c 7c 3s");
assert!(matches!(a, Err(Fault::Inconsistent(_))) && matches!(b, Err(Fault::Inconsistent(_))));
}
#[test]
fn card_below_the_short_deck_is_invalid() {
let (a, b) = both(Variant::ShortDeck, "Ah 5d", "7c Kd 9s");
assert!(invalid(&a) && invalid(&b));
let (a, b) = both(Variant::ShortDeck, "Ah Kd", "7c 2d 9s");
assert!(invalid(&a) && invalid(&b));
}
#[test]
fn six_is_the_lowest_short_deck_card() {
let (a, b) = both(Variant::ShortDeck, "Ah 6d", "7c Kd 9s");
assert!(a.is_ok() && b.is_ok());
}
#[test]
fn valid_inputs_pass_in_every_variant() {
assert!(both(Variant::Nlhe, "Ah Kd", "7c 2d 3s").0.is_ok());
assert!(both(Variant::Plo5, "Ah Kd Qs Jc 9h", "7c 2d 3s 4h 8d").0.is_ok());
assert!(both(Variant::KPoker, "Ah Kd", "7c 2d 3s 4h").0.is_ok());
assert!(both(Variant::Aof, "Ah Kd", "7c 2d 3s").0.is_ok());
}
#[test]
fn arbitrary_card_lists_never_panic() {
use pokerfreak::card::deck;
use pokerfreak::rng::Rng;
let mut rng = Rng::seeded(99);
for variant in Variant::ALL {
let r = rules(variant);
for i in 0..300usize {
let (hole_len, board_len) = (i % 8, (i / 8) % 8);
// Duplicates and short-deck-foreign cards are possible on purpose: draw from the full deck twice.
let (cards, next) = deck::draw(&deck::full(), hole_len + board_len, rng).expect("draw");
rng = next;
let (hole, board) = cards.split_at(hole_len);
let a = read(&r, hole, board);
let b = glance(&r, hole, board);
assert_eq!(a.is_ok(), b.is_ok(), "{variant:?} {hole:?} {board:?}");
}
}
}
+93
View File
@@ -0,0 +1,93 @@
//! `glance` is the fast path of `read`: same made class and draw. Property over seeded random hands.
use pokerfreak::card::{deck, Card};
use pokerfreak::read::glance::glance;
use pokerfreak::read::hand_read::read;
use pokerfreak::read::draw::Draw;
use pokerfreak::rng::Rng;
use pokerfreak::variant::{rules, Rules, Variant};
fn cs(text: &str) -> Vec<Card> {
Card::parse_list(text).expect("cards")
}
fn pool(rules: &Rules) -> Vec<Card> {
deck::full().into_iter().filter(|c| rules.deck.contains(c.rank)).collect()
}
/// A random hole + board for the variant, drawn only from the variant's deck.
fn deal(rules: &Rules, board_len: usize, rng: Rng) -> (Vec<Card>, Vec<Card>, Rng) {
let (cards, rng) = deck::draw(&pool(rules), rules.hole_cards + board_len, rng).expect("pool");
let (hole, board) = cards.split_at(rules.hole_cards);
(hole.to_vec(), board.to_vec(), rng)
}
#[test]
fn glance_equals_made_and_draw_of_read_in_every_variant() {
for variant in Variant::ALL {
let r = rules(variant);
let hands = if r.hole_cards > 2 { 150 } else { 600 };
let mut rng = Rng::seeded(0xC0FFEE ^ variant as u64);
for i in 0..hands {
let board_len = 3 + i % 3;
let (hole, board, next) = deal(&r, board_len, rng);
rng = next;
let full = read(&r, &hole, &board).expect("read");
let quick = glance(&r, &hole, &board).expect("glance");
assert_eq!(quick, (full.made, full.draw), "{variant:?} {hole:?} / {board:?}");
}
}
}
#[test]
fn read_invariants_over_random_hands() {
for variant in Variant::ALL {
let r = rules(variant);
let hands = if r.hole_cards > 2 { 100 } else { 400 };
let mut rng = Rng::seeded(77 + variant as u64);
for i in 0..hands {
let board_len = 3 + i % 3;
let (hole, board, next) = deal(&r, board_len, rng);
rng = next;
let read = read(&r, &hole, &board).expect("read");
let unseen = r.deck.size() - hole.len() - board.len();
assert!(read.outs as usize <= unseen);
assert!((0.0..=1.0).contains(&read.texture.wetness));
if board_len == 5 {
assert_eq!(read.outs, 0);
assert_eq!(read.draw, Draw::None);
}
if read.nut_draw {
assert!(matches!(read.draw, Draw::Flush | Draw::Combo), "{:?}", read.draw);
}
}
}
}
#[test]
fn glance_agrees_with_read_on_concrete_hands() {
let r = rules(Variant::Nlhe);
for (hole, board) in [("Ah Kd", "Ks 7c 2d"), ("9h 8h", "7h 6c 2h"), ("2h 2d", "6c 7s 8d 9h")] {
let (h, b) = (cs(hole), cs(board));
let full = read(&r, &h, &b).expect("read");
assert_eq!(glance(&r, &h, &b).expect("glance"), (full.made, full.draw));
}
}
/// Smoke test only: 20 000 glances must finish within a generous bound.
#[test]
fn glance_is_cheap_enough_for_monte_carlo_loops() {
let r = rules(Variant::Nlhe);
let cards = pool(&r);
let start = std::time::Instant::now();
let mut rng = Rng::seeded(5);
let mut checksum = 0usize;
for i in 0..20_000 {
let (picked, next) = deck::draw(&cards, 2 + 3 + i % 3, rng).expect("pool");
rng = next;
let (hole, board) = picked.split_at(2);
let (made, draw) = glance(&r, hole, board).expect("glance");
checksum += made as usize + draw as usize;
}
assert!(checksum > 0);
assert!(start.elapsed().as_secs() < 20, "took {:?}", start.elapsed());
}
+144
View File
@@ -0,0 +1,144 @@
//! Made-hand classes relative to the board. Kicker rule: top pair is "good" with a kicker of queen or better.
use pokerfreak::card::Card;
use pokerfreak::read::hand_read::read;
use pokerfreak::read::made::Made;
use pokerfreak::variant::{rules, Variant};
fn cs(text: &str) -> Vec<Card> {
Card::parse_list(text).expect("cards")
}
fn made(variant: Variant, hole: &str, board: &str) -> Made {
read(&rules(variant), &cs(hole), &cs(board)).expect("read").made
}
fn nl(hole: &str, board: &str, expected: Made) {
assert_eq!(made(Variant::Nlhe, hole, board), expected, "{hole} on {board}");
}
#[test]
fn air_has_no_pair() {
nl("Ah Kd", "9c 6s 2d", Made::Air);
nl("Ah Kd", "9c 6s 2d 4h 3c", Made::Air);
}
#[test]
fn board_only_pair_trips_and_two_pair() {
nl("Ah Kd", "9c 9s 4d", Made::BoardOnly);
nl("Ah Kd", "7c 7s 7d", Made::BoardOnly);
nl("Ah 4d", "Kc Ks 7d 7s 3h", Made::BoardOnly);
nl("Ah Kd", "7c 7d 7h 7s 2d", Made::BoardOnly);
}
#[test]
fn board_only_straight_and_flush() {
nl("2c 2d", "Tc Jd Qh Ks Ac", Made::BoardOnly);
nl("2h 3d", "Ah Kh Qh 9h 4h", Made::BoardOnly);
}
#[test]
fn hero_flush_card_above_the_board_flush_improves_it() {
nl("Jh 3d", "Ah Kh Qh 9h 4h", Made::Flush);
}
#[test]
fn pocket_pairs_against_the_board() {
nl("Qc Qd", "Jh 8s 2c", Made::Overpair);
nl("3c 3d", "Kh 9s 5c", Made::Underpair);
nl("7c 7d", "Kh 9s 5c", Made::MiddlePair);
}
#[test]
fn top_pair_kicker_split_at_queen() {
nl("Ah Kd", "Ks 7c 2d", Made::TopPairGoodKicker);
nl("Kh Qd", "Ks 7c 2d", Made::TopPairGoodKicker);
nl("Kh Jd", "Ks 7c 2d", Made::TopPairWeakKicker);
nl("Kh 4d", "Ks 7c 2d", Made::TopPairWeakKicker);
}
#[test]
fn middle_and_bottom_pair_on_the_flop() {
nl("9h 4d", "Ks 9c 2d", Made::MiddlePair);
nl("Ah 2d", "Ks 9c 2c", Made::BottomPair);
}
#[test]
fn middle_and_bottom_pair_on_four_cards() {
nl("9h Ad", "Ks 9c 6d 4c", Made::MiddlePair);
nl("6h Ad", "Ks 9c 6d 4c", Made::MiddlePair);
nl("4h Ad", "Ks 9c 6d 4c", Made::BottomPair);
}
#[test]
fn two_pair_must_use_a_hole_card() {
nl("Ah 7d", "Ac 7s 2d", Made::TwoPair);
nl("Qh 3d", "Qc 8d 8s", Made::TwoPair);
nl("9h 9d", "Kc Ks 2d", Made::TwoPair);
}
#[test]
fn set_versus_trips() {
nl("7c 7d", "7h Ks 2d", Made::Set);
nl("7c Ad", "7h 7s 2d", Made::Trips);
}
#[test]
fn straight_flush_full_house_quads() {
nl("9h 8d", "7c 6s 5d", Made::Straight);
nl("Ah 2h", "Kh 7h 9c 4h", Made::Flush);
nl("Kh 7d", "7c 7s Kd", Made::FullHouse);
nl("Kh Kd", "Kc 7s 7d", Made::FullHouse);
nl("7c 7d", "7h 7s 2d", Made::Quads);
nl("7c Ad", "7h 7s 7d", Made::Quads);
nl("9h 8h", "7h 6h 5h", Made::StraightFlush);
}
#[test]
fn wheel_straight_in_full_deck() {
nl("Ah 2d", "3c 4s 5d", Made::Straight);
}
#[test]
fn classes_are_ordered_weakest_first() {
assert!(Made::Air < Made::BoardOnly && Made::BoardOnly < Made::Underpair);
assert!(Made::TopPairGoodKicker < Made::Overpair && Made::Overpair < Made::TwoPair);
assert!(Made::Set < Made::Straight && Made::FullHouse < Made::StraightFlush);
}
// --- Omaha-5: exactly two hole cards and three board cards ---
#[test]
fn plo5_board_flush_is_not_playable_with_one_suited_hole_card() {
let m = made(Variant::Plo5, "Ah 2c 3d 8s 9c", "Kh 7h 4h Qh 5d");
assert_ne!(m, Made::Flush);
}
#[test]
fn plo5_flush_needs_two_suited_hole_cards() {
assert_eq!(made(Variant::Plo5, "Ah 2h 3d 8s 9c", "Kh 7h 4h 6c 5d"), Made::Flush);
}
#[test]
fn plo5_classifies_from_best_two_plus_three() {
assert_eq!(made(Variant::Plo5, "Ah Ad 3d 8s 9c", "Ks 7h 4h"), Made::Overpair);
assert_eq!(made(Variant::Plo5, "Kh Qd 3d 8s 9c", "Ks 7h 4h"), Made::TopPairGoodKicker);
assert_eq!(made(Variant::Plo5, "7c 7d 3d 8s 9c", "7h Ks 2d"), Made::Set);
assert_eq!(made(Variant::Plo5, "7c Ad 3d 8s 9c", "7h 7s 2d"), Made::Trips);
}
#[test]
fn plo5_straight_uses_exactly_two_hole_cards() {
assert_eq!(made(Variant::Plo5, "9h 8d 2d 3s Kc", "7c 6s 5d"), Made::Straight);
}
#[test]
fn plo5_board_straight_alone_is_not_a_straight_for_hero() {
let m = made(Variant::Plo5, "Ac Ad 2d 2s Kc", "5c 6s 7d 8h 9c");
assert_ne!(m, Made::Straight);
}
#[test]
fn short_deck_pair_classes_follow_the_same_rules() {
assert_eq!(made(Variant::ShortDeck, "Ah Kd", "Ks 7c 8d"), Made::TopPairGoodKicker);
assert_eq!(made(Variant::ShortDeck, "Ah Ad", "Ks 7c 8d"), Made::Overpair);
}
+105
View File
@@ -0,0 +1,105 @@
//! Outs: unseen cards (deck of the variant minus hole and board) after which hero's class improves.
//! Counted: Straight or better for any hand; for one-pair hands also two pair / trips / set
//! made with a rank hero holds. Board-only improvements and cards that merely pair the board do not count.
use pokerfreak::card::Card;
use pokerfreak::read::hand_read::read;
use pokerfreak::variant::{rules, Variant};
fn cs(text: &str) -> Vec<Card> {
Card::parse_list(text).expect("cards")
}
fn outs(variant: Variant, hole: &str, board: &str) -> u8 {
read(&rules(variant), &cs(hole), &cs(board)).expect("read").outs
}
fn nl(hole: &str, board: &str) -> u8 {
outs(Variant::Nlhe, hole, board)
}
#[test]
fn flush_draw_has_nine_outs_in_a_full_deck() {
assert_eq!(nl("Ah Kh", "2h 7h Qc"), 9);
}
#[test]
fn flush_draw_has_five_outs_in_short_deck() {
// 9 cards per suit, 4 seen. 31 cards unseen instead of 47.
assert_eq!(outs(Variant::ShortDeck, "Ah Kh", "Qh 9h 7c"), 5);
assert_eq!(outs(Variant::Nlhe, "Ah Kh", "Qh 9h 7c"), 9);
}
#[test]
fn open_ended_straight_draw_has_eight_outs() {
assert_eq!(nl("9h 8c", "7d 6s 2c"), 8);
}
#[test]
fn short_deck_open_ender_has_eight_outs_among_fewer_cards() {
// Ts and Js complete 7-J / 6-T? Here the 6 and the J complete 6-T and 7-J; 8 outs of 31 unseen cards.
assert_eq!(outs(Variant::ShortDeck, "9h 8c", "7d Ts Kc"), 8);
}
#[test]
fn gutshot_has_four_outs() {
assert_eq!(nl("9h 7c", "6d 5s Kc"), 4);
}
#[test]
fn double_gutshot_has_eight_outs() {
assert_eq!(nl("5h 7c", "3d 6s 9c"), 8);
}
#[test]
fn flush_plus_open_ender_has_fifteen_outs() {
// 9 hearts + three non-heart tens + three non-heart fives.
assert_eq!(nl("9h 8h", "7h 6h 2c"), 15);
}
#[test]
fn top_pair_good_kicker_has_five_outs() {
// Three aces (two pair) and two kings (trips). Pairing the seven or deuce does not count.
assert_eq!(nl("Ah Kd", "Ks 7c 2d"), 5);
}
#[test]
fn underpair_has_two_outs_to_a_set() {
assert_eq!(nl("3c 3d", "Kh 9s 5c"), 2);
}
#[test]
fn board_completed_straight_is_not_an_out() {
// Fives and tens give the board a straight; hero's deuces add nothing. Only the two deuces make a set.
assert_eq!(nl("2h 2d", "6c 7s 8d 9h"), 2);
}
#[test]
fn river_has_no_outs() {
assert_eq!(nl("Ah 5h", "Kh 9h 2c 7d Jc"), 0);
assert_eq!(nl("9h 8c", "7d 6s 2c Kd Kc"), 0);
}
#[test]
fn made_flush_cannot_be_improved_by_a_plain_flush_card() {
// Ace-high flush made: only full house, quads or straight flush improve it.
let o = nl("Ah 5h", "Kh 9h 2h");
assert!(o <= 12, "{o}");
}
#[test]
fn air_without_draw_has_no_outs() {
assert_eq!(nl("Ah Kd", "9c 6s 2d"), 0);
}
#[test]
fn plo5_nut_flush_draw_outs() {
// Ah 2h in hand (no pair, no straight draw), Kh 7h on the board: nine unseen hearts (no other improvement for this hand).
assert_eq!(outs(Variant::Plo5, "Ah 2h 3s Qc 8d", "Kh 7h 4c 9d"), 9);
}
#[test]
fn outs_never_exceed_the_unseen_cards() {
for (hole, board) in [("Ah Kh", "2h 7h Qc"), ("9h 8h", "7h 6h 2c"), ("7c 7d", "7h Ks 2d")] {
assert!(nl(hole, board) <= 47);
}
}
+145
View File
@@ -0,0 +1,145 @@
//! Board texture: suit pattern, pairing, connectivity (deck-dependent windows), high card, wetness.
use pokerfreak::card::{Card, Rank};
use pokerfreak::read::pairing::Pairing;
use pokerfreak::read::suit_pattern::SuitPattern;
use pokerfreak::read::texture::{texture, Texture};
use pokerfreak::variant::{rules, Variant};
fn cs(text: &str) -> Vec<Card> {
Card::parse_list(text).expect("cards")
}
fn full(board: &str) -> Texture {
texture(&rules(Variant::Nlhe), &cs(board))
}
fn short(board: &str) -> Texture {
texture(&rules(Variant::ShortDeck), &cs(board))
}
#[test]
fn empty_board_is_the_neutral_texture() {
let t = full("");
assert_eq!(t.suits, SuitPattern::Rainbow);
assert_eq!(t.pairing, Pairing::Unpaired);
assert_eq!(t.connectivity, 0);
assert_eq!(t.high, None);
assert_eq!(t.wetness, 0.0);
}
#[test]
fn dry_ace_seven_deuce_rainbow() {
let t = full("Ah 7c 2d");
assert_eq!(t.suits, SuitPattern::Rainbow);
assert_eq!(t.pairing, Pairing::Unpaired);
assert_eq!(t.high, Some(Rank::Ace));
// Only A-2 share a window (the wheel): 2 ranks.
assert_eq!(t.connectivity, 2);
assert!(t.wetness < 0.15, "wetness {}", t.wetness);
}
#[test]
fn king_seven_deuce_is_completely_dry() {
let t = full("Ks 7d 2c");
assert_eq!(t.connectivity, 1);
assert_eq!(t.wetness, 0.0);
}
#[test]
fn monotone_board() {
let t = full("Ah 7h 2h");
assert_eq!(t.suits, SuitPattern::Monotone);
assert!(t.wetness >= 0.4, "wetness {}", t.wetness);
}
#[test]
fn two_tone_board() {
assert_eq!(full("Ah 7h 2c").suits, SuitPattern::TwoTone);
}
#[test]
fn four_flush_on_the_turn() {
let t = full("Ah Kh 7h 2h");
assert_eq!(t.suits, SuitPattern::FourFlush);
assert!(t.wetness >= 0.5, "wetness {}", t.wetness);
}
#[test]
fn two_two_suit_split_is_two_tone() {
assert_eq!(full("Ah Kh 7c 2c").suits, SuitPattern::TwoTone);
}
#[test]
fn three_of_a_suit_on_four_cards_is_monotone() {
assert_eq!(full("Ah Kh 7h 2c").suits, SuitPattern::Monotone);
}
#[test]
fn connected_two_tone_jack_ten_nine() {
let t = full("Jh Th 9c");
assert_eq!(t.suits, SuitPattern::TwoTone);
assert_eq!(t.connectivity, 3);
assert_eq!(t.high, Some(Rank::Jack));
assert!(t.wetness >= 0.45, "wetness {}", t.wetness);
}
#[test]
fn connectivity_counts_ace_low_window_in_full_deck() {
assert_eq!(full("Ac 2d 3h 4s").connectivity, 4);
}
#[test]
fn broadway_board_has_five_connected_ranks() {
assert_eq!(full("Ac Kd Qh Js Th").connectivity, 5);
}
#[test]
fn short_deck_ace_low_window_is_ace_six_seven_eight_nine() {
assert_eq!(short("As 6d 7c").connectivity, 3);
// Same cards in the full deck: A is far from 6-7, only 6-7 share a window.
assert_eq!(full("As 6d 7c").connectivity, 2);
}
#[test]
fn short_deck_full_wheel_board() {
assert_eq!(short("As 6d 7c 8h 9s").connectivity, 5);
}
#[test]
fn pairing_levels() {
assert_eq!(full("Ks 7d 2c").pairing, Pairing::Unpaired);
assert_eq!(full("Ks Kd 7c").pairing, Pairing::Paired);
assert_eq!(full("7c 7d 2h 2s").pairing, Pairing::TwoPaired);
assert_eq!(full("7c 7d 7h").pairing, Pairing::Trips);
assert_eq!(full("7c 7d 7h 7s").pairing, Pairing::Heavy);
assert_eq!(full("7c 7d 7h 2s 2c").pairing, Pairing::Heavy);
}
#[test]
fn paired_board_is_a_little_wetter_than_its_unpaired_twin() {
let paired = full("Ks Kd 7c");
let unpaired = full("Ks 7d 2c");
assert!(paired.wetness > unpaired.wetness);
assert!(paired.wetness < 0.2);
}
#[test]
fn flush_texture_ordering() {
let rainbow = full("Qh 8c 3d").wetness;
let two_tone = full("Qh 8h 3d").wetness;
let monotone = full("Qh 8h 3h").wetness;
assert!(rainbow < two_tone && two_tone < monotone);
}
#[test]
fn connected_is_wetter_than_disconnected() {
assert!(full("8c 7d 6h").wetness > full("Kc 7d 2h").wetness);
}
#[test]
fn wetness_stays_in_unit_interval_on_extreme_boards() {
for board in ["Ah Kh Qh Jh Th", "7c 7d 7h 7s 2d", "Ac 2c 3c 4c 5c", "Ah Kh Qh Jh"] {
let w = full(board).wetness;
assert!((0.0..=1.0).contains(&w), "{board}: {w}");
}
}