#[repr(C)]pub struct Deck { /* private fields */ }
Expand description
wiki | “Live” deck of cards for play
An array of Card objects with methods appropriate for a deck of cards.
Note that cards are pop()
’d from end of the array for speed, making
that notionally the “top” of the deck. We show the Deck reversed
when printing for this reason to bake debugging easier. Cards in the
deck are not accessed randomly by index, though they can be removed
by value.
Implementations§
source§impl Deck
impl Deck
sourcepub fn new(t: DeckType) -> Deck
pub fn new(t: DeckType) -> Deck
Create a new deck from the given DeckType, e.g.
use onejoker::prelude::*;
let d = Deck::new(DeckType::English);
sourcepub fn new_by_name(dname: &str) -> Deck
pub fn new_by_name(dname: &str) -> Deck
Create a new deck from a DeckType by name, e.g.
use onejoker::prelude::*;
let d = Deck::new_by_name("canasta");
sourcepub fn deck_type(&self) -> DeckType
pub fn deck_type(&self) -> DeckType
Return the DeckType associated with this deck
use onejoker::prelude::*;
let d = Deck::new_by_name("lowball");
assert_eq!(DeckType::LowJoker, d.deck_type());
sourcepub fn reproducible(self, seed: u64) -> Self
pub fn reproducible(self, seed: u64) -> Self
Set the PRNG seed for this deck
sourcepub fn shuffled(self) -> Self
pub fn shuffled(self) -> Self
Initial shuffle for new deck
Returns self
for chaining.
use onejoker::prelude::*;
let d = Deck::new(DeckType::English).shuffled();
sourcepub fn new_hand(&self) -> Hand
pub fn new_hand(&self) -> Hand
Create a new Hand associated with this deck
use onejoker::prelude::*;
let d = Deck::new(DeckType::English);
let h = d.new_hand();
sourcepub fn to_vec(&self) -> Vec<Card>
pub fn to_vec(&self) -> Vec<Card>
Export the current contents of the deck
Returns a new vector of Card, leaving the deck itself unchanged.
use onejoker::prelude::*;
let d = Deck::new(DeckType::English).shuffled();
let saved_copy: Vec<Card> = d.to_vec();
sourcepub fn shuffle(&mut self)
pub fn shuffle(&mut self)
Shuffle the deck in place
Does not refill the deck, but just shuffles whatever cards are
currently in it. There is a separate refill_and_shuffle
method
for doing both.
sourcepub fn refill(&mut self)
pub fn refill(&mut self)
Refill the deck to its original contents
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::Pinochle).shuffled();
let mut h = d.new_hand().init(d.draw(12));
println!("{}", d.remaining()); // 36
// . . .
d.refill();
println!("{}", d.remaining()); // 48
sourcepub fn refill_and_shuffle(&mut self)
pub fn refill_and_shuffle(&mut self)
Refill the deck and shuffle
use onejoker::prelude::*;
let mut d = Deck::new_by_name("bridge").shuffled();
let mut h = d.new_hand().init(d.draw(13));
// . . .
d.refill_and_shuffle();
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of cards currently in the deck
use onejoker::prelude::*;
let mut d = Deck::new_by_name("panguinge").shuffled();
println!("{}, {}", d.size(), d.len()); // 320, 320
let mut h = d.new_hand().init(d.draw(10));
println!("{}, {}", d.size(), d.len()); // 320, 310
sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Return the total number of cards in the full deck
use onejoker::prelude::*;
let d = Deck::new(DeckType::Swiss);
println!("{}", d.size()); // 36
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Is the deck empty?
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English).shuffled();
let v: Vec<Card> = d.pop_all().collect();
assert!(d.is_empty());
d.refill();
assert!(d.is_not_empty());
sourcepub fn is_not_empty(&self) -> bool
pub fn is_not_empty(&self) -> bool
Is the deck not empty?
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English).shuffled();
assert!(d.is_not_empty());
let v: Vec<Card> = d.pop_all().collect();
assert!(d.is_empty());
sourcepub fn contains(&self, card: Card) -> bool
pub fn contains(&self, card: Card) -> bool
Does the deck contain the given Card?
#[macro_use] extern crate onejoker;
use onejoker::prelude::*;
let d = Deck::new(DeckType::English);
assert!(d.contains(card!("As")));
assert!(! d.contains(card!("Jk")));
sourcepub fn push(&mut self, card: Card) -> bool
pub fn push(&mut self, card: Card) -> bool
Push a Card onto the deck
We do not generally expect cards to go in this direction, but might need to for testing and simulation.
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English).shuffled();
let mut burn = d.new_hand();
burn.push(d.pop().unwrap());
// oops, put it back
d.push(burn.pop().unwrap());
sourcepub fn pop(&mut self) -> Option<Card>
pub fn pop(&mut self) -> Option<Card>
Pop a Card from the deck
Return None
if the deck is empty.
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English).shuffled();
let c: Card = d.pop().unwrap();
sourcepub fn push_n<I>(&mut self, n: usize, cards: I) -> usizewhere
I: IntoIterator<Item = Card>,
pub fn push_n<I>(&mut self, n: usize, cards: I) -> usizewhere
I: IntoIterator<Item = Card>,
Push first n
Cards of collection onto the deck
Return the number actually pushed.
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English).shuffled();
let mut burn = d.new_hand();
burn.push_n(3, d.pop_n(3));
// oops, put them all back
d.push_n(3, burn.pop_n(3));
sourcepub fn push_all<I>(&mut self, cards: I) -> usizewhere
I: IntoIterator<Item = Card>,
pub fn push_all<I>(&mut self, cards: I) -> usizewhere
I: IntoIterator<Item = Card>,
Push a collection of Cards onto the deck
Return the number actually pushed.
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English).shuffled();
let mut burn = d.new_hand();
burn.push_all(d.draw(3));
// oops, put them all back
d.push_all(burn.pop_all());
sourcepub fn pop_n(&mut self, n: usize) -> impl Iterator<Item = Card>
pub fn pop_n(&mut self, n: usize) -> impl Iterator<Item = Card>
Pop n
cards from the deck as an iterator
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English).shuffled();
let mut flop: Vec<Card> = d.pop_n(3).collect();
sourcepub fn draw(&mut self, n: usize) -> impl Iterator<Item = Card>
pub fn draw(&mut self, n: usize) -> impl Iterator<Item = Card>
Synonym for pop_n()
use onejoker::prelude::*;
// A common idiom for initial deals:
let mut d = Deck::new(DeckType::English).shuffled();
let mut player1 = d.new_hand().init(d.draw(5));
let mut player2 = d.new_hand().init(d.draw(5));
sourcepub fn pop_all(&mut self) -> impl Iterator<Item = Card> + '_
pub fn pop_all(&mut self) -> impl Iterator<Item = Card> + '_
Pop all cards from the deck as an iterator
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English).shuffled();
let mut pile: Vec<Card> = d.pop_all().collect();
assert_eq!(52, pile.len());
assert!(d.is_empty());
sourcepub fn remove_card(&mut self, card: Card) -> bool
pub fn remove_card(&mut self, card: Card) -> bool
Remove a Card from the deck by value
Return true
if found.
#[macro_use] extern crate onejoker;
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English);
assert!(d.remove_card(card!("As")));
assert!(! d.remove_card(card!("Jk")));
sourcepub fn draw_card(&mut self, c: Card) -> bool
pub fn draw_card(&mut self, c: Card) -> bool
Synonym for remove_card()
#[macro_use] extern crate onejoker;
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English);
assert!(d.draw_card(card!("As")));
sourcepub fn draw_hand<I>(&mut self, cards: I) -> impl Iterator<Item = Card>where
I: IntoIterator<Item = Card>,
pub fn draw_hand<I>(&mut self, cards: I) -> impl Iterator<Item = Card>where
I: IntoIterator<Item = Card>,
Take the exactly given Cards from the Deck
Useful for simulations and testing.
#[macro_use] extern crate onejoker;
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English);
let mut p1 = d.new_hand().init(d.draw_hand(hand!("Ac", "Kd")));
let mut p2 = d.new_hand().init(d.draw_hand(hand!("2h", "2s")));
sourcepub fn sort(&mut self)
pub fn sort(&mut self)
Sort the deck in place
Uses the same sort as for hands, which sorts them descending by rank
and then by suit. But remember that cards are pop()
’d from the end,
so the “top” of the deck is the end of the array, so cards will be
dealt in ascending order.
#[macro_use] extern crate onejoker;
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English).shuffled();
d.sort();
assert_eq!(card!("2c"), d.pop().unwrap());
sourcepub fn combinations(&self, k: usize) -> impl Iterator<Item = Hand>
pub fn combinations(&self, k: usize) -> impl Iterator<Item = Hand>
Iterate over combinations
Iterate over all combinations of k
cards from those
currently in the deck.
#[macro_use] extern crate onejoker;
use onejoker::prelude::*;
let mut d = Deck::new(DeckType::English);
let p1 = d.new_hand().init(d.draw_hand(hand!("Ac", "Kd")));
let p2 = d.new_hand().init(d.draw_hand(hand!("2h", "2s")));
// Run through 1,712,304 possible Texas Hold'em boards
for h in d.combinations(5) {
// . . .
}
Trait Implementations§
source§impl<'de> Deserialize<'de> for Deck
impl<'de> Deserialize<'de> for Deck
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<'a> IntoIterator for &'a Deck
impl<'a> IntoIterator for &'a Deck
source§impl IntoIterator for Deck
impl IntoIterator for Deck
source§impl Ord for Deck
impl Ord for Deck
source§impl PartialEq for Deck
impl PartialEq for Deck
source§impl PartialOrd for Deck
impl PartialOrd for Deck
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl Eq for Deck
Auto Trait Implementations§
impl Freeze for Deck
impl RefUnwindSafe for Deck
impl Send for Deck
impl Sync for Deck
impl Unpin for Deck
impl UnwindSafe for Deck
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)