Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5b277601ea | |||
| 418e422f12 | |||
| bc2d1b126e | |||
| 08e8656ecf | |||
| 73ffef76b0 |
+40
-4
@@ -321,6 +321,9 @@ impl<S> SessionStats<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Oom;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Session<G: Game> {
|
pub struct Session<G: Game> {
|
||||||
stats: SessionStats<G::Stats>,
|
stats: SessionStats<G::Stats>,
|
||||||
@@ -402,10 +405,16 @@ where
|
|||||||
pub fn is_win(&self) -> bool {
|
pub fn is_win(&self) -> bool {
|
||||||
self.state.is_win()
|
self.state.is_win()
|
||||||
}
|
}
|
||||||
pub fn is_winnable(&self) -> Option<Vec<StateSnapshot<G>>> {
|
pub fn is_winnable(&self) -> Result<Option<Vec<StateSnapshot<G>>>, Oom> {
|
||||||
let mut state_moves = std::collections::HashMap::new();
|
const HUGE_CAP: usize = 1 << 25;
|
||||||
|
let mut state_moves = std::collections::HashMap::with_capacity(HUGE_CAP);
|
||||||
let mut state = self.clone();
|
let mut state = self.clone();
|
||||||
while !state.is_win() {
|
while !state.is_win() {
|
||||||
|
// don't look for empty hash map buckets when the hash map is 99% full!
|
||||||
|
if HUGE_CAP * 127 <= state_moves.len() * 128 {
|
||||||
|
return Err(Oom);
|
||||||
|
}
|
||||||
|
|
||||||
// Continue existing iterator if it exists
|
// Continue existing iterator if it exists
|
||||||
let it = state_moves
|
let it = state_moves
|
||||||
.entry(state.state().clone())
|
.entry(state.state().clone())
|
||||||
@@ -419,12 +428,39 @@ where
|
|||||||
|
|
||||||
// No more moves. If we can't undo we're done
|
// No more moves. If we can't undo we're done
|
||||||
if state.history().is_empty() {
|
if state.history().is_empty() {
|
||||||
return None;
|
return Ok(None);
|
||||||
} else {
|
} else {
|
||||||
state.undo();
|
state.undo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(state.state.history)
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(state.state.history))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<G: Game> Game for SessionState<G>
|
impl<G: Game> Game for SessionState<G>
|
||||||
|
|||||||
@@ -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(0)).is_winnable().unwrap();
|
||||||
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}");
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user