implement is_winnable
This commit is contained in:
+20
-5
@@ -97,7 +97,7 @@ impl Card {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Hash)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
pub struct Stack(Vec<Card>);
|
pub struct Stack(Vec<Card>);
|
||||||
impl Stack {
|
impl Stack {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
@@ -131,7 +131,7 @@ impl std::ops::DerefMut for Stack {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Hash)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
pub struct Pile {
|
pub struct Pile {
|
||||||
face_down: Stack,
|
face_down: Stack,
|
||||||
face_up: Stack,
|
face_up: Stack,
|
||||||
@@ -175,14 +175,15 @@ impl Pile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
pub struct Session<G: Game> {
|
pub struct Session<G: Game> {
|
||||||
seed: G,
|
seed: G,
|
||||||
state: G,
|
state: G,
|
||||||
history: Vec<G::Instruction>,
|
history: Vec<G::Instruction>,
|
||||||
}
|
}
|
||||||
impl<G: Game + Clone> Session<G>
|
impl<G: Game + Clone + Eq + core::hash::Hash> Session<G>
|
||||||
where
|
where
|
||||||
G::Instruction: Clone,
|
G::Instruction: Clone + Eq + core::hash::Hash,
|
||||||
{
|
{
|
||||||
pub fn new(state: G) -> Self {
|
pub fn new(state: G) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -195,7 +196,21 @@ where
|
|||||||
&self.history
|
&self.history
|
||||||
}
|
}
|
||||||
pub fn is_winnable(&self) -> Option<Vec<G::Instruction>> {
|
pub fn is_winnable(&self) -> Option<Vec<G::Instruction>> {
|
||||||
None
|
let mut observed_states = std::collections::HashSet::new();
|
||||||
|
let mut state = self.clone();
|
||||||
|
'outer: while !state.is_win() {
|
||||||
|
observed_states.insert(state.clone());
|
||||||
|
for instruction in state.possible_instructions() {
|
||||||
|
let mut next_state = state.clone();
|
||||||
|
next_state.process_instruction(instruction);
|
||||||
|
if !observed_states.contains(&next_state) {
|
||||||
|
state = next_state;
|
||||||
|
continue 'outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(state.history)
|
||||||
}
|
}
|
||||||
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
|
||||||
|
|||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
use crate::Rng;
|
use crate::Rng;
|
||||||
use crate::card_game::{CardValue, Game, Pile, Stack};
|
use crate::card_game::{CardValue, Game, Pile, Stack};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
pub struct KlondikeConfig {}
|
pub struct KlondikeConfig {}
|
||||||
impl Default for KlondikeConfig {
|
impl Default for KlondikeConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
@@ -67,7 +67,7 @@ impl KlondikeInstruction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Hash)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
struct KlondikeState {
|
struct KlondikeState {
|
||||||
piles: [Pile; 13],
|
piles: [Pile; 13],
|
||||||
}
|
}
|
||||||
@@ -163,7 +163,7 @@ impl Iterator for KlondikeIter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
pub struct Klondike {
|
pub struct Klondike {
|
||||||
config: KlondikeConfig,
|
config: KlondikeConfig,
|
||||||
state: KlondikeState,
|
state: KlondikeState,
|
||||||
|
|||||||
+2
-2
@@ -8,7 +8,8 @@ fn test_klondike() {
|
|||||||
let mut session = Session::new(game);
|
let mut session = Session::new(game);
|
||||||
|
|
||||||
// is winnable
|
// is winnable
|
||||||
let is_winnable = session.is_winnable().is_some();
|
let is_winnable = session.is_winnable();
|
||||||
|
println!("is_winnable = {is_winnable:?}");
|
||||||
|
|
||||||
// play game
|
// play game
|
||||||
while let Some(instruction) = session.possible_instructions().next() {
|
while let Some(instruction) = session.possible_instructions().next() {
|
||||||
@@ -23,6 +24,5 @@ fn test_klondike() {
|
|||||||
println!("move {i} = {instruction:?}");
|
println!("move {i} = {instruction:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("is_winnable = {is_winnable}");
|
|
||||||
println!("is_win = {is_win}");
|
println!("is_win = {is_win}");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user