Struct onejoker::cards::deck::Deck

source ·
#[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

source

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);
source

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");
source

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());
source

pub fn reproducible(self, seed: u64) -> Self

Set the PRNG seed for this deck

source

pub fn shuffled(self) -> Self

Initial shuffle for new deck

Returns self for chaining.

use onejoker::prelude::*;

let d = Deck::new(DeckType::English).shuffled();
source

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();
source

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();
source

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.

source

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
source

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();
source

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
source

pub fn remaining(&self) -> usize

Alias for len()

source

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
source

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());
source

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());
source

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")));
source

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());
source

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();
source

pub fn push_n<I>(&mut self, n: usize, cards: I) -> usize
where 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));
source

pub fn push_all<I>(&mut self, cards: I) -> usize
where 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());
source

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();
source

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));
source

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());
source

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")));
source

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")));
source

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")));
source

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());
source

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) {
   // . . .
}
source§

impl Deck

source

pub fn iter(&self) -> CardIter

Create a new iterator over the deck.

Trait Implementations§

source§

impl Clone for Deck

source§

fn clone(&self) -> Deck

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Deck

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Deck

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Deck

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Deck

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl FromStr for Deck

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Deck

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a> IntoIterator for &'a Deck

§

type Item = Card

The type of the elements being iterated over.
§

type IntoIter = CardIter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> CardIter

Creates an iterator from a value. Read more
source§

impl IntoIterator for Deck

§

type Item = Card

The type of the elements being iterated over.
§

type IntoIter = CardIntoIter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> CardIntoIter

Creates an iterator from a value. Read more
source§

impl Ord for Deck

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Deck

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Deck

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Deck

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl 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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,