fini
This commit is contained in:
+20
-3
@@ -269,7 +269,7 @@ pub struct SessionState<G: Game> {
|
|||||||
impl<G: Game> Session<G>
|
impl<G: Game> Session<G>
|
||||||
where
|
where
|
||||||
G: Clone + Eq + core::hash::Hash,
|
G: Clone + Eq + core::hash::Hash,
|
||||||
G::Stats: Clone,
|
G::Stats: Clone + Default,
|
||||||
G::Instruction: Clone + Eq + core::hash::Hash,
|
G::Instruction: Clone + Eq + core::hash::Hash,
|
||||||
{
|
{
|
||||||
pub fn new(state: G, stats: G::Stats, config: G::Config) -> Self {
|
pub fn new(state: G, stats: G::Stats, config: G::Config) -> Self {
|
||||||
@@ -288,17 +288,34 @@ where
|
|||||||
}
|
}
|
||||||
pub fn new_default(state: G) -> Self
|
pub fn new_default(state: G) -> Self
|
||||||
where
|
where
|
||||||
G::Stats: Default,
|
|
||||||
G::Config: Default,
|
G::Config: Default,
|
||||||
{
|
{
|
||||||
Self::new(state, Default::default(), Default::default())
|
Self::new(state, Default::default(), Default::default())
|
||||||
}
|
}
|
||||||
pub fn state(&self) -> &G {
|
pub const fn state(&self) -> &G {
|
||||||
&self.state.state
|
&self.state.state
|
||||||
}
|
}
|
||||||
|
pub const fn config(&self) -> &G::Config {
|
||||||
|
&self.config
|
||||||
|
}
|
||||||
pub fn history(&self) -> &[G::Instruction] {
|
pub fn history(&self) -> &[G::Instruction] {
|
||||||
&self.state.history
|
&self.state.history
|
||||||
}
|
}
|
||||||
|
pub fn undo(&mut self) {
|
||||||
|
self.state
|
||||||
|
.process_instruction(&mut self.stats, &self.config, SessionInstruction::Undo)
|
||||||
|
}
|
||||||
|
pub fn possible_instructions(&self) -> impl Iterator<Item = G::Instruction> + use<G> {
|
||||||
|
self.state.state.possible_instructions()
|
||||||
|
}
|
||||||
|
pub fn process_instruction(&mut self, instruction: G::Instruction) {
|
||||||
|
self.state
|
||||||
|
.state
|
||||||
|
.process_instruction(&mut self.stats.inner_stats, &self.config, instruction)
|
||||||
|
}
|
||||||
|
pub fn is_win(&self) -> bool {
|
||||||
|
self.state.is_win()
|
||||||
|
}
|
||||||
pub fn is_winnable(&self) -> Option<Vec<G::Instruction>> {
|
pub fn is_winnable(&self) -> Option<Vec<G::Instruction>> {
|
||||||
let mut observed = std::collections::HashSet::new();
|
let mut observed = std::collections::HashSet::new();
|
||||||
struct StateMachine<G, P, I> {
|
struct StateMachine<G, P, I> {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use card_game::{Card, CardValue, Game, Pile, Session, Suit};
|
use card_game::{Card, CardValue, Game, Pile, Session, Suit};
|
||||||
use klondike::{
|
use klondike::{
|
||||||
DstFoundation, DstTableau, Foundation, Klondike, KlondikeInstruction, KlondikePile,
|
DstFoundation, DstTableau, Foundation, Klondike, KlondikeConfig, KlondikeInstruction,
|
||||||
KlondikePileStack, SkipCards, Tableau, TableauStack,
|
KlondikePile, KlondikePileStack, SkipCards, Tableau, TableauStack,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
@@ -144,6 +144,7 @@ impl core::str::FromStr for SessionInstruction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn find_valid_instruction(
|
fn find_valid_instruction(
|
||||||
|
config: &KlondikeConfig,
|
||||||
state: &Klondike,
|
state: &Klondike,
|
||||||
naive_instruction: NaiveInstruction,
|
naive_instruction: NaiveInstruction,
|
||||||
) -> Option<KlondikeInstruction> {
|
) -> Option<KlondikeInstruction> {
|
||||||
@@ -173,7 +174,7 @@ fn find_valid_instruction(
|
|||||||
});
|
});
|
||||||
let instruction =
|
let instruction =
|
||||||
KlondikeInstruction::DstTableau(DstTableau { tableau, src });
|
KlondikeInstruction::DstTableau(DstTableau { tableau, src });
|
||||||
if state.is_instruction_valid(instruction) {
|
if state.is_instruction_valid(config, instruction) {
|
||||||
return Some(instruction);
|
return Some(instruction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,7 +192,7 @@ fn find_valid_instruction(
|
|||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
state
|
state
|
||||||
.is_instruction_valid(instruction)
|
.is_instruction_valid(config, instruction)
|
||||||
.then_some(instruction)
|
.then_some(instruction)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,7 +242,7 @@ fn get_good_move(state: &Klondike) -> Option<KlondikeInstruction> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), std::io::Error> {
|
fn main() -> Result<(), std::io::Error> {
|
||||||
let mut session = Session::new(Klondike::new_random());
|
let mut session = Session::new_default(Klondike::new_random());
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
loop {
|
loop {
|
||||||
// display game
|
// display game
|
||||||
@@ -257,7 +258,7 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
|
|
||||||
// run game
|
// run game
|
||||||
match instruction {
|
match instruction {
|
||||||
SessionInstruction::New => session = Session::new(Klondike::new_random()),
|
SessionInstruction::New => session = Session::new_default(Klondike::new_random()),
|
||||||
SessionInstruction::Undo => session.undo(),
|
SessionInstruction::Undo => session.undo(),
|
||||||
SessionInstruction::Exit => break Ok(()),
|
SessionInstruction::Exit => break Ok(()),
|
||||||
SessionInstruction::Hint => {
|
SessionInstruction::Hint => {
|
||||||
@@ -277,7 +278,7 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
}
|
}
|
||||||
SessionInstruction::Klondike(naive_instruction) => {
|
SessionInstruction::Klondike(naive_instruction) => {
|
||||||
if let Some(instruction) =
|
if let Some(instruction) =
|
||||||
find_valid_instruction(session.state(), naive_instruction)
|
find_valid_instruction(session.config(), session.state(), naive_instruction)
|
||||||
{
|
{
|
||||||
session.process_instruction(instruction);
|
session.process_instruction(instruction);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user