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

View file

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