From ec8d998f75190be075b607c5683ed5b1b26839d2 Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Fri, 15 May 2026 07:03:31 -0700 Subject: [PATCH] card game stuff --- Cargo.lock | 7 ------- Cargo.toml | 1 - src/card_game.rs | 36 ++++++++++++++++++++++++++++++++---- src/klondike.rs | 27 ++++++++++++++++++--------- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f8d0731..e20da57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,7 +18,6 @@ checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" name = "card_game" version = "0.1.0" dependencies = [ - "deranged", "rand", ] @@ -48,12 +47,6 @@ dependencies = [ "libc", ] -[[package]] -name = "deranged" -version = "0.5.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c" - [[package]] name = "equivalent" version = "1.0.2" diff --git a/Cargo.toml b/Cargo.toml index c0022fe..3c34404 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] -deranged = "0.5.8" rand = { version = "0.10.1", default-features = false, features = ["thread_rng"] } diff --git a/src/card_game.rs b/src/card_game.rs index 3b669d9..c8034a0 100644 --- a/src/card_game.rs +++ b/src/card_game.rs @@ -13,9 +13,10 @@ pub trait Game { /// 2 bits for suit ID /// 4 bits for card Value /// TODO: better encoding for slightly more decks -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, Hash, PartialEq)] pub struct Card(u8); -pub struct CardValue(deranged::RangedU8<1, 13>); +pub struct CardValue(u8); +#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub enum Suit { Spades, Hearts, @@ -25,8 +26,7 @@ pub enum Suit { impl Card { pub fn value(&self) -> CardValue { let masked = self.0 & 0b1111; - let value = unsafe { deranged::RangedU8::new_unchecked(masked) }; - CardValue(value) + CardValue(masked) } pub fn suit(&self) -> Suit { let red = self.is_red(); @@ -67,6 +67,34 @@ impl Stack { use rand::seq::SliceRandom; self.0.shuffle(rng); } + pub fn push(&mut self, card: Card) { + self.0.push(card); + } + pub fn pop(&mut self) -> Option { + self.0.pop() + } + pub fn is_empty(&mut self) -> bool { + self.0.is_empty() + } +} + +pub struct Pile { + face_down: Stack, + face_up: Stack, +} +impl Pile { + pub fn pop(&mut self) -> Option { + let card = self.face_up.pop()?; + if self.face_up.is_empty() { + if let Some(card) = self.face_down.pop() { + self.face_up.push(card); + } + } + Some(card) + } + pub fn push(&mut self, card: Card) { + self.face_up.push(card); + } } pub struct Session { diff --git a/src/klondike.rs b/src/klondike.rs index 8183851..a348a66 100644 --- a/src/klondike.rs +++ b/src/klondike.rs @@ -1,15 +1,11 @@ use crate::Rng; -use crate::card_game::{Card, Game, Stack}; +use crate::card_game::{Card, Game, Pile, Stack}; -struct Pile { - face_down: Stack, - face_up: Stack, -} struct KlondikeConfig {} struct KlondikeState { piles: [Pile; 14], } -enum KlondikePileId { +pub enum KlondikePileId { Stock, Hand, Foundation0, @@ -25,9 +21,21 @@ enum KlondikePileId { Tableau6, Tableau7, } +impl std::ops::Index for KlondikeState { + type Output = Pile; + fn index(&self, index: KlondikePileId) -> &Self::Output { + &self.piles[index as usize] + } +} +impl std::ops::IndexMut for KlondikeState { + fn index_mut(&mut self, index: KlondikePileId) -> &mut Self::Output { + &mut self.piles[index as usize] + } +} + pub struct KlondikeInstruction { - src: KlondikePileId, - dst: KlondikePileId, + pub src: KlondikePileId, + pub dst: KlondikePileId, } pub struct Klondike { config: KlondikeConfig, @@ -49,6 +57,7 @@ impl Game for Klondike { todo!() } fn process_instruction(&mut self, instruction: Self::Instruction) { - todo!() + let card = self.state[instruction.src].pop().unwrap(); + self.state[instruction.dst].push(card); } }