types: played_cards is now HashMap<Color, Digit> instead of <Color, Card>

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-08-20 11:51:54 +02:00
parent fe40316b2c
commit 674e5549c2
Signed by: pep
GPG Key ID: DEDA74AEECA9D0F2
1 changed files with 12 additions and 18 deletions

View File

@ -47,13 +47,13 @@ impl fmt::Display for Variant {
}
/// Digit values a card can take
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum Digit {
One,
Two,
Three,
Four,
Five,
One = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
}
impl FromStr for Digit {
@ -90,18 +90,12 @@ impl fmt::Display for Digit {
impl From<Digit> for u8 {
fn from(digit: Digit) -> u8 {
match digit {
Digit::One => 1,
Digit::Two => 2,
Digit::Three => 3,
Digit::Four => 4,
Digit::Five => 5,
}
digit as u8
}
}
/// Colors a card can take
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum Color {
Blue,
Green,
@ -311,7 +305,7 @@ pub struct GameState {
/// Discard pile
discarded_cards: Vec<Card>,
/// Cards played on the board
played_cards: HashMap<Color, Card>,
played_cards: HashMap<Color, Digit>,
}
impl GameState {
@ -374,8 +368,8 @@ impl GameState {
// Ranks
self.colors.iter().enumerate().for_each(|(i, color)| {
if let Some(card) = self.played_cards.get(color) {
print!("{} ", card);
if let Some(rank) = self.played_cards.get(color) {
print!("{} ", rank);
if i == self.colors.len() - 1 {
println!();
}
@ -403,7 +397,7 @@ impl GameState {
// Drawing deck, number of cards remaining
let total = {
let played = self.colors.iter().fold(0u8, |res, color| {
res + self.played_cards.get(color).map(|card| u8::from(card.digit.clone())).unwrap_or(0)
res + self.played_cards.get(color).map(|digit| u8::from(*digit)).unwrap_or(0)
});
let in_hands = self.players * self.hand_capacity;
self.total_cards - in_hands - played - u8::try_from(self.discarded_cards.len()).unwrap()