display stats

This commit is contained in:
2026-05-17 09:37:52 -07:00
parent 2f2910dce2
commit 97cc81987f
3 changed files with 42 additions and 11 deletions
+20 -9
View File
@@ -250,9 +250,15 @@ impl<S> SessionStats<S> {
undos: 0, undos: 0,
} }
} }
pub const fn stats(&self) -> &S {
&self.inner_stats
}
const fn increment_undos(&mut self) { const fn increment_undos(&mut self) {
self.undos += 1; self.undos += 1;
} }
pub const fn undos(&self) -> usize {
self.undos
}
} }
pub struct Session<G: Game> { pub struct Session<G: Game> {
@@ -266,6 +272,15 @@ pub struct SessionState<G: Game> {
state: G, state: G,
history: Vec<G::Instruction>, history: Vec<G::Instruction>,
} }
impl<G: Game + Clone> SessionState<G> {
fn new(state: G) -> Self {
Self {
seed: state.clone(),
state,
history: Vec::new(),
}
}
}
impl<G: Game> Session<G> impl<G: Game> Session<G>
where where
G: Clone + Eq + core::hash::Hash, G: Clone + Eq + core::hash::Hash,
@@ -274,16 +289,9 @@ where
{ {
pub fn new(state: G, stats: G::Stats, config: G::Config) -> Self { pub fn new(state: G, stats: G::Stats, config: G::Config) -> Self {
Self { Self {
stats: SessionStats { stats: SessionStats::new(stats),
inner_stats: stats,
undos: 0,
},
config, config,
state: SessionState { state: SessionState::new(state),
seed: state.clone(),
state,
history: Vec::new(),
},
} }
} }
pub fn new_default(state: G) -> Self pub fn new_default(state: G) -> Self
@@ -292,6 +300,9 @@ where
{ {
Self::new(state, Default::default(), Default::default()) Self::new(state, Default::default(), Default::default())
} }
pub const fn stats(&self) -> &SessionStats<G::Stats> {
&self.stats
}
pub const fn state(&self) -> &G { pub const fn state(&self) -> &G {
&self.state.state &self.state.state
} }
+16 -2
View File
@@ -1,7 +1,7 @@
use card_game::{Card, CardValue, Game, Pile, Session, Suit}; use card_game::{Card, CardValue, Game, Pile, Session, SessionStats, Suit};
use klondike::{ use klondike::{
DstFoundation, DstTableau, Foundation, Klondike, KlondikeConfig, KlondikeInstruction, DstFoundation, DstTableau, Foundation, Klondike, KlondikeConfig, KlondikeInstruction,
KlondikePile, KlondikePileStack, SkipCards, Tableau, TableauStack, KlondikePile, KlondikePileStack, KlondikeStats, SkipCards, Tableau, TableauStack,
}; };
use std::fmt::Display; use std::fmt::Display;
@@ -83,6 +83,18 @@ impl Display for Displayed<&Klondike> {
} }
} }
impl Display for Displayed<&SessionStats<KlondikeStats>> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"recycles: {} moves: {} undos: {}",
self.0.stats().recycle_count(),
self.0.stats().moves(),
self.0.undos()
)
}
}
#[derive(Debug)] #[derive(Debug)]
struct Invalid; struct Invalid;
struct Parsed<T>(T); struct Parsed<T>(T);
@@ -245,6 +257,8 @@ fn main() -> Result<(), std::io::Error> {
let mut session = Session::new_default(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 stats
println!("{}", Displayed(session.stats()));
// display game // display game
println!("{}", Displayed(session.state())); println!("{}", Displayed(session.state()));
+6
View File
@@ -46,6 +46,12 @@ impl KlondikeStats {
moves: 0, moves: 0,
} }
} }
pub const fn recycle_count(&self) -> usize {
self.recycle_count
}
pub const fn moves(&self) -> usize {
self.moves
}
const fn increment_recycle_count(&mut self) { const fn increment_recycle_count(&mut self) {
self.recycle_count += 1; self.recycle_count += 1;
} }