Implement Stats #6

Merged
Quaternions merged 6 commits from stats into master 2026-05-17 16:46:09 +00:00
2 changed files with 28 additions and 10 deletions
Showing only changes of commit 2f2910dce2 - Show all commits
+20 -3
View File
@@ -269,7 +269,7 @@ pub struct SessionState<G: Game> {
impl<G: Game> Session<G>
where
G: Clone + Eq + core::hash::Hash,
G::Stats: Clone,
G::Stats: Clone + Default,
G::Instruction: Clone + Eq + core::hash::Hash,
{
pub fn new(state: G, stats: G::Stats, config: G::Config) -> Self {
@@ -288,17 +288,34 @@ where
}
pub fn new_default(state: G) -> Self
where
G::Stats: Default,
G::Config: Default,
{
Self::new(state, Default::default(), Default::default())
}
pub fn state(&self) -> &G {
pub const fn state(&self) -> &G {
&self.state.state
}
pub const fn config(&self) -> &G::Config {
&self.config
}
pub fn history(&self) -> &[G::Instruction] {
&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>> {
let mut observed = std::collections::HashSet::new();
struct StateMachine<G, P, I> {
+8 -7
View File
@@ -1,7 +1,7 @@
use card_game::{Card, CardValue, Game, Pile, Session, Suit};
use klondike::{
DstFoundation, DstTableau, Foundation, Klondike, KlondikeInstruction, KlondikePile,
KlondikePileStack, SkipCards, Tableau, TableauStack,
DstFoundation, DstTableau, Foundation, Klondike, KlondikeConfig, KlondikeInstruction,
KlondikePile, KlondikePileStack, SkipCards, Tableau, TableauStack,
};
use std::fmt::Display;
@@ -144,6 +144,7 @@ impl core::str::FromStr for SessionInstruction {
}
fn find_valid_instruction(
config: &KlondikeConfig,
state: &Klondike,
naive_instruction: NaiveInstruction,
) -> Option<KlondikeInstruction> {
@@ -173,7 +174,7 @@ fn find_valid_instruction(
});
let instruction =
KlondikeInstruction::DstTableau(DstTableau { tableau, src });
if state.is_instruction_valid(instruction) {
if state.is_instruction_valid(config, instruction) {
return Some(instruction);
}
}
@@ -191,7 +192,7 @@ fn find_valid_instruction(
_ => return None,
};
state
.is_instruction_valid(instruction)
.is_instruction_valid(config, instruction)
.then_some(instruction)
}
@@ -241,7 +242,7 @@ fn get_good_move(state: &Klondike) -> Option<KlondikeInstruction> {
}
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();
loop {
// display game
@@ -257,7 +258,7 @@ fn main() -> Result<(), std::io::Error> {
// run game
match instruction {
SessionInstruction::New => session = Session::new(Klondike::new_random()),
SessionInstruction::New => session = Session::new_default(Klondike::new_random()),
SessionInstruction::Undo => session.undo(),
SessionInstruction::Exit => break Ok(()),
SessionInstruction::Hint => {
@@ -277,7 +278,7 @@ fn main() -> Result<(), std::io::Error> {
}
SessionInstruction::Klondike(naive_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);
} else {