Expand description
§Poker
poker is a crate for efficient poker hand evaluation supporting both
5-card and 3-card poker hands. It is ported into Rust from the treys
Python package, utilizing fast lookup algorithms for speedy evaluation,
which have been adapted and made more idiomatic for Rust where appropriate.
§5-Card Poker Example
use poker::{Card, Evaluator, Rank, cards, evaluate::FiveCardHandClass};
// Create a reusable evaluator
let eval = Evaluator::new();
// Parse a `Vec` of cards from a str
let royal_flush_cards: Vec<Card> = cards!("Ks Js Ts Qs As").try_collect()?;
// Evaluate the hand
let royal_flush_eval = eval.evaluate_five(royal_flush_cards)?;
assert!(matches!(
royal_flush_eval.classify(),
FiveCardHandClass::StraightFlush { rank: Rank::Ace }
));
assert!(royal_flush_eval.is_royal_flush());
// Compare hands
let pair_cards: Vec<Card> = cards!("3c 4h Td 3h Kd").try_collect()?;
let pair_eval = eval.evaluate_five(pair_cards)?;
assert!(royal_flush_eval.is_better_than(pair_eval));§3-Card Poker Example
use poker::{Card, Evaluator, Rank, cards};
// Create a reusable evaluator
let eval = Evaluator::new();
// Three-card straight flush
let straight_flush: Vec<Card> = cards!("Ah Kh Qh").try_collect()?;
let sf_eval = eval.evaluate_three(straight_flush)?;
assert!(sf_eval.is_straight_flush());
assert_eq!(sf_eval.straight_flush(), Some(Rank::Ace));
// Compare with three of a kind (straight flush beats three of a kind)
let trips: Vec<Card> = cards!("As Ad Ac").try_collect()?;
let trips_eval = eval.evaluate_three(trips)?;
assert!(sf_eval.is_better_than(trips_eval));The Evaluator does not expose any mutable methods, so it’s perfectly
safe to wrap it into an Arc and share it between
multiple threads.
use std::{cmp, sync::Arc, thread};
use poker::{Eval, Evaluator, FiveCard, deck};
let shared_evaluator = Arc::new(Evaluator::new());
let mut handles = vec![];
for _ in 0..10 {
let evaluator = Arc::clone(&shared_evaluator);
handles.push(thread::spawn(move || {
let deck = deck::shuffled();
let hand = &deck[..5];
evaluator
.evaluate_five(hand)
.unwrap_or(Eval::<FiveCard>::WORST_FIVE)
}));
}
let max = handles
.into_iter()
.filter_map(|handle| handle.join().ok())
.fold(Eval::<FiveCard>::WORST_FIVE, cmp::max);
println!("{}", max);Modules§
- card
- Create and manage poker cards with suits and ranks.
- deck
- A module for generating decks of cards.
- error
- Two different error types that may be encountered when trying to parse
Cardtypes from strings, or when trying to evaluate hands. - evaluate
- Anything and everything about evaluating poker hands.
Macros§
- box_
cards - Use this macro to chain two or more slices of
Cardinto a single boxed slice ofCard. This may be useful for bundling a hand and poker board together for evaluation, as in Texas Holdem. - card
- A utility macro for creating a single card.
- cards
- A utility macro for creating multiple cards.
Structs§
- Card
- A single playing card.
- Eval
- Represents the successful results of evaluation a poker hand.
- Evaluator
- The
Evaluatorstruct is used to evaluate poker hands. It contains lookup tables for three-card and five-card poker hands. - Five
Card - Marker type for five-card poker hands.
- Three
Card - Marker type for three-card poker hands.
Enums§
- Eval
Error - An error that can be thrown when evaluating poker hands.
- Parse
Card Error - An error that can be thrown when parsing
Cardtypes from strings. - Rank
- An enumeration type for representing the thirteen card ranks, from two to ace.
- Suit
- An enumeration type for representing the four card suits.
Type Aliases§
- Eval
Result - Result type for evaluation operations, either returning an
Evalor anEvalError.