src/table.rs
author Peter Gervai <grin@grin.hu>
Tue, 31 Jan 2023 23:25:50 +0100
changeset 4 a2f0cb2b5c13
child 5 0dd7f2c9fd81
permissions -rw-r--r--
Split main into module files.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     1
/*** Table ****/
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     2
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     3
use std::collections::VecDeque;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     5
use crate::card::Card;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     6
use crate::row::Row;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     7
use crate::player::PlayerCard;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     8
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     9
#[derive(Debug)]
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    10
pub(crate) struct Table {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    11
    rows: Vec<Row>,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    12
    player_cards: VecDeque<PlayerCard>, // owned by a player
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 Table {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    16
    pub(crate) fn new(row_cards: VecDeque<Card>) -> Self {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    17
        let mut rows = Vec::with_capacity(5);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    18
        for card in row_cards {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    19
            // create a new row then put a card into it
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    20
            let mut row = Row::new();
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    21
            if let Some(_c) = row.push_or_collect(card) {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
                panic!("Freshly created row overflowed");
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
            rows.push( row );
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    25
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    26
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    27
        Table {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    28
            rows,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    29
            player_cards: VecDeque::new(),
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    32
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
    pub(crate) fn lay_player_card( &mut self, card: Card, player_id: i32 ) {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
        self.player_cards.push_back( PlayerCard { player_id, card } );
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
    pub(crate) fn sort_cards( &mut self ) {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
        self.player_cards.make_contiguous().sort_by( |a,b| b.card.cmp(&a.card) );
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    39
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    40
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    41
    pub(crate) fn has_player_cards( &self ) -> bool {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    42
        self.player_cards.len() > 0
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
    pub(crate) fn get_smallest_player_card( &mut self ) -> PlayerCard {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
        // FIXME: check!
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    47
        self.player_cards.pop_back().expect("out of player cards on table")
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    50
    pub(crate) fn get_closest_row( &self, pcard: &PlayerCard ) -> Option<usize> {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    51
        // get the row id with last card closest smaller to players'
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    52
        let row_heads = self.get_row_heads();
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
        let mut closest_val = None;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    54
        let mut diff = 127;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
        for i in 0..row_heads.len() {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    56
            if row_heads[i] < pcard.card.value && pcard.card.value - row_heads[i] < diff {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
                closest_val = Some(i);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
                diff = pcard.card.value - row_heads[i];
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
                // println!("DEBUG: pcard {}, row {}, head {}, diff {}, closest {:?}", pcard.card.value, i, row_heads[i], diff, closest_val);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
            }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    61
        }
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
        closest_val
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    64
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    65
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    66
    pub(crate) fn put_card_into_row( &mut self, pcard: PlayerCard, row_id: usize ) -> Option<VecDeque<Card>> {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    67
        self.rows[row_id as usize].push_or_collect(pcard.card)
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
    }
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
    pub(crate) fn get_row_heads( &self ) -> Vec<i8> {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
        let mut heads: Vec<i8> = Vec::new();
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    72
        for i in 0..self.rows.len() {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    73
            heads.push( self.rows[i].last_card_value() );
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    74
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    75
        heads
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
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    78
    // take a whole row and hand it over
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    79
    pub(crate) fn take_row( &mut self, row_id: usize ) -> VecDeque<Card> {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    80
        self.rows[row_id].take_row()
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    81
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    82
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    83
    // collect remaining cards in the rows at the end of round
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
    pub(crate) fn collect_rows( &mut self ) -> VecDeque<Card> {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
        let mut cards = VecDeque::new();
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    86
        for row in 0..self.rows.len() {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    87
            self.rows[row]
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
                .take_row()
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
                .into_iter()
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    90
                .for_each(|card| cards.push_back(card));
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    91
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    92
        cards
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    93
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    94
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
    pub fn peek_rows(&self) -> &Vec<Row> {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    96
        &self.rows
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    97
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    98
}
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    99