diff -r 3d9dba5b16e0 -r a2f0cb2b5c13 src/row.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/row.rs Tue Jan 31 23:25:50 2023 +0100 @@ -0,0 +1,63 @@ +/*** Row ****/ +// a row of cards on the table (max 5) + +use std::{collections::VecDeque, mem}; + +use crate::card::Card; + +#[derive(Debug)] +pub(crate) struct Row { + cards: VecDeque, +} + +impl Row { + const MAX_LEN: usize = 5; + + pub(crate) fn new() -> Self { + Row { + cards: VecDeque::with_capacity(5), + } + } + + pub(crate) fn push_or_collect( &mut self, card: Card ) -> Option> { + trace!("Called push_or_collect on row {:?}", &self); + if self.cards.len() < Self::MAX_LEN { + trace!("Less than {} cards, putting at the end", Self::MAX_LEN); + self.cards.push_back(card); + None + + } else { + trace!("Row is full, len {}, maxlen {}", self.cards.len(), Self::MAX_LEN); + // row overflow + let row_cards = mem::take( &mut self.cards ); + self.cards.push_back(card); + if self.cards.len() != 1 { + panic!("New row must have one card, not {}", self.cards.len()); + } + Some(row_cards) + } + } + + pub(crate) fn take_row( &mut self ) -> VecDeque { + // take cards and empty the row + mem::take( &mut self.cards ) + } + + pub(crate) fn last_card_value(&self) -> i8 { + // println!("last_card_value: cards {:?}, len {}", self.cards, self.cards.len()); + self.cards.get( self.cards.len()-1 ).unwrap().value + } + + // sum of row card points + pub(crate) fn sum(&self) -> i32 { + let mut sum: i32 = 0; + self.cards.iter().for_each(|card| { + sum += card.points as i32; + }); + sum + } + + pub fn len(&self) -> usize { + self.cards.len() + } +}