implement score

This commit is contained in:
2026-05-18 12:01:32 -07:00
parent fc62da992e
commit fef4fe4d55
2 changed files with 20 additions and 2 deletions
+3 -2
View File
@@ -87,10 +87,11 @@ impl Display for Displayed<&SessionStats<KlondikeStats>> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(
f, f,
"recycles: {} moves: {} undos: {}", "recycles: {} moves: {} undos: {} score:{}",
self.0.stats().recycle_count(), self.0.stats().recycle_count(),
self.0.stats().moves(), self.0.stats().moves(),
self.0.undos() self.0.undos(),
self.0.stats().score() - self.0.undos() * 15,
) )
} }
} }
+17
View File
@@ -24,22 +24,35 @@ pub struct KlondikeConfig {
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct KlondikeStats { pub struct KlondikeStats {
score: usize,
recycle_count: usize, recycle_count: usize,
moves: usize, moves: usize,
} }
impl KlondikeStats { impl KlondikeStats {
pub const fn new() -> Self { pub const fn new() -> Self {
KlondikeStats { KlondikeStats {
score: 0,
recycle_count: 0, recycle_count: 0,
moves: 0, moves: 0,
} }
} }
pub const fn score(&self) -> usize {
self.score
}
pub const fn recycle_count(&self) -> usize { pub const fn recycle_count(&self) -> usize {
self.recycle_count self.recycle_count
} }
pub const fn moves(&self) -> usize { pub const fn moves(&self) -> usize {
self.moves 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) { const fn increment_recycle_count(&mut self) {
self.recycle_count += 1; self.recycle_count += 1;
} }
@@ -597,11 +610,15 @@ impl Game for Klondike {
} }
// Move a card from anywhere to a foundation // Move a card from anywhere to a foundation
KlondikeInstruction::DstFoundation(DstFoundation { src, foundation }) => { KlondikeInstruction::DstFoundation(DstFoundation { src, foundation }) => {
stats.increment_score_foundation();
let card = self.state.take_top_card(src); let card = self.state.take_top_card(src);
self.state.extend_foundation(foundation, card); self.state.extend_foundation(foundation, card);
} }
// Move a stack of cards from anywhere to a tableau // Move a stack of cards from anywhere to a tableau
KlondikeInstruction::DstTableau(DstTableau { src, tableau }) => { KlondikeInstruction::DstTableau(DstTableau { src, tableau }) => {
if src == KlondikePileStack::Stock {
stats.increment_score_tableau();
}
let cards = self.state.take_stack(src); let cards = self.state.take_stack(src);
self.state.extend_tableau(tableau, cards); self.state.extend_tableau(tableau, cards);
} }