Crate poker

Source
Expand description

§Poker

poker is a crate for efficient poker hand evaluation ported into Rust from the treys Python package. This package utilizes algorithms for speedy evaluation, which have been added on to or made more idiomatic for Rust where appropriate.

use poker::{cards, Card, EvalClass, Evaluator, Rank};

// 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_hand = eval.evaluate(royal_flush_cards)?;

assert!(matches!(
    royal_flush_hand.class(),
    EvalClass::StraightFlush {
        high_rank: Rank::Ace
    }
));
assert!(royal_flush_hand.is_royal_flush());

// Compare hands
let pair_cards: Vec<Card> = cards!("3c 4h Td 3h Kd").try_collect()?;
let pair_hand = eval.evaluate(pair_cards)?;
assert!(royal_flush_hand.is_better_than(pair_hand));

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::{deck, Eval, Evaluator};

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(hand).unwrap_or(Eval::WORST)
    }));
}

let max = handles
    .into_iter()
    .map(|handle| handle.join().unwrap())
    .fold(Eval::WORST, 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 Card types 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 Card into a single boxed slice of Card. 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
The result of a successful poker hand evaluation. When printed in Display format, shows the proper, qualified name of the poker hand.
Evaluator
This structure does all the heavy lifting of evaluating poker hands.

Enums§

EvalClass
A utility enumeration type for pattern matching against the result of Eval::class. Each variant represents a class of poker hand. Royal flush is not included, but can be matched against EvalClass:StraightFlush { high_card: Rank::Ace } if desired.
EvalError
An error that can be thrown when evaluating poker hands.
ParseCardError
An error than can be thrown when parsing Card types 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.
OSZAR »