small_card_deck/
lib.rs

1// - This file contains the deck, card, and suit types
2
3use rand::seq::SliceRandom;
4use std::fmt::Display;
5
6#[derive(Debug, Clone, PartialEq)]
7pub enum Suit {
8    Hearts,
9    Diamonds,
10    Clubs,
11    Spades,
12}
13
14impl Suit {
15    fn to_string(&self) -> String {
16        match self {
17            Suit::Hearts => "♥".to_string(),
18            Suit::Diamonds => "♦".to_string(),
19            Suit::Clubs => "♣".to_string(),
20            Suit::Spades => "♠".to_string(),
21        }
22    }
23}
24
25#[derive(Debug, Clone, PartialEq)]
26pub enum Value {
27    Ace,
28    Two,
29    Three,
30    Four,
31    Five,
32    Six,
33    Seven,
34    Eight,
35    Nine,
36    Ten,
37    Jack,
38    Queen,
39    King,
40}
41
42impl Value {
43    fn to_string(&self) -> String {
44        match self {
45            Value::Ace => "A".to_string(),
46            Value::Two => "2".to_string(),
47            Value::Three => "3".to_string(),
48            Value::Four => "4".to_string(),
49            Value::Five => "5".to_string(),
50            Value::Six => "6".to_string(),
51            Value::Seven => "7".to_string(),
52            Value::Eight => "8".to_string(),
53            Value::Nine => "9".to_string(),
54            Value::Ten => "10".to_string(),
55            Value::Jack => "J".to_string(),
56            Value::Queen => "Q".to_string(),
57            Value::King => "K".to_string(),
58        }
59    }
60}
61
62impl Display for Value {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        write!(f, "{}", self.to_string())
65    }
66}
67
68#[derive(Debug, Clone)]
69pub struct Card {
70    suit: Suit,
71    value: Value,
72}
73
74impl PartialEq for Card {
75    fn eq(&self, other: &Self) -> bool {
76        self.suit == other.suit && self.value == other.value
77    }
78}
79
80impl Card {
81    fn new(suit: Suit, value: Value) -> Self {
82        Card { suit, value }
83    }
84
85    fn to_string(&self) -> String {
86        format!("{}{}", self.value.to_string(), self.suit.to_string())
87    }
88}
89
90impl Display for Card {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        write!(f, "{}", self.to_string())
93    }
94}
95
96#[derive(Debug, Clone, Default)]
97pub struct Deck {
98    cards: Vec<Card>,
99}
100
101impl Deck {
102    pub fn new() -> Self {
103        let mut cards = Vec::new();
104        let suits = vec![Suit::Hearts, Suit::Diamonds, Suit::Clubs, Suit::Spades];
105        let values = vec![
106            Value::Ace,
107            Value::Two,
108            Value::Three,
109            Value::Four,
110            Value::Five,
111            Value::Six,
112            Value::Seven,
113            Value::Eight,
114            Value::Nine,
115            Value::Ten,
116            Value::Jack,
117            Value::Queen,
118            Value::King,
119        ];
120
121        for suit in suits.iter() {
122            for value in values.iter() {
123                cards.push(Card::new(suit.clone(), value.clone()));
124            }
125        }
126
127        Deck { cards }
128    }
129
130    pub fn shuffle(&mut self) {
131        self.cards.shuffle(&mut rand::thread_rng());
132    }
133
134    pub fn peek(&self) -> Option<&Card> {
135        self.cards.first()
136    }
137}
138
139impl Display for Deck {
140    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
141        for card in self.cards.iter() {
142            write!(f, "{} ", card)?;
143        }
144        Ok(())
145    }
146}