Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f9012b01c4 | |||
| e18e242eae | |||
| 576489c226 |
+39
-3
@@ -321,18 +321,29 @@ impl<S> SessionStats<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Session<G: Game> {
|
#[derive(Clone)]
|
||||||
|
pub struct Session<G: Game>
|
||||||
|
where
|
||||||
|
G::Config: Clone,
|
||||||
|
G::Instruction: Clone,
|
||||||
|
{
|
||||||
stats: SessionStats<G::Stats>,
|
stats: SessionStats<G::Stats>,
|
||||||
config: G::Config,
|
config: G::Config,
|
||||||
state: SessionState<G>,
|
state: SessionState<G>,
|
||||||
}
|
}
|
||||||
#[derive(Clone, Eq, Hash, PartialEq)]
|
#[derive(Clone, Eq, Hash, PartialEq)]
|
||||||
pub struct SessionState<G: Game> {
|
pub struct SessionState<G: Game>
|
||||||
|
where
|
||||||
|
G::Instruction: Clone,
|
||||||
|
{
|
||||||
seed: G,
|
seed: G,
|
||||||
state: G,
|
state: G,
|
||||||
history: Vec<G::Instruction>,
|
history: Vec<G::Instruction>,
|
||||||
}
|
}
|
||||||
impl<G: Game + Clone> SessionState<G> {
|
impl<G: Game + Clone> SessionState<G>
|
||||||
|
where
|
||||||
|
G::Instruction: Clone,
|
||||||
|
{
|
||||||
fn new(state: G) -> Self {
|
fn new(state: G) -> Self {
|
||||||
Self {
|
Self {
|
||||||
seed: state.clone(),
|
seed: state.clone(),
|
||||||
@@ -345,6 +356,7 @@ impl<G: Game> Session<G>
|
|||||||
where
|
where
|
||||||
G: Clone + Eq + core::hash::Hash,
|
G: Clone + Eq + core::hash::Hash,
|
||||||
G::Stats: Clone + Default,
|
G::Stats: Clone + Default,
|
||||||
|
G::Config: Clone,
|
||||||
G::Instruction: Clone + Eq + core::hash::Hash,
|
G::Instruction: Clone + Eq + core::hash::Hash,
|
||||||
{
|
{
|
||||||
pub fn new(state: G, config: G::Config) -> Self {
|
pub fn new(state: G, config: G::Config) -> Self {
|
||||||
@@ -389,6 +401,30 @@ 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>> {
|
||||||
|
let mut state_moves = std::collections::HashMap::new();
|
||||||
|
let mut state = self.clone();
|
||||||
|
while !state.is_win() {
|
||||||
|
// Continue existing iterator if it exists
|
||||||
|
let it = state_moves
|
||||||
|
.entry(state.state().clone())
|
||||||
|
.or_insert_with(|| state.state().possible_instructions());
|
||||||
|
|
||||||
|
// Run one possible move
|
||||||
|
if let Some(instruction) = it.next() {
|
||||||
|
state.process_instruction(instruction);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No more moves. If we can't undo we're done
|
||||||
|
if state.history().is_empty() {
|
||||||
|
return None;
|
||||||
|
} else {
|
||||||
|
state.undo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(state.state.history)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl<G: Game> Game for SessionState<G>
|
impl<G: Game> Game for SessionState<G>
|
||||||
where
|
where
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ use klondike::{
|
|||||||
KlondikePile, KlondikePileStack, KlondikeStats, SkipCards, Tableau, TableauStack,
|
KlondikePile, KlondikePileStack, KlondikeStats, SkipCards, Tableau, TableauStack,
|
||||||
};
|
};
|
||||||
|
|
||||||
// #[cfg(test)]
|
#[cfg(test)]
|
||||||
// mod test;
|
mod test;
|
||||||
|
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
struct Displayed<T>(T);
|
struct Displayed<T>(T);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use klondike::Klondike;
|
|
||||||
use card_game::Session;
|
use card_game::Session;
|
||||||
|
use klondike::Klondike;
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_winnable() {
|
fn test_is_winnable() {
|
||||||
// is winnable
|
// is winnable
|
||||||
|
|||||||
Reference in New Issue
Block a user