#[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
impl Hand
sourcepub fn convert_decktype(&self, t: DeckType) -> Hand
pub fn convert_decktype(&self, t: DeckType) -> Hand
Clone the hand, change its deck type, and fix the aces
sourcepub fn new_by_name(dname: &str) -> Hand
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");
sourcepub fn init<I>(self, iter: I) -> Selfwhere
I: IntoIterator<Item = Card>,
pub fn init<I>(self, iter: I) -> Selfwhere
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"));
sourcepub fn sorted(self) -> Self
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");
sourcepub fn deck_type(&self) -> DeckType
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);
sourcepub fn len(&self) -> usize
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);
sourcepub fn is_empty(&self) -> bool
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());
sourcepub fn is_not_empty(&self) -> bool
pub fn is_not_empty(&self) -> bool
Is the hand not empty?
sourcepub fn clear(&mut self)
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());
sourcepub fn index_of(&self, card: Card) -> Option<usize>
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);
sourcepub fn contains(&self, card: Card) -> bool
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));
sourcepub fn card_at(&self, index: usize) -> Option<Card>
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);
sourcepub fn set_card_at(&mut self, index: usize, card: Card) -> bool
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);
sourcepub fn push(&mut self, card: Card) -> bool
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");
sourcepub fn pop(&mut self) -> Option<Card>
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");
sourcepub fn push_n<I>(&mut self, n: usize, iter: I) -> usizewhere
I: IntoIterator<Item = Card>,
pub fn push_n<I>(&mut self, n: usize, iter: I) -> usizewhere
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)));
sourcepub fn push_all<I>(&mut self, iter: I) -> usizewhere
I: IntoIterator<Item = Card>,
pub fn push_all<I>(&mut self, iter: I) -> usizewhere
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());
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 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);
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 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);
sourcepub fn set<I>(&mut self, iter: I) -> boolwhere
I: IntoIterator<Item = Card>,
pub fn set<I>(&mut self, iter: I) -> boolwhere
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");
sourcepub fn insert_at(&mut self, index: usize, card: Card) -> bool
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");
sourcepub fn remove_at(&mut self, index: usize) -> Option<Card>
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");
sourcepub fn remove_card(&mut self, card: Card) -> bool
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");
sourcepub fn truncate(&mut self, n: usize)
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");
sourcepub fn sort(&mut self)
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");
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 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);
sourcepub fn equals(&self, other: &Self) -> bool
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));
sourcepub fn is_equivalent_to(&self, other: &Self) -> bool
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));
sourcepub fn ace_fix(&mut self)
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);
sourcepub fn discard(&mut self, indices: &[usize]) -> bool
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");
sourcepub fn sequence(&self) -> Rank
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);
sourcepub fn blackjack_total(&self) -> (bool, u32)
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));
Trait Implementations§
source§impl<'de> Deserialize<'de> for Hand
impl<'de> Deserialize<'de> for Hand
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 Hand
impl<'a> IntoIterator for &'a Hand
source§impl IntoIterator for Hand
impl IntoIterator for Hand
source§impl Ord for Hand
impl Ord for Hand
source§impl PartialEq for Hand
impl PartialEq for Hand
source§impl PartialOrd for Hand
impl PartialOrd for Hand
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 Copy for Hand
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> 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: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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
)