Split main into module files.
/*** 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<Card>,
}
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<VecDeque<Card>> {
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<Card> {
// 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()
}
}