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