initial commit - basic TUI blackjack game
This commit is contained in:
commit
0a72da768e
9 changed files with 738 additions and 0 deletions
70
src/card.rs
Normal file
70
src/card.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue