src/deck.rs
changeset 5 0dd7f2c9fd81
parent 4 a2f0cb2b5c13
equal deleted inserted replaced
4:a2f0cb2b5c13 5:0dd7f2c9fd81
     1 /*** Deck ****/
     1 /// This is the implementation of a Deck of cards.
       
     2 /// There are two basic kinds:
       
     3 /// 1. The Game Deck, automatically generated 104 cards.
       
     4 /// 2. Any Deck-type pile, like the hand of a Player.
       
     5 /// Descks can be shuffled, cards pushed to bottom and
       
     6 /// pulled from top. Maybe others, too.
       
     7 /// 
       
     8 
     2 use std::collections::VecDeque;
     9 use std::collections::VecDeque;
     3 
       
     4 use rand::Rng;
    10 use rand::Rng;
     5 
    11 
       
    12 /// We reference [`Card`]s
     6 use crate::card::Card;
    13 use crate::card::Card;
     7 
    14 
     8 #[derive(Debug)]
    15 #[derive(Debug)]
       
    16 /// A Deck is an ordered two-ended vector (`VecDeque`) of [`Card`]s.
     9 pub(crate) struct Deck {
    17 pub(crate) struct Deck {
    10     content: VecDeque<Card>,
    18     content: VecDeque<Card>,
    11 }
    19 }
    12 
    20 
    13 impl Deck {
    21 impl Deck {
    37         // shufflers:
    45         // shufflers:
    38         // * naive: swap cards n times
    46         // * naive: swap cards n times
    39         // * kgb: half the deck, take 1..4 cards sequentially from each
    47         // * kgb: half the deck, take 1..4 cards sequentially from each
    40         // * grin: take 1..6 from front and put at bottom
    48         // * grin: take 1..6 from front and put at bottom
    41 
    49 
    42         // naive shuffle: exchange random cards
    50         // naive shuffle: exchange <n> random cards
    43         for _i in 1..=500 {
    51         for _ in 1..=500 {
       
    52             // Generate two random positions of cards in Deck
    44             let c1 = rng.gen_range(0 .. self.content.len());
    53             let c1 = rng.gen_range(0 .. self.content.len());
    45             let c2 = rng.gen_range(0 .. self.content.len());
    54             let c2 = rng.gen_range(0 .. self.content.len());
       
    55             // If it's not the same card we swap them in the `content` vector.
       
    56             // The `swap()` funtion handles ownership and does not angry 
       
    57             // the borrow checker.
    46             if c1 != c2 {
    58             if c1 != c2 {
    47                 self.content.swap(c1, c2);
    59                 self.content.swap(c1, c2);
    48             }
    60             }
    49         }
    61         }
    50         trace!("Deck after shuffle: len {}, {:?}", self.content.len(), self);
    62         trace!("Deck after shuffle: len {}, {:?}", self.content.len(), self);
    70     /// Returns the length of this [`Deck`].
    82     /// Returns the length of this [`Deck`].
    71     pub(crate) fn len( &self ) -> usize {
    83     pub(crate) fn len( &self ) -> usize {
    72         self.content.len()
    84         self.content.len()
    73     }
    85     }
    74 
    86 
       
    87     /// Take a card from inside of the Deck.
       
    88     /// Using an illegal index will `panic!()`, so don't.
    75     fn _get_nth( &mut self, n: usize ) -> Card {
    89     fn _get_nth( &mut self, n: usize ) -> Card {
    76         if let Some(c) = self.content.remove(n) {
    90         if let Some(c) = self.content.remove(n) {
    77             c
    91             c
    78         } else {
    92         } else {
    79             panic!("get_nth: index {} out of bounds ({})!", n, self.content.len());
    93             panic!("get_nth: index {} out of bounds ({})!", n, self.content.len());