From 2f2910dce2d45df8b016598d11fd766fb56a55ba Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Sun, 17 May 2026 09:29:28 -0700 Subject: [PATCH] fini --- card_game/src/lib.rs | 23 ++++++++++++++++++++--- klondike-cli/src/main.rs | 15 ++++++++------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/card_game/src/lib.rs b/card_game/src/lib.rs index df874bd..a894400 100644 --- a/card_game/src/lib.rs +++ b/card_game/src/lib.rs @@ -269,7 +269,7 @@ pub struct SessionState { impl Session where G: Clone + Eq + core::hash::Hash, - G::Stats: Clone, + G::Stats: Clone + Default, G::Instruction: Clone + Eq + core::hash::Hash, { pub fn new(state: G, stats: G::Stats, config: G::Config) -> Self { @@ -288,17 +288,34 @@ where } pub fn new_default(state: G) -> Self where - G::Stats: Default, G::Config: Default, { Self::new(state, Default::default(), Default::default()) } - pub fn state(&self) -> &G { + pub const fn state(&self) -> &G { &self.state.state } + pub const fn config(&self) -> &G::Config { + &self.config + } pub fn history(&self) -> &[G::Instruction] { &self.state.history } + pub fn undo(&mut self) { + self.state + .process_instruction(&mut self.stats, &self.config, SessionInstruction::Undo) + } + pub fn possible_instructions(&self) -> impl Iterator + use { + self.state.state.possible_instructions() + } + pub fn process_instruction(&mut self, instruction: G::Instruction) { + self.state + .state + .process_instruction(&mut self.stats.inner_stats, &self.config, instruction) + } + pub fn is_win(&self) -> bool { + self.state.is_win() + } pub fn is_winnable(&self) -> Option> { let mut observed = std::collections::HashSet::new(); struct StateMachine { diff --git a/klondike-cli/src/main.rs b/klondike-cli/src/main.rs index 4c399f0..2b6d0af 100644 --- a/klondike-cli/src/main.rs +++ b/klondike-cli/src/main.rs @@ -1,7 +1,7 @@ use card_game::{Card, CardValue, Game, Pile, Session, Suit}; use klondike::{ - DstFoundation, DstTableau, Foundation, Klondike, KlondikeInstruction, KlondikePile, - KlondikePileStack, SkipCards, Tableau, TableauStack, + DstFoundation, DstTableau, Foundation, Klondike, KlondikeConfig, KlondikeInstruction, + KlondikePile, KlondikePileStack, SkipCards, Tableau, TableauStack, }; use std::fmt::Display; @@ -144,6 +144,7 @@ impl core::str::FromStr for SessionInstruction { } fn find_valid_instruction( + config: &KlondikeConfig, state: &Klondike, naive_instruction: NaiveInstruction, ) -> Option { @@ -173,7 +174,7 @@ fn find_valid_instruction( }); let instruction = KlondikeInstruction::DstTableau(DstTableau { tableau, src }); - if state.is_instruction_valid(instruction) { + if state.is_instruction_valid(config, instruction) { return Some(instruction); } } @@ -191,7 +192,7 @@ fn find_valid_instruction( _ => return None, }; state - .is_instruction_valid(instruction) + .is_instruction_valid(config, instruction) .then_some(instruction) } @@ -241,7 +242,7 @@ fn get_good_move(state: &Klondike) -> Option { } fn main() -> Result<(), std::io::Error> { - let mut session = Session::new(Klondike::new_random()); + let mut session = Session::new_default(Klondike::new_random()); let mut input = String::new(); loop { // display game @@ -257,7 +258,7 @@ fn main() -> Result<(), std::io::Error> { // run game match instruction { - SessionInstruction::New => session = Session::new(Klondike::new_random()), + SessionInstruction::New => session = Session::new_default(Klondike::new_random()), SessionInstruction::Undo => session.undo(), SessionInstruction::Exit => break Ok(()), SessionInstruction::Hint => { @@ -277,7 +278,7 @@ fn main() -> Result<(), std::io::Error> { } SessionInstruction::Klondike(naive_instruction) => { if let Some(instruction) = - find_valid_instruction(session.state(), naive_instruction) + find_valid_instruction(session.config(), session.state(), naive_instruction) { session.process_instruction(instruction); } else {