3 Commits

Author SHA1 Message Date
Quaternions bc2d1b126e clean history 2026-05-19 10:22:44 -07:00
Quaternions 08e8656ecf test klondike iter 2026-05-19 09:46:11 -07:00
Quaternions 73ffef76b0 delete infinite loop test 2026-05-19 09:45:49 -07:00
3 changed files with 39 additions and 26 deletions
+27
View File
@@ -424,6 +424,33 @@ where
state.undo(); state.undo();
} }
} }
// history includes cycles
let mut state_index: std::collections::HashMap<_, _> = state
.history()
.iter()
.enumerate()
.map(|(i, snapshot)| (snapshot.state().clone(), i))
.collect();
// find the longest range where the start and end are the same state
while let Some(longest_range) = state
.history()
.iter()
.enumerate()
.filter_map(|(index, snapshot)| {
let &last_index = state_index.get(snapshot.state())?;
let longness = last_index - index;
(longness != 0).then_some(index..last_index)
})
.max_by_key(|range| range.len())
{
state.state.history.drain(longest_range);
for (i, snapshot) in state.history().iter().enumerate() {
state_index.insert(snapshot.state().clone(), i);
}
}
Some(state.state.history) Some(state.state.history)
} }
} }
+8 -26
View File
@@ -3,31 +3,13 @@ use klondike::Klondike;
#[test] #[test]
fn test_is_winnable() { fn test_is_winnable() {
// is winnable // is winnable
let is_winnable = Session::new_default(Klondike::with_seed(123)).is_winnable(); let is_winnable = Session::new_default(Klondike::with_seed(124)).is_winnable();
println!("is_winnable = {is_winnable:?}"); if let Some(win_moves) = is_winnable {
} // for (i, ins) in win_moves.into_iter().enumerate() {
#[test] // println!("{i} = {:?}", ins.instruction());
fn test_klondike() { // }
// create game session println!("Game is winnable with {} moves", win_moves.len());
let game = Klondike::with_seed(123); } else {
let mut session = Session::new_default(game); println!("Game is not winnable");
// is winnable
let is_winnable = session.is_winnable();
println!("is_winnable = {is_winnable:?}");
// play game
while let Some(instruction) = session.possible_instructions().next() {
session.process_instruction(instruction);
} }
// did win
let is_win = session.is_win();
// print session history
for (i, instruction) in session.history().iter().enumerate() {
println!("move {i} = {instruction:?}");
}
println!("is_win = {is_win}");
} }
+4
View File
@@ -533,6 +533,10 @@ impl Iterator for KlondikeIter {
instruction instruction
} }
} }
#[test]
fn test_klondike_iter() {
assert_eq!(KlondikeIter::new().count(), 721);
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Klondike { pub struct Klondike {