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