Added a basic deck

This commit is contained in:
2025-02-24 23:17:53 +00:00
parent 6f5d7d7361
commit 387c3f0beb
3 changed files with 70 additions and 3 deletions

33
card_stuffs/Cargo.lock generated
View File

@@ -13,9 +13,17 @@ name = "card_stuffs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"strum",
"strum_macros",
"thiserror", "thiserror",
] ]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.93" version = "1.0.93"
@@ -34,6 +42,31 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rustversion"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4"
[[package]]
name = "strum"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f64def088c51c9510a8579e3c5d67c65349dcf755e5479ad3d010aa6454e2c32"
[[package]]
name = "strum_macros"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c77a8c5abcaf0f9ce05d62342b7d298c346515365c36b673df4ebe3ced01fde8"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.98" version = "2.0.98"

View File

@@ -5,4 +5,6 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.96" anyhow = "1.0.96"
strum = "0.27.1"
strum_macros = "0.27.1"
thiserror = "2.0.11" thiserror = "2.0.11"

View File

@@ -1,8 +1,9 @@
use thiserror::Error; use thiserror::Error;
use anyhow::Result; use strum::IntoEnumIterator;
use strum_macros::EnumIter;
use std::fmt; use std::fmt;
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug, EnumIter, Copy, Clone)]
pub enum Suit { pub enum Suit {
Heart, Heart,
Diamond, Diamond,
@@ -38,7 +39,7 @@ impl fmt::Display for Suit {
} }
} }
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug, EnumIter, Copy, Clone)]
pub enum Value { pub enum Value {
Ace, Ace,
Two, Two,
@@ -133,6 +134,30 @@ impl Card {
} }
} }
#[derive(Debug)]
pub struct Deck {
pub cards: Vec<Card>
}
impl Default for Deck {
fn default() -> Deck {
let mut array = Vec::new();
for suit in Suit::iter() {
for value in Value::iter() {
array.push(
Card {
suit: suit,
value: value,
}
);
}
}
Deck {
cards: array,
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@@ -173,4 +198,11 @@ mod tests {
assert!(false, "Cards are not adjacent - should be an error") assert!(false, "Cards are not adjacent - should be an error")
} }
} }
#[test]
fn get_a_whole_deck() {
let d = Deck::default();
assert_eq!(d.cards.len(), 52); // Probably should test whether all cards are in... eh
println!("{:#?}", d);
}
} }