From 9dd379cb1b0b6011319c0bee02c935e1cd9e8bee Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Fri, 15 May 2026 07:35:31 -0700 Subject: [PATCH] card game stuff --- src/card_game.rs | 16 +++++++++++++--- src/klondike.rs | 6 +++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/card_game.rs b/src/card_game.rs index 0a1c264..863b91d 100644 --- a/src/card_game.rs +++ b/src/card_game.rs @@ -3,9 +3,10 @@ use crate::Rng; // TODO: pub struct ValidInstruction(I); pub trait Game { type Instruction; - fn enumerate_instructions(&self) -> impl Iterator; + fn possible_instructions(&self) -> impl Iterator; fn validate_instruction(&self, instruction: Self::Instruction) -> bool; fn process_instruction(&mut self, instruction: Self::Instruction); + fn is_win(&self) -> bool; } #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] @@ -118,6 +119,9 @@ impl Pile { pub fn make_face_down(&mut self) { self.face_down.extend(self.face_up.drain(..)); } + pub fn is_empty(&self) -> bool { + self.face_down.is_empty() && self.face_up.is_empty() + } pub fn pop(&mut self) -> Option { let card = self.face_up.pop()?; if self.face_up.is_empty() { @@ -148,14 +152,17 @@ impl Session { pub fn history(&self) -> &[G::Instruction] { &self.history } + pub fn is_winnable(&self) -> Option> { + None + } } impl Game for Session where G::Instruction: Clone, { type Instruction = G::Instruction; - fn enumerate_instructions(&self) -> impl Iterator { - self.state.enumerate_instructions() + fn possible_instructions(&self) -> impl Iterator { + self.state.possible_instructions() } fn validate_instruction(&self, instruction: Self::Instruction) -> bool { self.state.validate_instruction(instruction) @@ -164,4 +171,7 @@ where self.history.push(instruction.clone()); self.state.process_instruction(instruction); } + fn is_win(&self) -> bool { + self.state.is_win() + } } diff --git a/src/klondike.rs b/src/klondike.rs index 1d5aa95..79abab8 100644 --- a/src/klondike.rs +++ b/src/klondike.rs @@ -85,7 +85,7 @@ impl Klondike { } impl Game for Klondike { type Instruction = KlondikeInstruction; - fn enumerate_instructions(&self) -> impl Iterator { + fn possible_instructions(&self) -> impl Iterator { vec![].into_iter() } fn validate_instruction(&self, instruction: Self::Instruction) -> bool { @@ -95,4 +95,8 @@ impl Game for Klondike { let card = self.state[instruction.src].pop().unwrap(); self.state[instruction.dst].push(card); } + fn is_win(&self) -> bool { + // assuming only valid moves, tableau empty and stock empty means win + self.state.piles[0..9].iter().all(|pile| pile.is_empty()) + } }