From 09edadc8223f0902d671c8396d10d86f2bd3f2ea Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Fri, 15 May 2026 06:35:01 -0700 Subject: [PATCH] card game stuff --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + src/card_game.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/klondike.rs | 6 ++++++ 4 files changed, 64 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index cf83f94..e373348 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ name = "card_game" version = "0.1.0" dependencies = [ "deranged", + "rand", ] [[package]] @@ -14,3 +15,18 @@ name = "deranged" version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c" + +[[package]] +name = "rand" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2e8e8bcc7961af1fdac401278c6a831614941f6164ee3bf4ce61b7edb162207" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" diff --git a/Cargo.toml b/Cargo.toml index faee622..6950307 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2024" [dependencies] deranged = "0.5.8" +rand = { version = "0.10.1", default-features = false } diff --git a/src/card_game.rs b/src/card_game.rs index 89e3e90..c0d4fe0 100644 --- a/src/card_game.rs +++ b/src/card_game.rs @@ -11,6 +11,7 @@ pub trait Game { /// 2 bits for suit ID /// 4 bits for card Value /// TODO: better encoding for slightly more decks +#[derive(Clone, Debug)] pub struct Card(u8); pub struct CardValue(deranged::RangedU8<1, 13>); pub enum Suit { @@ -19,8 +20,48 @@ pub enum Suit { Clubs, Diamonds, } +impl Card { + pub fn value(&self) -> CardValue { + let masked = self.0 & 0b1111; + let value = unsafe { deranged::RangedU8::new_unchecked(masked) }; + CardValue(value) + } + pub fn suit(&self) -> Suit { + let red = self.is_red(); + let kiki = self.is_kiki(); + match (kiki, red) { + (false, false) => Suit::Spades, + (false, true) => Suit::Hearts, + (true, false) => Suit::Clubs, + (true, true) => Suit::Diamonds, + } + } + /// Is the suit red. + pub fn is_red(&self) -> bool { + self.0 & 0b010000 != 0 + } + /// Is the suit shape spikey. (Bouba/kiki) + pub fn is_kiki(&self) -> bool { + self.0 & 0b100000 != 0 + } + pub fn deck(&self) -> u8 { + self.0 >> 6 + } +} pub struct Stack(Vec); +impl Stack { + /// Generate a full deck of cards with the specified deck id. + pub fn full_deck(deck_id: u8) -> Stack { + let mut stack = Vec::with_capacity(52); + for suit in 0..4 { + for value in 1..=13 { + stack.push(Card(deck_id << 6 | suit << 4 | value)); + } + } + Stack(stack) + } +} pub struct Session { state: G, diff --git a/src/klondike.rs b/src/klondike.rs index 5a695e0..3aac6a3 100644 --- a/src/klondike.rs +++ b/src/klondike.rs @@ -32,3 +32,9 @@ pub struct Klondike { config: KlondikeConfig, state: KlondikeState, } +impl Klondike { + pub fn new() -> Self { + let deck = Stack::full_deck(0); + unimplemented!() + } +}