From fef4fe4d55cd561c68f22a504ebcaca207c88f4e Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Mon, 18 May 2026 12:01:32 -0700 Subject: [PATCH] implement score --- klondike-cli/src/main.rs | 5 +++-- klondike/src/lib.rs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/klondike-cli/src/main.rs b/klondike-cli/src/main.rs index 1821081..87ec66e 100644 --- a/klondike-cli/src/main.rs +++ b/klondike-cli/src/main.rs @@ -87,10 +87,11 @@ impl Display for Displayed<&SessionStats> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, - "recycles: {} moves: {} undos: {}", + "recycles: {} moves: {} undos: {} score:{}", self.0.stats().recycle_count(), self.0.stats().moves(), - self.0.undos() + self.0.undos(), + self.0.stats().score() - self.0.undos() * 15, ) } } diff --git a/klondike/src/lib.rs b/klondike/src/lib.rs index 2ebbbe0..978e733 100644 --- a/klondike/src/lib.rs +++ b/klondike/src/lib.rs @@ -24,22 +24,35 @@ pub struct KlondikeConfig { #[derive(Clone, Debug, Default)] pub struct KlondikeStats { + score: usize, recycle_count: usize, moves: usize, } impl KlondikeStats { pub const fn new() -> Self { KlondikeStats { + score: 0, recycle_count: 0, moves: 0, } } + pub const fn score(&self) -> usize { + self.score + } pub const fn recycle_count(&self) -> usize { self.recycle_count } pub const fn moves(&self) -> usize { self.moves } + /// A card was moved to a foundation. + const fn increment_score_foundation(&mut self) { + self.score += 10; + } + /// A card was moved from stock to tableau. + const fn increment_score_tableau(&mut self) { + self.score += 5; + } const fn increment_recycle_count(&mut self) { self.recycle_count += 1; } @@ -597,11 +610,15 @@ impl Game for Klondike { } // Move a card from anywhere to a foundation KlondikeInstruction::DstFoundation(DstFoundation { src, foundation }) => { + stats.increment_score_foundation(); let card = self.state.take_top_card(src); self.state.extend_foundation(foundation, card); } // Move a stack of cards from anywhere to a tableau KlondikeInstruction::DstTableau(DstTableau { src, tableau }) => { + if src == KlondikePileStack::Stock { + stats.increment_score_tableau(); + } let cards = self.state.take_stack(src); self.state.extend_tableau(tableau, cards); }