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()