Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 59067ebfbf |
+4
-13
@@ -321,9 +321,6 @@ 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>,
|
||||||
@@ -405,16 +402,10 @@ 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) -> Result<Option<Vec<StateSnapshot<G>>>, Oom> {
|
pub fn is_winnable(&self) -> Option<Vec<StateSnapshot<G>>> {
|
||||||
const HUGE_CAP: usize = 1 << 25;
|
let mut state_moves = std::collections::HashMap::new();
|
||||||
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())
|
||||||
@@ -428,7 +419,7 @@ 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 Ok(None);
|
return None;
|
||||||
} else {
|
} else {
|
||||||
state.undo();
|
state.undo();
|
||||||
}
|
}
|
||||||
@@ -460,7 +451,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Some(state.state.history))
|
Some(state.state.history)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<G: Game> Game for SessionState<G>
|
impl<G: Game> Game for SessionState<G>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ 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(0)).is_winnable().unwrap();
|
let is_winnable = Session::new_default(Klondike::with_seed(124)).is_winnable();
|
||||||
if let Some(win_moves) = is_winnable {
|
if let Some(win_moves) = is_winnable {
|
||||||
// for (i, ins) in win_moves.into_iter().enumerate() {
|
// for (i, ins) in win_moves.into_iter().enumerate() {
|
||||||
// println!("{i} = {:?}", ins.instruction());
|
// println!("{i} = {:?}", ins.instruction());
|
||||||
|
|||||||
+7
-7
@@ -629,18 +629,19 @@ impl Klondike {
|
|||||||
KlondikeInstruction::RotateStock => 4,
|
KlondikeInstruction::RotateStock => 4,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = KlondikeInstruction> + use<> {
|
||||||
|
let state = self.state.clone();
|
||||||
|
KlondikeIter::new().filter(move |&instruction| state.is_instruction_valid(instruction))
|
||||||
|
}
|
||||||
/// A single move that usually makes progress towards a winning game
|
/// A single move that usually makes progress towards a winning game
|
||||||
pub fn get_auto_move(&self) -> Option<KlondikeInstruction> {
|
pub fn get_auto_move(&self) -> Option<KlondikeInstruction> {
|
||||||
self.possible_instructions()
|
self.iter()
|
||||||
.filter(|ins| !ins.is_useless())
|
.filter(|ins| !ins.is_useless())
|
||||||
.min_by_key(|ins| self.instruction_priority(ins))
|
.min_by_key(|ins| self.instruction_priority(ins))
|
||||||
}
|
}
|
||||||
/// A list of possible moves with useless moves filtered out and sorted by a simple priority function
|
/// A list of possible moves with useless moves filtered out and sorted by a simple priority function
|
||||||
pub fn get_sorted_moves(&self) -> Vec<KlondikeInstruction> {
|
pub fn get_sorted_moves(&self) -> Vec<KlondikeInstruction> {
|
||||||
let mut useful_moves: Vec<_> = self
|
let mut useful_moves: Vec<_> = self.iter().filter(|ins| !ins.is_useless()).collect();
|
||||||
.possible_instructions()
|
|
||||||
.filter(|ins| !ins.is_useless())
|
|
||||||
.collect();
|
|
||||||
useful_moves.sort_by_key(|ins| self.instruction_priority(ins));
|
useful_moves.sort_by_key(|ins| self.instruction_priority(ins));
|
||||||
useful_moves
|
useful_moves
|
||||||
}
|
}
|
||||||
@@ -651,8 +652,7 @@ impl Game for Klondike {
|
|||||||
type Config = KlondikeConfig;
|
type Config = KlondikeConfig;
|
||||||
type Instruction = KlondikeInstruction;
|
type Instruction = KlondikeInstruction;
|
||||||
fn possible_instructions(&self) -> impl Iterator<Item = Self::Instruction> + use<> {
|
fn possible_instructions(&self) -> impl Iterator<Item = Self::Instruction> + use<> {
|
||||||
let state = self.state.clone();
|
self.get_sorted_moves().into_iter()
|
||||||
KlondikeIter::new().filter(move |&instruction| state.is_instruction_valid(instruction))
|
|
||||||
}
|
}
|
||||||
fn is_instruction_valid(&self, _config: &Self::Config, instruction: Self::Instruction) -> bool {
|
fn is_instruction_valid(&self, _config: &Self::Config, instruction: Self::Instruction) -> bool {
|
||||||
self.state.is_instruction_valid(instruction)
|
self.state.is_instruction_valid(instruction)
|
||||||
|
|||||||
Reference in New Issue
Block a user