70 lines
1.4 KiB
Rust
70 lines
1.4 KiB
Rust
use std::fmt;
|
|
|
|
#[derive(Clone)]
|
|
pub enum Suit {
|
|
Club,
|
|
Diamond,
|
|
Heart,
|
|
Spade,
|
|
}
|
|
|
|
impl fmt::Display for Suit {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
let c = match self {
|
|
Suit::Club => "♣",
|
|
Suit::Diamond => "♦",
|
|
Suit::Heart => "♥",
|
|
Suit::Spade => "♠",
|
|
};
|
|
write!(f, "{c}")
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Card {
|
|
pub suit: Suit,
|
|
pub value: String,
|
|
}
|
|
|
|
impl Card {
|
|
pub fn new(suit: Suit, value: &str) -> Self {
|
|
Self {
|
|
suit,
|
|
value: value.to_owned(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<&Card> for u8 {
|
|
fn from(card: &Card) -> u8 {
|
|
match card.value.as_ref() {
|
|
"A" => 11,
|
|
"K" => 10,
|
|
"Q" => 10,
|
|
"J" => 10,
|
|
x => x.parse().unwrap(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Card {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{}{}", self.suit, self.value)
|
|
}
|
|
}
|
|
|
|
/// Create a new deck of cards, without jokers
|
|
pub(crate) fn deck_without_jokers() -> Vec<Card> {
|
|
let mut deck = Vec::new();
|
|
|
|
for suit in [Suit::Club, Suit::Diamond, Suit::Heart, Suit::Spade] {
|
|
for num_card in 2..=10 {
|
|
deck.push(Card::new(suit.clone(), &(num_card.to_string())))
|
|
}
|
|
for face_card in ["J", "Q", "K", "A"] {
|
|
deck.push(Card::new(suit.clone(), face_card))
|
|
}
|
|
}
|
|
|
|
deck
|
|
}
|