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