undo
This commit is contained in:
+16
-4
@@ -165,14 +165,17 @@ impl Pile {
|
||||
}
|
||||
|
||||
pub struct Session<G: Game> {
|
||||
seed: Rng,
|
||||
seed: G,
|
||||
state: G,
|
||||
history: Vec<G::Instruction>,
|
||||
}
|
||||
impl<G: Game> Session<G> {
|
||||
pub fn new(seed: Rng, state: G) -> Self {
|
||||
impl<G: Game + Clone> Session<G>
|
||||
where
|
||||
G::Instruction: Clone,
|
||||
{
|
||||
pub fn new(state: G) -> Self {
|
||||
Self {
|
||||
seed,
|
||||
seed: state.clone(),
|
||||
state,
|
||||
history: Vec::new(),
|
||||
}
|
||||
@@ -183,6 +186,15 @@ impl<G: Game> Session<G> {
|
||||
pub fn is_winnable(&self) -> Option<Vec<G::Instruction>> {
|
||||
None
|
||||
}
|
||||
pub fn undo(&mut self) {
|
||||
// replay the entire history of the game except one move
|
||||
self.history.pop();
|
||||
let mut state = self.seed.clone();
|
||||
for instruction in self.history() {
|
||||
state.process_instruction(instruction.clone());
|
||||
}
|
||||
self.state = state;
|
||||
}
|
||||
}
|
||||
impl<G: Game> Game for Session<G>
|
||||
where
|
||||
|
||||
+3
-1
@@ -1,6 +1,7 @@
|
||||
use crate::Rng;
|
||||
use crate::card_game::{Game, Pile, Stack};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct KlondikeConfig {}
|
||||
impl Default for KlondikeConfig {
|
||||
fn default() -> Self {
|
||||
@@ -66,7 +67,7 @@ impl KlondikeInstruction {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Hash)]
|
||||
#[derive(Clone, Debug, Hash)]
|
||||
struct KlondikeState {
|
||||
piles: [Pile; 13],
|
||||
}
|
||||
@@ -157,6 +158,7 @@ impl Iterator for KlondikeIter {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Klondike {
|
||||
config: KlondikeConfig,
|
||||
state: KlondikeState,
|
||||
|
||||
+2
-3
@@ -5,9 +5,8 @@ fn test_klondike() {
|
||||
use crate::klondike::Klondike;
|
||||
|
||||
// create game session
|
||||
let seed = Rng::default();
|
||||
let game = Klondike::new(seed.clone(), Default::default());
|
||||
let mut session = Session::new(seed, game);
|
||||
let game = Klondike::new(Rng::default(), Default::default());
|
||||
let mut session = Session::new(game);
|
||||
|
||||
// is winnable
|
||||
let is_winnable = session.is_winnable().is_some();
|
||||
|
||||
Reference in New Issue
Block a user