src/row.rs
changeset 5 0dd7f2c9fd81
parent 4 a2f0cb2b5c13
equal deleted inserted replaced
4:a2f0cb2b5c13 5:0dd7f2c9fd81
     1 /*** Row ****/
     1 /// Implementation of a Row of Cards on the Table (max 5)
     2 // a row of cards on the table (max 5)
       
     3 
     2 
       
     3 // We use std::mem::take to do tricks with vector members and ownership.
     4 use std::{collections::VecDeque, mem};
     4 use std::{collections::VecDeque, mem};
     5 
     5 
     6 use crate::card::Card;
     6 use crate::card::Card;
     7 
     7 
     8 #[derive(Debug)]
     8 #[derive(Debug)]
       
     9 /// A `Row` of [`Card`]s, in a two-ended vector (`VecDeque`).
       
    10 /// A Row is smart and can do a few things on herself.
     9 pub(crate) struct Row {
    11 pub(crate) struct Row {
    10     cards: VecDeque<Card>,
    12     cards: VecDeque<Card>,
    11 }
    13 }
    12 
    14 
    13 impl Row {
    15 impl Row {
       
    16     /// The maximum length of a Row. More cards cause overflow and force collection.
    14     const MAX_LEN: usize = 5;
    17     const MAX_LEN: usize = 5;
    15 
    18 
       
    19     /// Create a new [`Row`] with capacity of 5 [`Card`]s.
    16     pub(crate) fn new() -> Self {
    20     pub(crate) fn new() -> Self {
    17         Row {
    21         Row {
    18             cards: VecDeque::with_capacity(5),
    22             cards: VecDeque::with_capacity(5),
    19         }
    23         }
    20     }
    24     }
    21 
    25 
       
    26     /// Push a [`Card`] to the end of the Row.
       
    27     /// If it would make Row over maximum length the function 
       
    28     /// returns the content of the row, and then push the new
       
    29     /// Card as the new Row head.
       
    30     /// Otherwise return None.
       
    31     /// # Arguments
       
    32     /// - [`Card`] - the new card to put into the Row
       
    33     /// # Returns
       
    34     /// - `Option<VecDequeue<Card>>` - None or Some(cards) to be collected from Row
    22     pub(crate) fn push_or_collect( &mut self, card: Card ) -> Option<VecDeque<Card>> {
    35     pub(crate) fn push_or_collect( &mut self, card: Card ) -> Option<VecDeque<Card>> {
    23         trace!("Called push_or_collect on row {:?}", &self);
    36         trace!("Called push_or_collect on row {:?}", &self);
    24         if self.cards.len() < Self::MAX_LEN {
    37         if self.cards.len() < Self::MAX_LEN {
    25             trace!("Less than {} cards, putting at the end", Self::MAX_LEN);
    38             trace!("Less than {} cards, putting at the end", Self::MAX_LEN);
    26             self.cards.push_back(card);
    39             self.cards.push_back(card);
    27             None
    40             None
    28 
    41 
    29         } else {
    42         } else {
    30             trace!("Row is full, len {}, maxlen {}", self.cards.len(), Self::MAX_LEN);
    43             trace!("Row is full, len {}, maxlen {}", self.cards.len(), Self::MAX_LEN);
    31             // row overflow
    44             // Row overflow. We take out `cards` from `self` with its ownership and 
       
    45             // leave the default (empty VecDeque) in its place, without disturbing
       
    46             // `self` ownership.
    32             let row_cards = mem::take( &mut self.cards );
    47             let row_cards = mem::take( &mut self.cards );
       
    48             // We put new card as new Row head.
    33             self.cards.push_back(card);
    49             self.cards.push_back(card);
    34             if self.cards.len() != 1 {
    50             if self.cards.len() != 1 {
    35                 panic!("New row must have one card, not {}", self.cards.len());
    51                 panic!("New row must have one card, not {}", self.cards.len());
    36             }
    52             }
       
    53             // Return the collected old Row content
    37             Some(row_cards)
    54             Some(row_cards)
    38         }
    55         }
    39     }
    56     }
    40 
    57 
       
    58     /// Take out all `cards` (and their ownership) and return them.
    41     pub(crate) fn take_row( &mut self ) -> VecDeque<Card> {
    59     pub(crate) fn take_row( &mut self ) -> VecDeque<Card> {
    42         // take cards and empty the row
    60         // take cards and empty the row
    43         mem::take( &mut self.cards )
    61         mem::take( &mut self.cards )
    44     }
    62     }
    45 
    63 
       
    64     /// Return the `value` of the last card in the Row.
       
    65     /// This is the largest value since the Row is always ordered.
    46     pub(crate) fn last_card_value(&self) -> i8 {
    66     pub(crate) fn last_card_value(&self) -> i8 {
    47         // println!("last_card_value: cards {:?}, len {}", self.cards, self.cards.len());
    67         // println!("last_card_value: cards {:?}, len {}", self.cards, self.cards.len());
    48         self.cards.get( self.cards.len()-1 ).unwrap().value
    68         self.cards.get( self.cards.len()-1 ).unwrap().value
    49     }
    69     }
    50 
    70 
    55             sum += card.points as i32;
    75             sum += card.points as i32;
    56         });
    76         });
    57         sum
    77         sum
    58     }
    78     }
    59 
    79 
       
    80     #[allow(dead_code)]
       
    81     /// We could use this, but... borrow checker, you know. Needs more love.
       
    82     /// I could have used `_len` to shut up the dead_code warning, so this is
       
    83     /// another example to do it the other way.
    60     pub fn len(&self) -> usize {
    84     pub fn len(&self) -> usize {
    61         self.cards.len()
    85         self.cards.len()
    62     }
    86     }
    63 }
    87 }