45 lines
2.0 KiB
Rust
45 lines
2.0 KiB
Rust
//! 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)
|
|
}
|