refactor is_winnable
This commit is contained in:
+35
-33
@@ -321,18 +321,29 @@ impl<S> SessionStats<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Session<G: Game> {
|
#[derive(Clone)]
|
||||||
|
pub struct Session<G: Game>
|
||||||
|
where
|
||||||
|
G::Config: Clone,
|
||||||
|
G::Instruction: Clone,
|
||||||
|
{
|
||||||
stats: SessionStats<G::Stats>,
|
stats: SessionStats<G::Stats>,
|
||||||
config: G::Config,
|
config: G::Config,
|
||||||
state: SessionState<G>,
|
state: SessionState<G>,
|
||||||
}
|
}
|
||||||
#[derive(Clone, Eq, Hash, PartialEq)]
|
#[derive(Clone, Eq, Hash, PartialEq)]
|
||||||
pub struct SessionState<G: Game> {
|
pub struct SessionState<G: Game>
|
||||||
|
where
|
||||||
|
G::Instruction: Clone,
|
||||||
|
{
|
||||||
seed: G,
|
seed: G,
|
||||||
state: G,
|
state: G,
|
||||||
history: Vec<G::Instruction>,
|
history: Vec<G::Instruction>,
|
||||||
}
|
}
|
||||||
impl<G: Game + Clone> SessionState<G> {
|
impl<G: Game + Clone> SessionState<G>
|
||||||
|
where
|
||||||
|
G::Instruction: Clone,
|
||||||
|
{
|
||||||
fn new(state: G) -> Self {
|
fn new(state: G) -> Self {
|
||||||
Self {
|
Self {
|
||||||
seed: state.clone(),
|
seed: state.clone(),
|
||||||
@@ -345,6 +356,7 @@ impl<G: Game> Session<G>
|
|||||||
where
|
where
|
||||||
G: Clone + Eq + core::hash::Hash,
|
G: Clone + Eq + core::hash::Hash,
|
||||||
G::Stats: Clone + Default,
|
G::Stats: Clone + Default,
|
||||||
|
G::Config: Clone,
|
||||||
G::Instruction: Clone + Eq + core::hash::Hash,
|
G::Instruction: Clone + Eq + core::hash::Hash,
|
||||||
{
|
{
|
||||||
pub fn new(state: G, config: G::Config) -> Self {
|
pub fn new(state: G, config: G::Config) -> Self {
|
||||||
@@ -390,38 +402,28 @@ where
|
|||||||
self.state.is_win()
|
self.state.is_win()
|
||||||
}
|
}
|
||||||
pub fn is_winnable(&self) -> Option<Vec<G::Instruction>> {
|
pub fn is_winnable(&self) -> Option<Vec<G::Instruction>> {
|
||||||
let mut observed = std::collections::HashSet::new();
|
let mut state_moves = std::collections::HashMap::new();
|
||||||
struct StateMachine<G, P, I> {
|
let mut state = self.clone();
|
||||||
state: G,
|
while !state.is_win() {
|
||||||
possible_instructions_iter: P,
|
// Continue existing iterator if it exists
|
||||||
instruction: I,
|
let it = state_moves
|
||||||
}
|
.entry(state.state().clone())
|
||||||
let mut dummy_stats = self.stats.inner_stats.clone();
|
.or_insert_with(|| state.state().possible_instructions());
|
||||||
let mut state = self.state.state.clone();
|
|
||||||
let mut it = state.possible_instructions();
|
// Run one possible move
|
||||||
let mut path = Vec::new();
|
if let Some(instruction) = it.next() {
|
||||||
'outer: while !state.is_win() {
|
state.process_instruction(instruction);
|
||||||
observed.insert(state.clone());
|
continue;
|
||||||
for instruction in &mut it {
|
}
|
||||||
let mut next_state = state.clone();
|
|
||||||
next_state.process_instruction(&mut dummy_stats, &self.config, instruction.clone());
|
// No more moves. If we can't undo we're done
|
||||||
if !observed.contains(&next_state) {
|
if state.history().is_empty() {
|
||||||
let possible_instructions_iter =
|
return None;
|
||||||
core::mem::replace(&mut it, next_state.possible_instructions());
|
} else {
|
||||||
let state = core::mem::replace(&mut state, next_state);
|
state.undo();
|
||||||
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())
|
Some(state.state.history)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<G: Game> Game for SessionState<G>
|
impl<G: Game> Game for SessionState<G>
|
||||||
|
|||||||
Reference in New Issue
Block a user