Expand description
AyaPoker is a Rust library for fast poker hand evaluation based on the OMPEval C++ hand evaluator, with support for most popular poker variants.
§Features
- Can be used to rank hands from standard poker, ace-to-five lowball, deuce-to-seven lowball, six-or-better (short-deck), Omaha, Omaha Hi/Lo, Badugi or Baduci.
- Can evaluate hands with 0 to 7 cards, with the missing cards counting as the worst possible kickers, allowing for use in stud poker games.
- Uses compile-time generated perfect hash function lookup tables for excellent runtime performance and fast initialization.
- Has extensive suite of tests to ensure correct implementation of the hand ranking rules for each variant.
§Flags
This crate has the following Cargo features:
std
: By default,aya_poker
is a!#[no_std]
crate, but can be compiled with thestd
feature in order to allow the initialization ofDeck
s with system-generated random seeds.colored
: Usecolored
to display cards and hands in color.colored-4color
: Same ascolored
, but using a four-color deck.
§Example
use std::cmp::Ordering;
use aya_poker::{base::*, deck::Deck, poker_rank};
const SEED: u64 = 42;
const SAMPLE_SIZE: usize = 100_000;
fn main() -> Result<(), ParseError> {
// We can initialize cards by specifying the rank and suit,
// and then use collect to combine the cards into a Hand
let player = [
Card::new(Rank::King, Suit::Hearts),
Card::new(Rank::King, Suit::Clubs),
]
.iter()
.collect();
// Or, first parse the cards from strings
let opponent = ["Js".parse::<Card>(), "Qs".parse::<Card>()]
.iter()
.copied()
.collect::<Result<Hand, ParseError>>()?;
// Or, parse a space-separated string of cards as a Hand
let board = "Ah 5s Ts".parse()?;
let equity = equity_calculator(&player, &opponent, &board);
println!(
"{} has {:.1}% equity on {} against {}.",
player,
100.0 * equity,
board,
opponent
);
Ok(())
}
fn equity_calculator(
player: &Hand,
opponent: &Hand,
board: &Hand,
) -> f64 {
// To simulate board run-outs, we begin by preparing a deck
// that doesn't contain the already dealt-out cards
let available_cards = CARDS
.iter()
.filter(|c| !player.contains(c))
.filter(|c| !opponent.contains(c))
.filter(|c| !board.contains(c));
let mut deck = Deck::with_seed(available_cards, SEED);
let mut pots_won = 0.0;
for _ in 0..SAMPLE_SIZE {
// Then, for each run we draw cards to complete the board
deck.reset();
let missing = 5 - board.len();
let complete_board = board
.iter()
.chain(deck.deal(missing).unwrap().iter())
.collect::<Hand>();
// Evaluate the player's hand given the completed board
let mut player_hand = *player;
player_hand.extend(complete_board.iter());
let player_rank = poker_rank(&player_hand);
// Evaluate the opponent's hand
let mut opponent_hand = *opponent;
opponent_hand.extend(complete_board.iter());
let opponent_rank = poker_rank(&opponent_hand);
// And record the player's share of the pot for the run
match player_rank.cmp(&opponent_rank) {
Ordering::Greater => pots_won += 1.0,
Ordering::Less => {}
Ordering::Equal => pots_won += 0.5,
};
}
pots_won / SAMPLE_SIZE as f64
}
Modules§
- base
- Basic types for playing card games.
- deck
- Deck types optimized for fast shuffling suitable for use in simulators.
Structs§
- AceFive
Hand Rank - The strength ranking of a hand in ace-five lowball poker.
- Baduci
Hand Rank - The strength ranking of a hand in Baduci (ace-high Badugi).
- Badugi
Hand Rank - The strength ranking of a hand in Badugi.
- Deuce
Seven Hand Rank - The strength ranking of a hand in deuce-seven lowball poker.
- Poker
Hand Rank - The strength ranking of a hand in standard poker.
- Short
Deck Hand Rank - The strength ranking of a hand in six-plus (short-deck) poker.
Enums§
- Badugi
Rank Category - A Badugi/Baduci hand-ranking category corresponding to the size of the made hand.
- Poker
Rank Category - A poker hand-ranking category, i.e. a straight, a flush, etc.
Functions§
- ace_
five_ rank - Returns the rank of the best 5-card ace-five lowball poker hand that can be made from the given cards.
- baduci_
rank - Returns the rank of the best Baduci (Badugi with aces playing high) hand that can be made from the given cards.
- badugi_
rank - Returns the rank of the best Badugi hand that can be made from the given cards.
- deuce_
seven_ rank - Returns the rank of the best 5-card deuce-seven lowball poker hand that can be made from the given cards.
- omaha_
lo_ rank - Returns the rank of the best 5-card ace-five lowball poker hand that can be made with precisely two hole cards and three cards from the board.
- omaha_
rank - Returns the rank of the best 5-card poker hand that can be made with two hole cards and three board cards.
- poker_
rank - Returns the rank of the best standard 5-card poker hand that can be made from the given cards.
- short_
deck_ rank - Returns the rank of the best 5-card six-or-better poker hand that can be made from the given cards.