refactor winnable
This commit is contained in:
+19
-28
@@ -227,42 +227,33 @@ where
|
|||||||
possible_instructions_iter: P,
|
possible_instructions_iter: P,
|
||||||
instruction: I,
|
instruction: I,
|
||||||
}
|
}
|
||||||
let state = self.state.clone();
|
let mut state = self.state.clone();
|
||||||
let mut state = StateMachine {
|
let mut it = state.possible_instructions();
|
||||||
possible_instructions_iter: state.possible_instructions(),
|
let mut path = Vec::new();
|
||||||
state,
|
'outer: while !state.is_win() {
|
||||||
instruction: None,
|
observed.insert(state.clone());
|
||||||
};
|
for instruction in &mut it {
|
||||||
let mut history = Vec::new();
|
let mut next_state = state.clone();
|
||||||
'outer: while !state.state.is_win() {
|
|
||||||
observed.insert(state.state.clone());
|
|
||||||
for instruction in &mut state.possible_instructions_iter {
|
|
||||||
let mut next_state = state.state.clone();
|
|
||||||
next_state.process_instruction(instruction.clone());
|
next_state.process_instruction(instruction.clone());
|
||||||
if !observed.contains(&next_state) {
|
if !observed.contains(&next_state) {
|
||||||
let it = next_state.possible_instructions();
|
let possible_instructions_iter =
|
||||||
history.push(core::mem::replace(
|
core::mem::replace(&mut it, next_state.possible_instructions());
|
||||||
&mut state,
|
let state = core::mem::replace(&mut state, next_state);
|
||||||
StateMachine {
|
path.push(StateMachine {
|
||||||
state: next_state,
|
state,
|
||||||
possible_instructions_iter: it,
|
possible_instructions_iter,
|
||||||
instruction: Some(instruction),
|
instruction,
|
||||||
},
|
});
|
||||||
));
|
|
||||||
continue 'outer;
|
continue 'outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let Some(last_state) = history.pop() else {
|
let Some(last_state) = path.pop() else {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
state = last_state;
|
state = last_state.state;
|
||||||
|
it = last_state.possible_instructions_iter;
|
||||||
}
|
}
|
||||||
Some(
|
Some(path.into_iter().map(|state| state.instruction).collect())
|
||||||
history
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|state| state.instruction)
|
|
||||||
.collect(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
pub fn undo(&mut self) {
|
pub fn undo(&mut self) {
|
||||||
// replay the entire history of the game except one move
|
// replay the entire history of the game except one move
|
||||||
|
|||||||
Reference in New Issue
Block a user