pub struct Deck { /* private fields */ }Expand description
The Deck represents a deck of zero or more cards. A default deck is 52 playing cards.
Internally the deck consists of two stacks consisting of dealt and undealt cards. The dealt stack
receives cards as they are dealt from the undealt stack.
The deck may be reset() to return it to its original state. A deck may be shuffle()’d to randomize
its order. Shuffling uses a Knuth shuffle.
A deck can contain more than one card with the same rank / suit combination although by default it does not.
A deck cannot have more cards added or removed to it once it is created.
Implementations§
Source§impl Deck
impl Deck
Sourcepub fn new() -> Deck
pub fn new() -> Deck
Creates a new Deck containing the standard set of 52 cards
Examples found in repository?
3fn main() {
4 let mut deck = Deck::new();
5
6 // Shuffle the deck
7 deck.shuffle();
8
9 // Deal a card
10 for _ in 0..10 {
11 if let Ok(card) = deck.deal_one() {
12 println!("You dealt a {}", card.name());
13 } else {
14 panic!("We should have enough cards for this not to happen")
15 }
16 }
17
18 // Put dealt cards back onto the deck
19 deck.reset();
20}Sourcepub fn from_cards(cards: &[Card]) -> Deck
pub fn from_cards(cards: &[Card]) -> Deck
Creates a new Deck containing the specified cards
Sourcepub fn undealt_count(&self) -> usize
pub fn undealt_count(&self) -> usize
Returns the number of remaining undealt cards in the Deck
Sourcepub fn dealt_count(&self) -> usize
pub fn dealt_count(&self) -> usize
Returns the number of dealt cards in the Deck
Sourcepub fn dealt_cards(&self) -> &[Card]
pub fn dealt_cards(&self) -> &[Card]
Returns the collection of dealt cards
Sourcepub fn top_card(&self) -> Option<Card>
pub fn top_card(&self) -> Option<Card>
Tells you the top card (very next to be drawn) in the undealt deck without dealing it.
Sourcepub fn bottom_card(&self) -> Option<Card>
pub fn bottom_card(&self) -> Option<Card>
Tells you the bottom card (very last to be drawn) in the undealt deck without dealing it.
Sourcepub fn deal_one(&mut self) -> Result<Card, &'static str>
pub fn deal_one(&mut self) -> Result<Card, &'static str>
Deals the card from the undealt pile. If there are no cards left, the function will return an error.
Examples found in repository?
3fn main() {
4 let mut deck = Deck::new();
5
6 // Shuffle the deck
7 deck.shuffle();
8
9 // Deal a card
10 for _ in 0..10 {
11 if let Ok(card) = deck.deal_one() {
12 println!("You dealt a {}", card.name());
13 } else {
14 panic!("We should have enough cards for this not to happen")
15 }
16 }
17
18 // Put dealt cards back onto the deck
19 deck.reset();
20}Sourcepub fn deal(&mut self, numcards: usize) -> Vec<Card>
pub fn deal(&mut self, numcards: usize) -> Vec<Card>
Deals one or more card from the undealt pile and returns them as an array.
Sourcepub fn deal_to_hand(&mut self, hand: &mut Hand, numcards: usize) -> usize
pub fn deal_to_hand(&mut self, hand: &mut Hand, numcards: usize) -> usize
Deals one or more card straight to the Hand. Returns the number of cards dealt.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Return the dealt cards back to the end of the undealt pile. Order is preserved according to the default order or the last shuffle.
Examples found in repository?
3fn main() {
4 let mut deck = Deck::new();
5
6 // Shuffle the deck
7 deck.shuffle();
8
9 // Deal a card
10 for _ in 0..10 {
11 if let Ok(card) = deck.deal_one() {
12 println!("You dealt a {}", card.name());
13 } else {
14 panic!("We should have enough cards for this not to happen")
15 }
16 }
17
18 // Put dealt cards back onto the deck
19 deck.reset();
20}Sourcepub fn reset_shuffle(&mut self)
pub fn reset_shuffle(&mut self)
Resets and shuffles the deck