use rand::prelude::SliceRandom; use rand::Rng; use std::fmt; use strum::IntoEnumIterator; use strum_macros::EnumIter; #[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter)] pub enum Suit { Club, Diamond, Heart, Spade, } #[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, EnumIter)] pub enum Rank { Two = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace, } impl fmt::Display for Suit { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { f.write_str(match self { Suit::Club => "♣", Suit::Diamond => "♢", Suit::Heart => "♡", Suit::Spade => "♠", }) } } impl fmt::Debug for Suit { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { write!(f, "{}", self) } } impl fmt::Display for Rank { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { f.write_str(match self { Rank::Ace => "A", Rank::King => "K", Rank::Queen => "Q", Rank::Jack => "J", Rank::Ten => "10", Rank::Nine => "9", Rank::Eight => "8", Rank::Seven => "7", Rank::Six => "6", Rank::Five => "5", Rank::Four => "4", Rank::Three => "3", Rank::Two => "2", }) } } impl fmt::Debug for Rank { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { write!(f, "{}", self) } } #[derive(PartialEq, Eq, Clone, Copy)] pub struct Card(pub Suit, pub Rank); impl fmt::Display for Card { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { let Card(suit, rank) = self; write!(f, "{}{}", suit, rank) } } impl fmt::Debug for Card { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { write!(f, "{}", self) } } fn make_deck() -> Vec { let mut result = vec![]; for suit in Suit::iter() { for rank in Rank::iter() { result.push(Card(suit, rank)); } } result } pub fn shuffle_deck(rng: &mut R) -> (Vec, Vec, Vec, Vec) where R: Rng, { let mut deck = make_deck(); deck.shuffle(rng); let mut deck = deck.iter(); let n = deck.by_ref().take(13).cloned().collect(); let w = deck.by_ref().take(13).cloned().collect(); let s = deck.by_ref().take(13).cloned().collect(); let e = deck.by_ref().take(13).cloned().collect(); (n, w, s, e) }