use crate::{
deck::{
Card, Deck, Rank, Suit,
range::{Range, RangeBase, RangeItem},
},
error::DucyError,
games::{CardDealer, holdem::RangeTextError},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Condition {
TripleSuited,
DoubleSuited,
SingleSuited,
Rainbow,
NoPair,
OnePair,
TwoPair,
Rundown {
len: Option<usize>,
min_gaps: usize,
max_gaps: usize,
},
}
const MAX_SPAN: usize = 12;
impl Condition {
fn parse(s: &str, cards_per_player: usize) -> Result<Self, DucyError> {
match s {
"ts" => return Ok(Self::TripleSuited),
"ds" => return Ok(Self::DoubleSuited),
"ss" => return Ok(Self::SingleSuited),
"r" => return Ok(Self::Rainbow),
"np" => return Ok(Self::NoPair),
"1p" => return Ok(Self::OnePair),
"2p" => return Ok(Self::TwoPair),
_ => {}
}
let (len, gaps) = s.split_once("rd").ok_or(DucyError::InvalidRange)?;
let number = |n: &str| n.parse::<usize>().map_err(|_| DucyError::InvalidRange);
let len = if len.is_empty() {
None
} else {
Some(number(len)?)
};
let (min_gaps, max_gaps) = match gaps.split_once('-') {
_ if gaps.is_empty() => (0, 0),
Some((lo, hi)) => (number(lo)?, number(hi)?),
None => (number(gaps)?, number(gaps)?),
};
let cards = len.unwrap_or(cards_per_player);
if !(2..=cards_per_player).contains(&cards)
|| min_gaps > max_gaps
|| cards - 1 + min_gaps > MAX_SPAN
{
return Err(DucyError::InvalidRange);
}
Ok(Self::Rundown {
len,
min_gaps,
max_gaps,
})
}
fn matches(self, ranks: &[u8; 13], suits: &[u8; 4]) -> bool {
let suited = suits.iter().filter(|&&c| c >= 2).count();
let pairs = ranks.iter().filter(|&&c| c == 2).count();
let trips = ranks.iter().any(|&c| c >= 3);
match self {
Self::TripleSuited => suited >= 3,
Self::DoubleSuited => suited == 2,
Self::SingleSuited => suited == 1,
Self::Rainbow => suited == 0,
Self::NoPair => pairs == 0 && !trips,
Self::OnePair => pairs == 1 && !trips,
Self::TwoPair => pairs >= 2,
Self::Rundown {
len,
min_gaps,
max_gaps,
} => {
let cards: usize = ranks.iter().map(|&c| c as usize).sum();
match len {
None => {
ranks.iter().all(|&c| c <= 1)
&& (min_gaps..=max_gaps).any(|g| has_run(ranks, cards, g))
}
Some(len) => (min_gaps..=max_gaps).any(|g| has_run(ranks, len, g)),
}
}
}
}
}
fn has_run(ranks: &[u8; 13], len: usize, gaps: usize) -> bool {
let span = len - 1 + gaps;
if span > MAX_SPAN {
return false;
}
let mut bits = 0u16;
for (i, &c) in ranks.iter().enumerate() {
if c > 0 {
bits |= 1 << (i + 1);
}
}
if bits & (1 << 13) != 0 {
bits |= 1;
}
(0..=13 - span).any(|low| {
let high = low + span;
let inside = (bits >> (low + 1)) & ((1u16 << (span - 1)) - 1);
bits & (1 << low) != 0 && bits & (1 << high) != 0 && inside.count_ones() as usize + 2 >= len
})
}
#[derive(Clone, Copy, Debug)]
struct Qualifier {
negate: bool,
condition: Condition,
}
impl Qualifier {
fn parse(s: &str, cards_per_player: usize) -> Result<Self, DucyError> {
let (negate, rest) = match s.strip_prefix('!') {
Some(rest) => (true, rest),
None => (false, s),
};
Ok(Self {
negate,
condition: Condition::parse(rest, cards_per_player)?,
})
}
fn matches(self, ranks: &[u8; 13], suits: &[u8; 4]) -> bool {
self.condition.matches(ranks, suits) != self.negate
}
}
pub(crate) struct Term {
pub(crate) cards: Deck,
pub(crate) ranks: [u8; 13],
qualifiers: Vec<Qualifier>,
}
impl Term {
pub(crate) fn parse(term: &str, cards_per_player: usize) -> Result<Self, DucyError> {
let mut parts = term.split('$');
let body: Vec<char> = parts.next().unwrap_or_default().chars().collect();
let qualifiers: Vec<Qualifier> = parts
.map(|q| Qualifier::parse(q, cards_per_player))
.collect::<Result<_, _>>()?;
let mut cards = Deck::empty();
let mut ranks = [0u8; 13];
let mut tokens = 0;
let mut i = 0;
while i < body.len() {
let c = body[i];
if c == 'x' || c == '*' {
i += 1;
} else {
let rank = Rank::try_from_char(&c)?;
match body
.get(i + 1)
.filter(|s| s.is_ascii_lowercase() && **s != 'x')
{
Some(s) => {
let card = Card::new(rank, Suit::try_from_char(s)?);
if cards.has_card(&card) {
return Err(DucyError::InvalidRange);
}
cards |= card;
i += 2;
}
None => {
ranks[rank as usize] += 1;
i += 1;
}
}
}
tokens += 1;
}
if (tokens == 0 && qualifiers.is_empty()) || tokens > cards_per_player {
return Err(DucyError::InvalidRange);
}
Ok(Self {
cards,
ranks,
qualifiers,
})
}
pub(crate) fn matches(&self, hand: Deck) -> bool {
if !hand.has_cards(&self.cards) {
return false;
}
let mut ranks = [0u8; 13];
let mut suits = [0u8; 4];
let mut rest_ranks = [0u8; 13];
for card in hand.iter(false) {
ranks[card.rank() as usize] += 1;
suits[card.suit() as usize] += 1;
if !self.cards.has_card(&card) {
rest_ranks[card.rank() as usize] += 1;
}
}
rest_ranks
.iter()
.zip(&self.ranks)
.all(|(have, need)| have >= need)
&& self.qualifiers.iter().all(|q| q.matches(&ranks, &suits))
}
}
pub(crate) fn total_hands(k: usize) -> u64 {
(0..k as u64).fold(1, |acc, i| acc * (52 - i) / (i + 1))
}
#[derive(Clone)]
pub struct OmahaRange {
cards_per_player: usize,
range_base: RangeBase,
}
impl OmahaRange {
pub fn new(cards_per_player: usize) -> Self {
Self {
cards_per_player,
range_base: RangeBase::new(),
}
}
pub fn parse(ranges: &str, cards_per_player: usize) -> Result<Self, DucyError> {
let mut range = Self::new(cards_per_player);
for part in ranges
.split([',', ' ', '\t', '\n'])
.filter(|p| !p.is_empty())
{
range.add(part, 1.0)?;
}
Ok(range)
}
pub fn add(&mut self, term: &str, weight: f64) -> Result<(), DucyError> {
if !weight.is_finite() {
return Err(DucyError::InvalidRange);
}
let term = Term::parse(term.trim(), self.cards_per_player)?;
for hand in Deck::all_cards().enumerate_combinations(self.cards_per_player) {
if term.matches(hand) {
self.range_base.add_deck_weight(hand, weight);
}
}
Ok(())
}
pub fn cards_per_player(&self) -> usize {
self.cards_per_player
}
pub fn combos(&self) -> u64 {
self.iter().filter(|item| item.get_weight() > 0.0).count() as u64
}
pub fn total_hands(&self) -> u64 {
total_hands(self.cards_per_player)
}
pub fn coverage(&self) -> f64 {
self.combos() as f64 / self.total_hands() as f64
}
pub fn weighted_coverage(&self) -> f64 {
let total: f64 = self.iter().map(|item| item.get_weight()).sum();
total / self.total_hands() as f64
}
pub fn report(text: &str, cards_per_player: usize) -> Result<OmahaRangeReport, RangeTextError> {
let mut range = Self::new(cards_per_player);
for (start, term) in crate::deck::range::terms(text) {
range.add(term, 1.0).map_err(|_| RangeTextError {
term: term.to_string(),
start,
message: format!(
"“{term}” isn’t an Omaha range term (e.g. AAxx$ds, $rd0-1, KK$ss)."
),
})?;
}
Ok(OmahaRangeReport {
combos: range.combos(),
total: range.total_hands(),
coverage: range.coverage(),
})
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct OmahaRangeReport {
pub combos: u64,
pub total: u64,
pub coverage: f64,
}
pub(crate) struct RangeSampler {
combos: Vec<Deck>,
cumulative: Vec<f64>,
}
impl RangeSampler {
pub(crate) fn new(range: &OmahaRange, available: Deck) -> Result<Self, DucyError> {
let mut combos: Vec<(Deck, f64)> = range
.iter()
.map(|item| (item.get_deck(), item.get_weight()))
.filter(|&(deck, w)| w > 0.0 && available.has_cards(&deck))
.collect();
if combos.is_empty() {
return Err(DucyError::InvalidRange);
}
combos.sort_unstable_by_key(|&(deck, _)| u64::from(deck));
let cumulative = combos
.iter()
.scan(0.0, |total, &(_, w)| {
*total += w;
Some(*total)
})
.collect();
Ok(Self {
combos: combos.into_iter().map(|(deck, _)| deck).collect(),
cumulative,
})
}
pub(crate) fn draw(&self, dealer: &mut CardDealer) -> Deck {
let total = self.cumulative[self.cumulative.len() - 1];
let x = dealer.random_f64(total);
let i = self
.cumulative
.partition_point(|&c| c <= x)
.min(self.combos.len() - 1);
self.combos[i]
}
}
impl Range for OmahaRange {
fn iter(&self) -> impl Iterator<Item = RangeItem> {
self.range_base.iter()
}
}
#[cfg(test)]
mod test {
use super::*;
fn count(range: &str) -> u64 {
OmahaRange::parse(range, 4).unwrap().combos()
}
#[test]
fn test_total_hands() {
assert_eq!(total_hands(4), 270_725);
assert_eq!(total_hands(5), 2_598_960);
assert_eq!(count("xxxx"), 270_725);
assert_eq!(count("$ds"), count("xxxx$ds"));
}
#[test]
fn test_report() {
let r = OmahaRange::report("AAKK, AAAA", 4).unwrap();
assert_eq!((r.combos, r.total), (37, 270_725));
assert!((r.coverage - 37.0 / 270_725.0).abs() < 1e-12);
let e = OmahaRange::report("AAxx, $nonsense", 4).unwrap_err();
assert_eq!((e.term.as_str(), e.start), ("$nonsense", 6));
assert!(e.message.contains("$nonsense"));
}
#[test]
fn test_rank_counts() {
assert_eq!(count("AA"), 6 * 1128 + 4 * 48 + 1);
assert_eq!(count("AAAA"), 1);
assert_eq!(count("AAKK"), 36);
assert_eq!(count("AAKK$ds"), 6);
}
#[test]
fn test_suit_classes_partition() {
let (ds, ss, r) = (count("$ds"), count("$ss"), count("$r"));
assert_eq!(ds + ss + r, 270_725);
assert_eq!(ds, 6 * 78 * 78);
assert_eq!(r, 13u64.pow(4));
assert_eq!(count("$ds, $ss, $r"), 270_725);
}
#[test]
fn test_pair_classes() {
assert_eq!(count("$np"), 715 * 256);
assert_eq!(count("$2p"), 78 * 36);
assert_eq!(count("$2p$np"), 0);
assert_eq!(count("$np") + count("$1p") + count("$2p"), {
270_725 - (13 * 4 * 48 + 13)
});
}
#[test]
fn test_specific_cards() {
assert_eq!(count("AsKs"), 50 * 49 / 2);
assert_eq!(count("AsA"), 20_825 - 17_296);
assert!(OmahaRange::parse("AsAs", 4).is_err());
}
#[test]
fn test_coverage_and_union() {
let range = OmahaRange::parse("AAxx, KKxx", 4).unwrap();
let both = count("AAKK");
assert_eq!(range.combos(), count("AA") + count("KK") - both);
assert!((range.coverage() - range.combos() as f64 / 270_725.0).abs() < 1e-12);
let mut weighted = OmahaRange::new(4);
weighted.add("AAAA", 0.5).unwrap();
weighted.add("KKKK", 1.0).unwrap();
assert!((weighted.weighted_coverage() - 1.5 / 270_725.0).abs() < 1e-15);
}
#[test]
fn test_invalid() {
for bad in [
"AAKKQ", "Z", "AA$xx", "AAx$", "$", "$5rd", "$1rd", "$rd2-1", "$rdx", "$rd-1", "$!",
"$!!r",
] {
assert!(OmahaRange::parse(bad, 4).is_err(), "{bad}");
}
assert!(OmahaRange::new(4).add("", 1.0).is_err());
assert_eq!(OmahaRange::parse("", 4).unwrap().combos(), 0);
}
fn is_match(term: &str, hand: &str) -> bool {
let hand = Deck::parse(hand).unwrap();
Term::parse(term, hand.num_cards() as usize)
.unwrap()
.matches(hand)
}
#[test]
fn test_rundowns() {
assert!(is_match("$rd", "Jh Td 9c 8s"));
assert!(is_match("$rd", "As 2d 3c 4h"));
assert!(is_match("$rd", "As Kd Qc Jh"));
assert!(!is_match("$rd", "Ks Ad 2c 3h"));
assert!(!is_match("$rd", "Th 9d 8c 6s"));
assert!(is_match("$rd1", "Th 9d 8c 6s"));
assert!(!is_match("$rd1", "Jh Td 9c 8s"));
assert!(is_match("$rd2", "Th 9d 7c 5s"));
assert!(is_match("$rd2", "Th 8d 6c 5s"));
assert!(is_match("$rd0-1", "Jh Td 9c 8s"));
assert!(is_match("$rd0-1", "Th 9d 8c 6s"));
assert!(is_match("$rd1", "Th 9d 7c 6s"));
assert!(!is_match("$rd0-1", "Th 9d 7c 5s"));
assert!(is_match("$3rd", "Th Td 9c 8s"));
assert!(is_match("$3rd$1p", "Th Td 9c 8s"));
assert!(is_match("TT$3rd", "Th Td 9c 8s"));
assert!(!is_match("$rd", "Th Td 9c 8s"));
assert!(is_match("$3rd", "Kh Td 9c 8s"));
assert!(is_match("$3rd", "Jh Td 9c 8s"));
assert!(!is_match("$3rd$!rd", "Jh Td 9c 8s"));
assert!(is_match("$3rd1", "Kh Td 9c 7s"));
assert!(!is_match("$3rd", "Kh Td 8c 6s"));
assert!(is_match("$2rd", "Kh Qd 8c 2s"));
assert_eq!(count("$rd"), 11 * 256);
assert_eq!(count("$rd1"), 30 * 256);
assert_eq!(count("$rd0-1"), count("$rd") + count("$rd1"));
assert!(count("$rd") + count("$rd1") + count("$rd2") <= count("$np"));
}
#[test]
fn test_rundown_five_card() {
assert!(is_match("$rd", "Qh Jh Td 9c 8s"));
assert!(is_match("$rd1", "Qh Jh Td 9c 7s"));
assert!(is_match("$4rd", "Qh Jh Td 9c 2s"));
assert!(!is_match("$rd", "Qh Jh Td 9c 2s"));
}
#[test]
fn test_negation() {
assert_eq!(count("$!r"), 270_725 - count("$r"));
assert_eq!(count("KK$!r"), count("KK$ss") + count("KK$ds"));
}
#[test]
fn test_triple_suited() {
assert!(is_match("$ts", "As Ks Qh Jh 9d 8d"));
assert!(!is_match("$ds", "As Ks Qh Jh 9d 8d"));
assert!(is_match("$ds", "As Ks Qh Jh 9d 8c"));
assert!(is_match("$ds", "As Ks Qs Jh Th 8c"));
assert!(!is_match("$ts", "As Ks Qs Jh Th 8c"));
assert_eq!(count("$ts"), 0);
}
#[test]
fn test_five_card() {
let range = OmahaRange::parse("AA$ds", 5).unwrap();
assert_eq!(range.total_hands(), 2_598_960);
assert!(range.combos() > 0);
}
}