Compare commits
2 Commits
bc2d1b126e
...
5b277601ea
| Author | SHA1 | Date | |
|---|---|---|---|
| 5b277601ea | |||
| 418e422f12 |
+13
-4
@@ -321,6 +321,9 @@ impl<S> SessionStats<S> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Oom;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Session<G: Game> {
|
||||
stats: SessionStats<G::Stats>,
|
||||
@@ -402,10 +405,16 @@ where
|
||||
pub fn is_win(&self) -> bool {
|
||||
self.state.is_win()
|
||||
}
|
||||
pub fn is_winnable(&self) -> Option<Vec<StateSnapshot<G>>> {
|
||||
let mut state_moves = std::collections::HashMap::new();
|
||||
pub fn is_winnable(&self) -> Result<Option<Vec<StateSnapshot<G>>>, Oom> {
|
||||
const HUGE_CAP: usize = 1 << 25;
|
||||
let mut state_moves = std::collections::HashMap::with_capacity(HUGE_CAP);
|
||||
let mut state = self.clone();
|
||||
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
|
||||
let it = state_moves
|
||||
.entry(state.state().clone())
|
||||
@@ -419,7 +428,7 @@ where
|
||||
|
||||
// No more moves. If we can't undo we're done
|
||||
if state.history().is_empty() {
|
||||
return None;
|
||||
return Ok(None);
|
||||
} else {
|
||||
state.undo();
|
||||
}
|
||||
@@ -451,7 +460,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
Some(state.state.history)
|
||||
Ok(Some(state.state.history))
|
||||
}
|
||||
}
|
||||
impl<G: Game> Game for SessionState<G>
|
||||
|
||||
@@ -3,7 +3,7 @@ use klondike::Klondike;
|
||||
#[test]
|
||||
fn test_is_winnable() {
|
||||
// is winnable
|
||||
let is_winnable = Session::new_default(Klondike::with_seed(124)).is_winnable();
|
||||
let is_winnable = Session::new_default(Klondike::with_seed(0)).is_winnable().unwrap();
|
||||
if let Some(win_moves) = is_winnable {
|
||||
// for (i, ins) in win_moves.into_iter().enumerate() {
|
||||
// println!("{i} = {:?}", ins.instruction());
|
||||
|
||||
Reference in New Issue
Block a user