card-cores
Reusable playing-card traits and concrete card/deck implementations for Rust.
The crate currently provides:
- Generic
CardandDecktraits - A compact standard playing-card representation
- A traditional 52-card deck
- Parsing and display for ranks, suits, and cards
- Drawing, shuffling, removal, iteration, and fixed-size combinations
- Convenience decks for Kuhn Poker and Leduc Hold'em
Modules
card-cores::traitscontains the genericCardandDeckinterfaces.card-cores::traditionalcontains the standard rank, suit, card, and deck types.
Usage
use card-;
let ace_of_spades = from;
assert_eq!;
assert_eq!;
let mut deck = new;
assert_eq!;
deck.shuffle;
let drawn = deck.draw;
assert!;
assert_eq!;
Cards
A traditional card is constructed from a Rank and Suit:
use card-;
let card = from;
Cards use a compact u8 representation:
use card-;
let card = from;
let value: u8 = card.into;
assert_eq!;
Cards are encoded in rank-major order:
2c 2d 2h 2s 3c 3d 3h 3s ... Ac Ad Ah As
The supported rank symbols are:
2 3 4 5 6 7 8 9 T J Q K A
The supported suit symbols are:
c d h s
These represent clubs, diamonds, hearts, and spades.
Parsing
Cards can be parsed from their two-character representation:
use card-Card;
let ace_of_spades = try_from?;
let ten_of_diamonds = try_from?;
# Ok::ParseCardError>
Rank parsing is case-insensitive, and suit parsing accepts either uppercase or lowercase letters.
Decks
Create a complete 52-card deck through the Deck trait:
use card-;
let deck = new;
assert_eq!;
assert!;
Drawing
draw removes and returns the final card in the deck's current ordering:
use card-;
let mut deck = new;
let card = deck.draw;
assert!;
assert_eq!;
Shuffling
use card-;
let mut deck = new;
deck.shuffle;
Removing a card
use card-;
let mut deck = new;
let ace_of_spades = from;
assert_eq!;
assert_eq!;
assert_eq!;
Removal uses swap removal, so it does not preserve the ordering of the remaining cards.
Iterating without consuming the deck
use card-;
let deck = new;
for card in deck.iter
A deck can also be consumed through IntoIterator:
use card-;
let deck = new;
for card in deck
Card combinations
Deck::combinations returns every unordered combination of N cards as an array:
use card-;
let deck = new;
let number_of_two_card_hands = deck..count;
assert_eq!;
Because N is a const generic parameter, each result has the type [Card; N]:
use card-;
let deck = new;
for in deck.
Kuhn Poker
Deck::kuhn creates a three-card deck containing one jack, queen, and king:
use card-;
let deck = kuhn;
assert_eq!;
The deck contains:
Kc Qc Jc
Leduc Hold'em
Deck::leduc creates a six-card deck containing two cards of each rank:
use card-;
let deck = leduc;
assert_eq!;
The deck contains:
Kc Qc Jc Kd Qh Js
The suits distinguish cards with equal ranks; standard Leduc hand strength depends on rank rather than suit.
Generic interfaces
The traits in card-cores::traits allow card-game implementations to operate independently of the traditional card representation.
Card
A card implementation defines associated rank and suit types and exposes accessors for each:
use ;
Deck
A deck owns cards and provides the operations commonly needed by card games:
Custom card games can implement these traits without depending on the traditional 52-card deck.
License
This project is licensed under the MIT License. See the LICENSE file for details.