Struct onejoker::cards::hand::Hand

source ·
#[repr(C)]
pub struct Hand { pub cards: [Card; 22], pub length: u8, pub deck_type: DeckType, }
Expand description

wiki | Hand of cards

A simple array of card objects with some utility methods. It is expected that most access will go through push()/pop(), which are fast, though things like insert() and remove() are available. Limited to 22 cards. If you need more, you can use Vec<Card>, but you lose some error checking and convenience methods.

Fields§

§cards: [Card; 22]

Array of Cards

§length: u8

Number of cards in the hand

§deck_type: DeckType

DeckType associated with this hand

Implementations§

source§

impl Hand

source

pub fn new(deck_type: DeckType) -> Hand

Create new Hand with the given DeckType

use onejoker::prelude::*;

let h = Hand::new(DeckType::OneJoker);
source

pub fn convert_decktype(&self, t: DeckType) -> Hand

Clone the hand, change its deck type, and fix the aces

source

pub fn new_by_name(dname: &str) -> Hand

Create new Hand from name of deck or game

use onejoker::prelude::*;

let h = Hand::new_by_name("onejoker");
source

pub fn init<I>(self, iter: I) -> Self
where I: IntoIterator<Item = Card>,

Initialize new hand

Return self for chaining.

use onejoker::prelude::*;

let d = Deck::new(DeckType::English);
let h = d.new_hand().init(hand!("Qs", "Ac"));
source

pub fn sorted(self) -> Self

Initial sort for new hand

Return self for chaining.

use onejoker::prelude::*;

let d = Deck::new(DeckType::English);
let h = d.new_hand().init(hand!("Qs", "Ac")).sorted();
assert_eq!(h.to_string(), "AcQs");
source

pub fn deck_type(&self) -> DeckType

Return the DeckType of this hand

use onejoker::prelude::*;

let d = Deck::new_by_name("poker"); // alias for "english"
let h = d.new_hand();
assert_eq!(h.deck_type(), DeckType::English);
source

pub fn len(&self) -> usize

How many cards in the hand?

use onejoker::prelude::*;

let mut d = Deck::new(DeckType::English);
let h = d.new_hand().init(d.draw_hand(hand!("Qs", "Ac")));
assert_eq!(h.len(), 2);
source

pub fn is_empty(&self) -> bool

Is the hand empty?

use onejoker::prelude::*;

let mut d = Deck::new(DeckType::English);
let mut h = d.new_hand();
assert!(h.is_empty());
h.push_all(d.draw(5));
assert!(h.is_not_empty());
source

pub fn is_not_empty(&self) -> bool

Is the hand not empty?

source

pub fn clear(&mut self)

Empty the hand

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("As", "Qc"));
assert_eq!(h.len(), 2);
h.clear();
assert!(h.is_empty());
source

pub fn to_vec(&self) -> Vec<Card>

Export vec of Cards

Note that Hand implements the Copy trait, so you can make a copy of a hand with just let h2 = h1;, but sometimes you’ll want a vec.

use onejoker::prelude::*;

let h = Hand::default().init(hand!("As", "Qc"));
let v = h.to_vec();
assert_eq!(v.len(), 2);
assert_eq!(h.len(), 2);
source

pub fn index_of(&self, card: Card) -> Option<usize>

Find given card in the hand

Return the index if the card if present, or None if not.

use onejoker::prelude::*;

let h = Hand::default().init(hand!("Ac", "Kc", "Qc", "Jc", "Tc"));
assert_eq!(h.index_of(QUEEN_OF_CLUBS).unwrap(), 2);
assert_eq!(h.index_of(FOUR_OF_CLUBS), None);
source

pub fn contains(&self, card: Card) -> bool

Does the hand contain the given Card?

use onejoker::prelude::*;

let h = Hand::default().init(hand!("Ac", "Kc", "Qc", "Jc", "Tc"));
assert!(h.contains(QUEEN_OF_CLUBS));
assert!(! h.contains(FOUR_OF_CLUBS));
source

pub fn card_at(&self, index: usize) -> Option<Card>

Return the Card at the given index

Return None if index is out of range.

use onejoker::prelude::*;

let h = Hand::default().init(hand!("Ac", "Kc", "Qc", "Jc", "Tc"));
assert_eq!(h.card_at(2).unwrap(), QUEEN_OF_CLUBS);
assert_eq!(h.card_at(7), None);
source

pub fn set_card_at(&mut self, index: usize, card: Card) -> bool

Set the Card at the given index

Return false if index is out of range or card is invalid.

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ac", "Kc", "Qc", "Jc", "Tc"));
assert_eq!(h.set_card_at(2, QUEEN_OF_DIAMONDS), true);
assert_eq!(h[2], QUEEN_OF_DIAMONDS);
source

pub fn push(&mut self, card: Card) -> bool

Push a Card onto the end of the hand

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ah", "Kh"));
h.push(QUEEN_OF_HEARTS);
assert_eq!(h.to_string(), "AhKhQh");
source

pub fn pop(&mut self) -> Option<Card>

Pop a Card from the end of the hand

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ah", "Kh", "Qh"));
assert_eq!(h.pop().unwrap(), QUEEN_OF_HEARTS);
assert_eq!(h.to_string(), "AhKh");
source

pub fn push_n<I>(&mut self, n: usize, iter: I) -> usize
where I: IntoIterator<Item = Card>,

Push at most n Cards onto the end of the hand

Return the number actually pushed.

use onejoker::prelude::*;

let mut d = Deck::new(DeckType::English);
let mut h = d.new_hand().init(hand!("Ah", "Kh"));
assert_eq!(3, h.push_n(3, d.pop_n(3)));
source

pub fn push_all<I>(&mut self, iter: I) -> usize
where I: IntoIterator<Item = Card>,

Push Cards onto the end of the hand

use onejoker::prelude::*;

let mut d = Deck::new(DeckType::English);
let mut h = d.new_hand().init(hand!("Ah", "Kh"));
h.push_all(d.pop_n(3));
assert_eq!(5, h.len());
source

pub fn pop_n(&mut self, n: usize) -> impl Iterator<Item = Card>

Pop n Cards from the end of the hand

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ah", "Kh", "Qh", "Jh", "Th"));
let v: Vec<Card> = h.pop_n(3).collect();
assert_eq!(v.len(), 3);
source

pub fn pop_all(&mut self) -> impl Iterator<Item = Card>

Pop all Cards from the end of the hand

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ah", "Kh", "Qh", "Jh", "Th"));
let v: Vec<Card> = h.pop_all().collect();
assert_eq!(v.len(), 5);
source

pub fn set<I>(&mut self, iter: I) -> bool
where I: IntoIterator<Item = Card>,

Replace all Cards in the hand with those given

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ah", "Kh"));
h.set(hand!("Qh", "Jh"));
assert_eq!(h.to_string(), "QhJh");
source

pub fn insert_at(&mut self, index: usize, card: Card) -> bool

Insert a Card at the given index

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ah", "Kh", "Jh", "Th"));
h.insert_at(2, QUEEN_OF_HEARTS);
assert_eq!(h.to_string(), "AhKhQhJhTh");
source

pub fn remove_at(&mut self, index: usize) -> Option<Card>

Remove the Card at the given index

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ah", "Kh", "Qh", "Jh", "Th"));
h.remove_at(2);
assert_eq!(h.to_string(), "AhKhJhTh");
source

pub fn remove_card(&mut self, card: Card) -> bool

Remove the given Card from the hand if present

Return true if the card was found and removed.

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ah", "Kh", "Qh", "Jh", "Th"));
h.remove_card(KING_OF_HEARTS);
assert_eq!(h.to_string(), "AhQhJhTh");
source

pub fn truncate(&mut self, n: usize)

Truncate the Hand to the given length

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ah", "Kh", "Qh", "Jh", "Th"));
h.truncate(3);
assert_eq!(h.to_string(), "AhKhQh");
source

pub fn shuffle(&mut self, rng: &mut Random)

Shuffle the Hand in place using the given Random

source

pub fn sort(&mut self)

Sort the Hand in place

Sort is descending by rank, then suit.

use onejoker::prelude::*;

let mut d = Deck::new(DeckType::English);
let mut h = d.new_hand().init(hand!("Th", "Kh", "Jh", "Ah", "Qh"));
h.sort();
assert_eq!(h.to_string(), "AhKhQhJhTh");
source

pub fn combinations(&self, k: usize) -> impl Iterator<Item = Hand>

Iterate over combinations

Iterate over all k-length combinations of cards in the hand.

use onejoker::prelude::*;

let mut d = Deck::new(DeckType::English);
let h = d.new_hand().init(hand!("Ah", "Kh", "Qh", "Jh", "Th"));
let mut count = 0;
for sub in h.combinations(3) {
    count += 1;
    println!("{}", sub);    // "AhKhQh", "AhKhJh", ...
}
assert_eq!(count, 10);
source

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

Return true if the hands are identical

Return true if hands contain exactly the same cards in the same order.

use onejoker::prelude::*;

let h1 = Hand::default().init(hand!("Ah", "Kh", "Qh", "Jh", "Th"));
let mut h2 = Hand::default().init(hand!("Ah", "Kh", "Qh", "Jh", "Th"));
assert!(h1.equals(&h2));
h2.set(hand!("Th", "Ah", "Qh", "Jh", "Kh"));
assert!(! h1.equals(&h2));
source

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

Return true if the hands are equivalent

Return true if hands contain the same cards, regardless of order.

use onejoker::prelude::*;

let h1 = Hand::default().init(hand!("Ah", "Kh", "Qh", "Jh", "Th"));
let h2 = Hand::default().init(hand!("Th", "Ah", "Qh", "Jh", "Kh"));
assert!(h1.is_equivalent_to(&h2));
source

pub fn ace_fix(&mut self)

Fix aces in the Hand to match the DeckType

Used internally–most users should not need this.

use onejoker::prelude::*;

let mut h = Hand::new(DeckType::Low).init(hand!("Ah", "2h"));
assert_eq!(h[0].rank(), Rank::LowAce);
// Unlike push() and set_card_at(), this does not error check
h[0] = ACE_OF_HEARTS;
assert_eq!(h[0].rank(), Rank::Ace);
h.ace_fix();
assert_eq!(h[0].rank(), Rank::LowAce);
source

pub fn discard(&mut self, indices: &[usize]) -> bool

Remove the cards at the given indices

use onejoker::prelude::*;

let mut h = Hand::default().init(hand!("Ah", "Kh", "Qh", "Jh", "Th"));
h.discard(&[1, 3]);
assert_eq!(h.to_string(), "AhQhTh");
source

pub fn sequence(&self) -> Rank

Is the hand a rank sequence?

Return lowest rank if the hand is a sequence of ranks regardless of the present order of cards, else return Rank::None. Works with high- or low-ace decks with no knights. Presence of any joker or duplicate ranks will cause a return of Rank::None.

use onejoker::prelude::*;

let mut h = Hand::new(DeckType::LowJoker).init(hand!("3h","4d","2c"));
assert_eq!(h.sequence(), Rank::Deuce);
h.set(hand!("Qh","Td","9c","Jd"));
assert_eq!(h.sequence(), Rank::Nine);
h.set(hand!("Qh","Kd","Jk","Jd"));
assert_eq!(h.sequence(), Rank::None);
h.set(hand!("5h","9c","Td","8d"));
assert_eq!(h.sequence(), Rank::None);
h.set(hand!("5h","4c","4d","3s"));
assert_eq!(h.sequence(), Rank::None);
source

pub fn blackjack_total(&self) -> (bool, u32)

Return value of hand in blackjack

Tuple contains a boolean indicating if the total is “soft”, and the total value itself. Works with low- or high-ace decks. Treats jokers as aces, knights as ten.

use onejoker::prelude::*;

let mut hand = Hand::new(DeckType::Low).init(hand!("2h","Kh","7c"));
assert_eq!(hand.blackjack_total(), (false, 19));
hand.set(hand!("As","6h","Ad"));
assert_eq!(hand.blackjack_total(), (true, 18));
source§

impl Hand

source

pub fn iter(&self) -> CardIter

Return an iterator over the Cards in the hand.

Trait Implementations§

source§

impl Clone for Hand

source§

fn clone(&self) -> Hand

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 Hand

source§

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

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

impl Default for Hand

source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for Hand

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 Hand

source§

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

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

impl Hash for Hand

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 Index<Range<usize>> for Hand

§

type Output = [Card]

The returned type after indexing.
source§

fn index(&self, index: Range<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeFrom<usize>> for Hand

§

type Output = [Card]

The returned type after indexing.
source§

fn index(&self, index: RangeFrom<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeFull> for Hand

§

type Output = [Card]

The returned type after indexing.
source§

fn index(&self, _index: RangeFull) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<RangeTo<usize>> for Hand

§

type Output = [Card]

The returned type after indexing.
source§

fn index(&self, index: RangeTo<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl Index<usize> for Hand

§

type Output = Card

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl IndexMut<Range<usize>> for Hand

source§

fn index_mut(&mut self, index: Range<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<RangeFrom<usize>> for Hand

source§

fn index_mut(&mut self, index: RangeFrom<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<RangeFull> for Hand

source§

fn index_mut(&mut self, _index: RangeFull) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<RangeTo<usize>> for Hand

source§

fn index_mut(&mut self, index: RangeTo<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<usize> for Hand

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<'a> IntoIterator for &'a Hand

§

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 Hand

§

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 Hand

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 Hand

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 Hand

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 Hand

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 Copy for Hand

source§

impl Eq for Hand

Auto Trait Implementations§

§

impl Freeze for Hand

§

impl RefUnwindSafe for Hand

§

impl Send for Hand

§

impl Sync for Hand

§

impl Unpin for Hand

§

impl UnwindSafe for Hand

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: Copy,

source§

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> 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>,

source§

impl<T> RuleType for T
where T: Copy + Debug + Eq + Hash + Ord,