From 576489c226a21cac6ce92396181f6bbfffdf647b Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Tue, 19 May 2026 07:10:38 -0700 Subject: [PATCH] Revert "temporarily remove is_winnable because it doesn't work" This reverts commit 5a52f2ab7ad6f39736738eddc7a0c800fadc4f55. --- card_game/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ klondike-cli/src/main.rs | 4 ++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/card_game/src/lib.rs b/card_game/src/lib.rs index 20d92eb..17c4bc9 100644 --- a/card_game/src/lib.rs +++ b/card_game/src/lib.rs @@ -389,6 +389,40 @@ where 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 { + state: G, + possible_instructions_iter: P, + instruction: I, + } + let mut dummy_stats = self.stats.inner_stats.clone(); + let mut state = self.state.state.clone(); + let mut it = state.possible_instructions(); + let mut path = Vec::new(); + 'outer: while !state.is_win() { + observed.insert(state.clone()); + for instruction in &mut it { + let mut next_state = state.clone(); + next_state.process_instruction(&mut dummy_stats, &self.config, instruction.clone()); + if !observed.contains(&next_state) { + let possible_instructions_iter = + core::mem::replace(&mut it, next_state.possible_instructions()); + let state = core::mem::replace(&mut state, next_state); + path.push(StateMachine { + state, + possible_instructions_iter, + instruction, + }); + continue 'outer; + } + } + let last_state = path.pop()?; + state = last_state.state; + it = last_state.possible_instructions_iter; + } + Some(path.into_iter().map(|state| state.instruction).collect()) + } } impl Game for SessionState where diff --git a/klondike-cli/src/main.rs b/klondike-cli/src/main.rs index 3721b75..d53d1b2 100644 --- a/klondike-cli/src/main.rs +++ b/klondike-cli/src/main.rs @@ -4,8 +4,8 @@ use klondike::{ KlondikePile, KlondikePileStack, KlondikeStats, SkipCards, Tableau, TableauStack, }; -// #[cfg(test)] -// mod test; +#[cfg(test)] +mod test; use std::fmt::Display; struct Displayed(T);