From 4ffd2a338f4d3a95a6af33a6b84f803c13d1a1dd Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Fri, 15 May 2026 07:57:01 -0700 Subject: [PATCH] card game stuff --- src/card_game.rs | 8 ++++++++ src/klondike.rs | 25 +++++++++++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/card_game.rs b/src/card_game.rs index 863b91d..284ce7f 100644 --- a/src/card_game.rs +++ b/src/card_game.rs @@ -66,6 +66,7 @@ impl Card { } } +#[derive(Hash)] pub struct Stack(Vec); impl Stack { pub fn new() -> Self { @@ -99,6 +100,7 @@ impl std::ops::DerefMut for Stack { } } +#[derive(Hash)] pub struct Pile { face_down: Stack, face_up: Stack, @@ -134,6 +136,12 @@ impl Pile { pub fn push(&mut self, card: Card) { self.face_up.push(card); } + pub fn face_up(&self) -> &[Card] { + &self.face_up + } + pub fn face_down(&self) -> &[Card] { + &self.face_down + } } pub struct Session { diff --git a/src/klondike.rs b/src/klondike.rs index 79abab8..3a03a1b 100644 --- a/src/klondike.rs +++ b/src/klondike.rs @@ -7,9 +7,11 @@ impl Default for KlondikeConfig { KlondikeConfig {} } } +#[derive(Hash)] struct KlondikeState { piles: [Pile; 13], } +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum KlondikePileId { Tableau0, Tableau1, @@ -25,18 +27,7 @@ pub enum KlondikePileId { Foundation2, Foundation3, } -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] - } -} - +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub struct KlondikeInstruction { pub src: KlondikePileId, pub dst: KlondikePileId, @@ -82,6 +73,12 @@ impl Klondike { }; Self { config, state } } + pub fn pile(&self, index: KlondikePileId) -> &Pile { + &self.state.piles[index as usize] + } + fn pile_mut(&mut self, index: KlondikePileId) -> &mut Pile { + &mut self.state.piles[index as usize] + } } impl Game for Klondike { type Instruction = KlondikeInstruction; @@ -92,8 +89,8 @@ impl Game for Klondike { todo!() } fn process_instruction(&mut self, instruction: Self::Instruction) { - let card = self.state[instruction.src].pop().unwrap(); - self.state[instruction.dst].push(card); + let card = self.pile_mut(instruction.src).pop().unwrap(); + self.pile_mut(instruction.dst).push(card); } fn is_win(&self) -> bool { // assuming only valid moves, tableau empty and stock empty means win