O(1) undo
This commit is contained in:
+30
-16
@@ -331,14 +331,32 @@ where
|
|||||||
config: G::Config,
|
config: G::Config,
|
||||||
state: SessionState<G>,
|
state: SessionState<G>,
|
||||||
}
|
}
|
||||||
#[derive(Clone, Eq, Hash, PartialEq)]
|
#[derive(Clone)]
|
||||||
|
pub struct StateSnapshot<G: Game>
|
||||||
|
where
|
||||||
|
G::Instruction: Clone,
|
||||||
|
{
|
||||||
|
state: G,
|
||||||
|
instruction: G::Instruction,
|
||||||
|
}
|
||||||
|
impl<G: Game> StateSnapshot<G>
|
||||||
|
where
|
||||||
|
G::Instruction: Clone,
|
||||||
|
{
|
||||||
|
pub const fn state(&self) -> &G {
|
||||||
|
&self.state
|
||||||
|
}
|
||||||
|
pub const fn instruction(&self) -> &G::Instruction {
|
||||||
|
&self.instruction
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct SessionState<G: Game>
|
pub struct SessionState<G: Game>
|
||||||
where
|
where
|
||||||
G::Instruction: Clone,
|
G::Instruction: Clone,
|
||||||
{
|
{
|
||||||
seed: G,
|
|
||||||
state: G,
|
state: G,
|
||||||
history: Vec<G::Instruction>,
|
history: Vec<StateSnapshot<G>>,
|
||||||
}
|
}
|
||||||
impl<G: Game + Clone> SessionState<G>
|
impl<G: Game + Clone> SessionState<G>
|
||||||
where
|
where
|
||||||
@@ -346,7 +364,6 @@ where
|
|||||||
{
|
{
|
||||||
fn new(state: G) -> Self {
|
fn new(state: G) -> Self {
|
||||||
Self {
|
Self {
|
||||||
seed: state.clone(),
|
|
||||||
state,
|
state,
|
||||||
history: Vec::new(),
|
history: Vec::new(),
|
||||||
}
|
}
|
||||||
@@ -381,7 +398,7 @@ where
|
|||||||
pub const fn config(&self) -> &G::Config {
|
pub const fn config(&self) -> &G::Config {
|
||||||
&self.config
|
&self.config
|
||||||
}
|
}
|
||||||
pub fn history(&self) -> &[G::Instruction] {
|
pub fn history(&self) -> &[StateSnapshot<G>] {
|
||||||
&self.state.history
|
&self.state.history
|
||||||
}
|
}
|
||||||
pub fn undo(&mut self) {
|
pub fn undo(&mut self) {
|
||||||
@@ -401,7 +418,7 @@ 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<G::Instruction>> {
|
pub fn is_winnable(&self) -> Option<Vec<StateSnapshot<G>>> {
|
||||||
let mut state_moves = std::collections::HashMap::new();
|
let mut state_moves = std::collections::HashMap::new();
|
||||||
let mut state = self.clone();
|
let mut state = self.clone();
|
||||||
while !state.is_win() {
|
while !state.is_win() {
|
||||||
@@ -456,19 +473,16 @@ where
|
|||||||
) {
|
) {
|
||||||
match instruction {
|
match instruction {
|
||||||
SessionInstruction::Undo => {
|
SessionInstruction::Undo => {
|
||||||
// replay the entire history of the game except one move
|
if let Some(snapshot) = self.history.pop() {
|
||||||
self.history.pop();
|
self.state = snapshot.state;
|
||||||
let mut inner_stats = G::Stats::default();
|
stats.increment_undos();
|
||||||
let mut state = self.seed.clone();
|
|
||||||
for instruction in &self.history {
|
|
||||||
state.process_instruction(&mut inner_stats, config, instruction.clone());
|
|
||||||
}
|
}
|
||||||
self.state = state;
|
|
||||||
stats.inner_stats = inner_stats;
|
|
||||||
stats.increment_undos();
|
|
||||||
}
|
}
|
||||||
SessionInstruction::InnerInstruction(instruction) => {
|
SessionInstruction::InnerInstruction(instruction) => {
|
||||||
self.history.push(instruction.clone());
|
self.history.push(StateSnapshot {
|
||||||
|
state: self.state.clone(),
|
||||||
|
instruction: instruction.clone(),
|
||||||
|
});
|
||||||
self.state
|
self.state
|
||||||
.process_instruction(&mut stats.inner_stats, config, instruction);
|
.process_instruction(&mut stats.inner_stats, config, instruction);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user