31 releases
Uses new Rust 2024
new 0.6.0 | Mar 8, 2025 |
---|---|
0.5.2 | Aug 26, 2024 |
0.5.1 | Mar 23, 2024 |
0.5.0 | Dec 17, 2023 |
0.3.8 | Oct 12, 2020 |
#32 in Game dev
206 downloads per month
Used in 3 crates
255KB
5.5K
SLoC
cardpack.rs
Generic pack of cards library written in Rust. The goals of the library include:
- Various types of decks of cards.
- Internationalization support.
- Ability to create custom sorts for a specific pack of cards.
UPDATE: This is a complete rewrite of the library taking advantage of generics in order to make the code cleaner, and easier to extend.
Usage
use cardpack::prelude::*;
fn main() {
let mut pack = Standard52::deck();
pack.shuffle();
// Deal no-limit hold'em hands for two players:
let small_blind = pack.draw(2).unwrap().sorted_by_rank();
let big_blind = pack.draw(2).unwrap().sorted_by_rank();
println!("small blind: {}", small_blind.to_string());
println!("big blind: {}", big_blind.to_string());
let flop = pack.draw(3).unwrap();
let turn = pack.draw(1).unwrap();
let river = pack.draw(1).unwrap();
println!();
println!("flop : {}", flop.to_string());
println!("turn : {}", turn.to_string());
println!("river: {}", river.to_string());
// Now, let's validate that the cards when collected back together are a valid Standard52
// deck of cards.
let reconstituted_pile =
Pile::<Standard52>::pile_on(&*vec![pack, small_blind, big_blind, flop, turn, river]);
assert!(Standard52::deck().same(&reconstituted_pile));
}
Details
The goal of this library is to be able to support the creation of card decks of various sizes and suits. Out of the box, the library supports:
- French Deck
- Short Deck
- Skat
- Tarot with Major and Minor Arcana
The project takes advantage of Project Fluent's Rust support to offer internationalization. Current languages supported are English and German.
Responsibilities
- Represent a specific type of card deck.
- Validate that a collection of cards is valid for that type of deck.
- Create a textual representation of a deck that can be serialized and deserialized.
- Shuffle a deck
- Verify that a specific card is playable given a set of discards.
Examples
The library has several examples programs, including demo
which shows you the different decks
available.
For the traditional 54 card French Deck with Jokers:
❯ cargo run --example demo -- --french -v
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s
Running `target/debug/examples/cli --french --verbose`
French Deck: B🃟 L🃟 A♠ K♠ Q♠ J♠ T♠ 9♠ 8♠ 7♠ 6♠ 5♠ 4♠ 3♠ 2♠ A♥ K♥ Q♥ J♥ T♥ 9♥ 8♥ 7♥ 6♥ 5♥ 4♥ 3♥ 2♥ A♦ K♦ Q♦ J♦ T♦ 9♦ 8♦ 7♦ 6♦ 5♦ 4♦ 3♦ 2♦ A♣ K♣ Q♣ J♣ T♣ 9♣ 8♣ 7♣ 6♣ 5♣ 4♣ 3♣ 2♣
French Deck Index: BJ LJ AS KS QS JS TS 9S 8S 7S 6S 5S 4S 3S 2S AH KH QH JH TH 9H 8H 7H 6H 5H 4H 3H 2H AD KD QD JD TD 9D 8D 7D 6D 5D 4D 3D 2D AC KC QC JC TC 9C 8C 7C 6C 5C 4C 3C 2C
French Deck Shuffled: K♣ 7♦ 8♣ Q♥ 6♠ J♦ 4♦ J♥ K♠ 9♥ 6♥ T♥ 2♦ 3♦ 3♣ J♣ 3♥ Q♣ 5♥ Q♦ 3♠ T♣ 7♥ 4♥ K♦ 5♦ 2♠ 6♦ T♠ 8♥ T♦ 7♠ 8♠ 2♣ Q♠ 7♣ A♣ 5♠ A♥ 9♣ 2♥ 9♦ 9♠ 4♠ K♥ 8♦ 5♣ A♦ L🃟 B🃟 A♠ 6♣ 4♣ J♠
Long in English and German:
Joker Full-Color
Joker One-Color
Ace of Spades
King of Spades
Queen of Spades
...
Other decks in the demo program are canasta
, euchre
, short
, pinochle
, skat
, spades
,
standard
, and tarot
.
Other examples are:
cargo run --example bridge
- Prints out a Bridge deal.cargo run --example handandfoot
- Shows how to support more than one decks like in the game Hand and Foot.cargo run --example poker
- A random heads up no-limit Poker deal.
References
Other Deck of Cards Libraries
- ascclemens/cards
- locka99/deckofcards-rs
- vsupalov/cards-rs
- droundy/bridge-cards
- Tarot Libraries
Dependencies
Dev Dependencies
- term-table
- rstest - Fixture-based test framework for Rust
TODO
Dependencies
~9–18MB
~229K SLoC