use rand::{RngExt, SeedableRng, rngs::StdRng};
use crate::deck::Deck;
use crate::ranking::hand_rank::HandRanking;
pub mod badugi;
pub mod dealt_hand;
pub mod deuce_to_seven;
pub mod flop_game;
pub mod holdem;
pub mod omaha;
pub mod omaha_analysis;
pub mod omaha_bomb_pot;
pub mod omaha_hilo;
pub mod omaha_range;
pub mod omaha_range_equity;
pub mod omaha_rank_model;
pub mod razz;
pub mod single_draw_27;
pub mod single_draw_a5;
pub mod stud;
pub mod stud_hilo;
pub trait GameState {}
#[derive(PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GameWinner<H: HandRanking> {
pub(crate) player_index: usize,
pub(crate) pot_amount: f64,
pub(crate) winning_hand: H,
}
impl<H: HandRanking> GameWinner<H> {
pub fn new(index: usize, pot_amount: f64, winning_hand: H) -> Self {
Self {
player_index: index,
pot_amount,
winning_hand,
}
}
pub fn player_index(&self) -> usize {
self.player_index
}
pub fn pot_amount(&self) -> f64 {
self.pot_amount
}
pub fn winning_hand(&self) -> &H {
&self.winning_hand
}
}
pub(crate) struct WinnerTracker<H: HandRanking + Ord + Copy> {
best_hand: Option<H>,
winners: Vec<usize>,
}
impl<H: HandRanking + Ord + Copy> WinnerTracker<H> {
pub fn new() -> Self {
Self {
best_hand: None,
winners: vec![],
}
}
pub fn best_hand(&self) -> Option<H> {
self.best_hand
}
pub fn consider(&mut self, player_index: usize, hand: H) {
match &self.best_hand {
Some(best) => match best.cmp(&hand) {
std::cmp::Ordering::Less => {
self.best_hand = Some(hand);
self.winners = vec![player_index];
}
std::cmp::Ordering::Equal => {
if !self.winners.contains(&player_index) {
self.winners.push(player_index);
}
}
std::cmp::Ordering::Greater => {}
},
None => {
self.best_hand = Some(hand);
self.winners = vec![player_index];
}
}
}
pub fn into_results(self) -> Vec<GameWinner<H>> {
let winner_count = self.winners.len();
if let Some(best_hand) = self.best_hand
&& winner_count > 0
{
let pot_distribution = 1.0 / winner_count as f64;
self.winners
.iter()
.map(|x| GameWinner::new(*x, pot_distribution, best_hand))
.collect()
} else {
vec![]
}
}
}
pub(crate) struct FastWinnerTracker {
best_score: u32,
winners: Vec<usize>,
}
impl FastWinnerTracker {
pub fn new() -> Self {
Self {
best_score: 0,
winners: Vec::with_capacity(4),
}
}
#[inline(always)]
pub fn best_score(&self) -> u32 {
self.best_score
}
#[inline(always)]
pub fn consider(&mut self, player_index: usize, score: u32) {
if score > self.best_score {
self.best_score = score;
self.winners.clear();
self.winners.push(player_index);
} else if score == self.best_score && !self.winners.contains(&player_index) {
self.winners.push(player_index);
}
}
pub fn distribute(&self, shares: &mut [u64]) {
if let Some(share) = EQUITY_SCALE.checked_div(self.winners.len() as u64) {
for &w in &self.winners {
shares[w] += share;
}
}
}
pub fn winners(&self) -> &[usize] {
&self.winners
}
pub fn distribute_scaled(&self, shares: &mut [u64], scale: u64) {
if let Some(share) = scale.checked_div(self.winners.len() as u64) {
for &w in &self.winners {
shares[w] += share;
}
}
}
}
pub(crate) struct CardDealer {
cards: Vec<Deck>,
dealt: usize,
rng: StdRng,
}
impl CardDealer {
pub fn new(deck: Deck) -> Self {
Self::with_rng(deck, rand::make_rng())
}
pub fn seeded(deck: Deck, seed: u64) -> Self {
Self::with_rng(deck, StdRng::seed_from_u64(seed))
}
pub fn maybe_seeded(deck: Deck, seed: Option<u64>) -> Self {
seed.map_or_else(|| Self::new(deck), |s| Self::seeded(deck, s))
}
fn with_rng(deck: Deck, rng: StdRng) -> Self {
Self {
cards: deck.enumerate_combinations(1).collect(),
dealt: 0,
rng,
}
}
pub fn random_f64(&mut self, below: f64) -> f64 {
self.rng.random_range(0.0..below)
}
pub fn available(&self) -> usize {
self.cards.len()
}
pub fn reset(&mut self) {
self.dealt = 0;
}
pub fn deal(&mut self, n: usize) -> Deck {
let mut hand = Deck::empty();
for _ in 0..n {
let j = self.rng.random_range(self.dealt..self.cards.len());
self.cards.swap(self.dealt, j);
hand |= self.cards[self.dealt];
self.dealt += 1;
}
hand
}
pub fn deal_excluding(&mut self, n: usize, exclude: Deck) -> Deck {
let exclude = u64::from(exclude);
let mut hand = Deck::empty();
let mut dealt_to_hand = 0;
while dealt_to_hand < n {
let j = self.rng.random_range(self.dealt..self.cards.len());
self.cards.swap(self.dealt, j);
let card = self.cards[self.dealt];
self.dealt += 1;
if u64::from(card) & exclude == 0 {
hand |= card;
dealt_to_hand += 1;
}
}
hand
}
}
pub(crate) const EQUITY_SCALE: u64 = 2520;
#[cfg(feature = "parallel")]
const PARALLEL_THRESHOLD: usize = 100;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct EquityShares {
pub runouts: u64,
pub shares: Vec<u64>,
}
impl EquityShares {
pub const SCALE: u64 = EQUITY_SCALE;
pub fn merge(&mut self, other: &EquityShares) {
if self.shares.len() < other.shares.len() {
self.shares.resize(other.shares.len(), 0);
}
for (s, o) in self.shares.iter_mut().zip(&other.shares) {
*s += o;
}
self.runouts += other.runouts;
}
pub fn equity(&self) -> Vec<f64> {
if self.runouts == 0 {
return vec![0.0; self.shares.len()];
}
let divisor = Self::SCALE as f64 * self.runouts as f64;
self.shares.iter().map(|&s| s as f64 / divisor).collect()
}
pub fn equity_sum(&self) -> Vec<f64> {
self.shares
.iter()
.map(|&s| s as f64 / Self::SCALE as f64)
.collect()
}
}
pub(crate) fn accumulate_equity<F>(
runouts: Vec<crate::deck::Deck>,
num_players: usize,
eval: F,
) -> Vec<f64>
where
F: Fn(&crate::deck::Deck, &mut [u64]) + Send + Sync,
{
accumulate_shares(runouts, num_players, eval).equity()
}
pub(crate) fn accumulate_shares<F>(
runouts: Vec<crate::deck::Deck>,
num_players: usize,
eval: F,
) -> EquityShares
where
F: Fn(&crate::deck::Deck, &mut [u64]) + Send + Sync,
{
let hand_count = runouts.len() as u64;
let win_shares = {
#[cfg(feature = "parallel")]
{
if runouts.len() >= PARALLEL_THRESHOLD {
use rayon::prelude::*;
runouts
.par_iter()
.fold(
|| vec![0u64; num_players],
|mut shares, community| {
eval(community, &mut shares);
shares
},
)
.reduce(
|| vec![0u64; num_players],
|mut a, b| {
for (i, &v) in b.iter().enumerate() {
a[i] += v;
}
a
},
)
} else {
let mut shares = vec![0u64; num_players];
for community in &runouts {
eval(community, &mut shares);
}
shares
}
}
#[cfg(not(feature = "parallel"))]
{
let mut shares = vec![0u64; num_players];
for community in &runouts {
eval(community, &mut shares);
}
shares
}
};
EquityShares {
runouts: hand_count,
shares: win_shares,
}
}
pub trait GameEvaluation<GS: GameState, H: HandRanking> {
fn evaluate_winners(&self, game_state: &GS) -> Vec<GameWinner<H>>;
}
pub trait GameEquityEvaluation<GS: GameState, H: HandRanking, GE: GameEvaluation<GS, H>> {
fn evaluate_equity(&self, game_state: &GS) -> Vec<f64>;
}