src/table.rs
author Peter Gervai <grin@grin.hu>
Sat, 04 Feb 2023 22:57:08 +0100
changeset 7 c2b77cc8664c
parent 5 0dd7f2c9fd81
permissions -rw-r--r--
Update version to 1.00
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
/// Implemantation of the playing Table.
4
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)]
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    10
/// A playing Table, containing 5 [`Row`]s and some [`PlayerCard`]s.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    11
/// A Table can do some useful function with its cards, like picking
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    12
/// closest row or moving cards.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    13
pub(crate) struct Table {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    14
    rows: Vec<Row>,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    15
    player_cards: VecDeque<PlayerCard>, // owned by a player
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    16
}
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    17
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    18
impl Table {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    19
    /// Creates a new `Table`, filling Rows from `row_cards`.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    20
    pub(crate) fn new(row_cards: VecDeque<Card>) -> Self {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    21
        // We have exactly 5 Rows.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
        let mut rows = Vec::with_capacity(5);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    23
        for card in row_cards {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    24
            // create a new row then put a card into it
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    25
            let mut row = Row::new();
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    26
            if let Some(_) = row.push_or_collect(card) {
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    27
                // Row should have one card, so... mustn't happen.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    28
                panic!("Freshly created row overflowed");
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    29
            }
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    30
            // Put the new row onto the Table.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
            rows.push( row );
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
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    34
        // And return the newly created Table.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
        Table {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
            rows,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
            player_cards: VecDeque::new(),
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
        }
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
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    41
    /// Gets a [`Card`] from a [`Player`] and put it into the `player_cards` area,
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    42
    /// remembering whose (`player_id`) card it was.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
    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
    44
        self.player_cards.push_back( PlayerCard { player_id, card } );
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    47
    /// Sort the [`Card`]s thrown by the [`Player`]s, since we'd need
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    48
    /// to get them ordered by value later.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
    pub(crate) fn sort_cards( &mut self ) {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    50
        // Sorting a normal VecDeque is not possible since it may contain
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    51
        // holes, so we need to de-hole it first, then it's sortable (through
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    52
        // a returned pointer slice).
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
        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
    54
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    56
    /// Returns true if we have unprocessed Player cards on the table.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
    pub(crate) fn has_player_cards( &self ) -> bool {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
        self.player_cards.len() > 0
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    61
    /// Return the smallest player card on the table.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    62
    /// FIXME: shall check whether it's ordered.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    63
    pub(crate) fn get_smallest_player_card( &mut self ) -> PlayerCard {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    64
        // FIXME: check orderedness!
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    65
        // FIXME: check asking when empty!
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    66
        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
    67
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    69
    /// Return the row which is closest to the `pcard` arg, or None if
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    70
    /// all Row tails are larger.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
    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
    72
        // get the row id with last card closest smaller to players'
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    73
        let row_tails = self.get_row_tails();
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    74
        let mut closest_val = None;
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    75
        let mut diff = 127; // larger than any
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    76
        // Check all the row tail cards
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    77
        for i in 0..row_tails.len() {
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    78
            if row_tails[i] < pcard.card.value && pcard.card.value - row_tails[i] < diff {
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    79
                // it is smaller than pcard and closer than the old closest one: match!
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    80
                // Store the row index
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    81
                closest_val = Some(i); 
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    82
                diff = pcard.card.value - row_tails[i];
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    83
                // debug!("DEBUG: pcard {}, row {}, head {}, diff {}, closest {:?}", pcard.card.value, i, row_heads[i], diff, closest_val);
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
            }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
        }
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
        closest_val
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    90
    /// Put a [`Card`] into the `row_id` Row (tail).
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    91
    /// Returns `None` if ok or `Some(cards)` when the Row is full.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    92
    pub(crate) fn put_card_into_row( &mut self, pcard: PlayerCard, row_id: usize ) -> Option<VecDeque<Card>> {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    93
        // We actually ask the Row to do it properly.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    94
        self.rows[row_id].push_or_collect(pcard.card)
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    96
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    97
    pub(crate) fn get_row_tails( &self ) -> Vec<i8> {
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    98
        let mut heads: Vec<i8> = Vec::new();
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    99
        for i in 0..self.rows.len() {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   100
            heads.push( self.rows[i].last_card_value() );
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   101
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   102
        heads
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   103
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   104
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   105
    // take a whole row and hand it over
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   106
    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
   107
        self.rows[row_id].take_row()
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   108
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   109
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   110
    // 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
   111
    pub(crate) fn collect_rows( &mut self ) -> VecDeque<Card> {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   112
        let mut cards = VecDeque::new();
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   113
        for row in 0..self.rows.len() {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   114
            self.rows[row]
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   115
                .take_row()
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   116
                .into_iter()
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   117
                .for_each(|card| cards.push_back(card));
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   118
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   119
        cards
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   120
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   121
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   122
    /// Return a non-mutable borrow of the rows to look at
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   123
    pub fn peek_rows(&self) -> &Vec<Row> {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   124
        &self.rows
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   125
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   126
}
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   127