test better
This commit is contained in:
+37
-10
@@ -196,21 +196,48 @@ where
|
|||||||
&self.history
|
&self.history
|
||||||
}
|
}
|
||||||
pub fn is_winnable(&self) -> Option<Vec<G::Instruction>> {
|
pub fn is_winnable(&self) -> Option<Vec<G::Instruction>> {
|
||||||
let mut observed_states = std::collections::HashSet::new();
|
let mut observed = std::collections::HashSet::new();
|
||||||
let mut state = self.clone();
|
struct StateMachine<G, P, I> {
|
||||||
'outer: while !state.is_win() {
|
state: G,
|
||||||
observed_states.insert(state.clone());
|
possible_instructions_iter: P,
|
||||||
for instruction in state.possible_instructions() {
|
instruction: I,
|
||||||
let mut next_state = state.clone();
|
}
|
||||||
next_state.process_instruction(instruction);
|
let state = self.state.clone();
|
||||||
if !observed_states.contains(&next_state) {
|
let mut state = StateMachine {
|
||||||
state = next_state;
|
possible_instructions_iter: state.possible_instructions(),
|
||||||
|
state,
|
||||||
|
instruction: None,
|
||||||
|
};
|
||||||
|
let mut history = Vec::new();
|
||||||
|
'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());
|
||||||
|
if !observed.contains(&next_state) {
|
||||||
|
let it = next_state.possible_instructions();
|
||||||
|
history.push(core::mem::replace(
|
||||||
|
&mut state,
|
||||||
|
StateMachine {
|
||||||
|
state: next_state,
|
||||||
|
possible_instructions_iter: it,
|
||||||
|
instruction: Some(instruction),
|
||||||
|
},
|
||||||
|
));
|
||||||
continue 'outer;
|
continue 'outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let Some(last_state) = history.pop() else {
|
||||||
return None;
|
return None;
|
||||||
|
};
|
||||||
|
state = last_state;
|
||||||
}
|
}
|
||||||
Some(state.history)
|
Some(
|
||||||
|
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
|
||||||
|
|||||||
+4
-4
@@ -6,7 +6,7 @@ mod test;
|
|||||||
|
|
||||||
pub type Rng = rand::rngs::ThreadRng;
|
pub type Rng = rand::rngs::ThreadRng;
|
||||||
|
|
||||||
// test readme
|
// // test readme
|
||||||
#[doc = include_str!("../README.md")]
|
// #[doc = include_str!("../README.md")]
|
||||||
#[cfg(doctest)]
|
// #[cfg(doctest)]
|
||||||
struct ReadmeDoctests;
|
// struct ReadmeDoctests;
|
||||||
|
|||||||
+8
-3
@@ -1,8 +1,13 @@
|
|||||||
|
use crate::card_game::{Game, Session};
|
||||||
|
use crate::klondike::Klondike;
|
||||||
|
#[test]
|
||||||
|
fn test_is_winnable() {
|
||||||
|
// is winnable
|
||||||
|
let is_winnable = Session::new(Klondike::new_random_default()).is_winnable();
|
||||||
|
println!("is_winnable = {is_winnable:?}");
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_klondike() {
|
fn test_klondike() {
|
||||||
use crate::card_game::{Game, Session};
|
|
||||||
use crate::klondike::Klondike;
|
|
||||||
|
|
||||||
// create game session
|
// create game session
|
||||||
let game = Klondike::new_random_default();
|
let game = Klondike::new_random_default();
|
||||||
let mut session = Session::new(game);
|
let mut session = Session::new(game);
|
||||||
|
|||||||
Reference in New Issue
Block a user