diff --git a/card_game/src/lib.rs b/card_game/src/lib.rs index a894400..a089397 100644 --- a/card_game/src/lib.rs +++ b/card_game/src/lib.rs @@ -250,9 +250,15 @@ impl SessionStats { undos: 0, } } + pub const fn stats(&self) -> &S { + &self.inner_stats + } const fn increment_undos(&mut self) { self.undos += 1; } + pub const fn undos(&self) -> usize { + self.undos + } } pub struct Session { @@ -266,6 +272,15 @@ pub struct SessionState { state: G, history: Vec, } +impl SessionState { + fn new(state: G) -> Self { + Self { + seed: state.clone(), + state, + history: Vec::new(), + } + } +} impl Session where G: Clone + Eq + core::hash::Hash, @@ -274,16 +289,9 @@ where { pub fn new(state: G, stats: G::Stats, config: G::Config) -> Self { Self { - stats: SessionStats { - inner_stats: stats, - undos: 0, - }, + stats: SessionStats::new(stats), config, - state: SessionState { - seed: state.clone(), - state, - history: Vec::new(), - }, + state: SessionState::new(state), } } pub fn new_default(state: G) -> Self @@ -292,6 +300,9 @@ where { Self::new(state, Default::default(), Default::default()) } + pub const fn stats(&self) -> &SessionStats { + &self.stats + } pub const fn state(&self) -> &G { &self.state.state } diff --git a/klondike-cli/src/main.rs b/klondike-cli/src/main.rs index 2b6d0af..8b31111 100644 --- a/klondike-cli/src/main.rs +++ b/klondike-cli/src/main.rs @@ -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::{ DstFoundation, DstTableau, Foundation, Klondike, KlondikeConfig, KlondikeInstruction, - KlondikePile, KlondikePileStack, SkipCards, Tableau, TableauStack, + KlondikePile, KlondikePileStack, KlondikeStats, SkipCards, Tableau, TableauStack, }; use std::fmt::Display; @@ -83,6 +83,18 @@ impl Display for Displayed<&Klondike> { } } +impl Display for Displayed<&SessionStats> { + 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)] struct Invalid; struct Parsed(T); @@ -245,6 +257,8 @@ fn main() -> Result<(), std::io::Error> { let mut session = Session::new_default(Klondike::new_random()); let mut input = String::new(); loop { + // display stats + println!("{}", Displayed(session.stats())); // display game println!("{}", Displayed(session.state())); diff --git a/klondike/src/lib.rs b/klondike/src/lib.rs index 210bc95..c5176a9 100644 --- a/klondike/src/lib.rs +++ b/klondike/src/lib.rs @@ -46,6 +46,12 @@ impl KlondikeStats { 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) { self.recycle_count += 1; }