From 674e5549c284dff2939351f1a8d166234d6031bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 20 Aug 2022 11:51:54 +0200 Subject: [PATCH] types: played_cards is now HashMap instead of MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/types.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/types.rs b/src/types.rs index 3e45480..c10936f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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 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, /// Cards played on the board - played_cards: HashMap, + played_cards: HashMap, } 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()