From ab3cf9b3f3b4dcc71f49368b731747f9624c289b Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Fri, 15 May 2026 09:01:03 -0700 Subject: [PATCH] fix ace to foundation --- src/card_game.rs | 13 +++++++++++++ src/klondike.rs | 22 +++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/card_game.rs b/src/card_game.rs index ec6751f..23f4d66 100644 --- a/src/card_game.rs +++ b/src/card_game.rs @@ -29,6 +29,19 @@ impl Suit { #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct CardValue(u8); impl CardValue { + pub const ACE: Self = CardValue(1); + pub const TWO: Self = CardValue(2); + pub const THREE: Self = CardValue(3); + pub const FOUR: Self = CardValue(4); + pub const FIVE: Self = CardValue(5); + pub const SIX: Self = CardValue(6); + pub const SEVEN: Self = CardValue(7); + pub const EIGHT: Self = CardValue(8); + pub const NINE: Self = CardValue(9); + pub const TEN: Self = CardValue(10); + pub const JACK: Self = CardValue(11); + pub const QUEEN: Self = CardValue(12); + pub const KING: Self = CardValue(13); pub fn checked_add(self, offset: u8) -> Option { let new_value = self.0.checked_add(offset)?; if 13 < new_value { diff --git a/src/klondike.rs b/src/klondike.rs index 3759caa..fbd78e6 100644 --- a/src/klondike.rs +++ b/src/klondike.rs @@ -1,5 +1,5 @@ use crate::Rng; -use crate::card_game::{Game, Pile, Stack}; +use crate::card_game::{CardValue, Game, Pile, Stack}; #[derive(Clone, Debug)] pub struct KlondikeConfig {} @@ -106,14 +106,18 @@ impl KlondikeState { ) => { // get the top cards - if let Some(src_card) = self.pile(src).face_up().last() - && let Some(dst_card) = self.pile(dst).face_up().last() - // suit matches? - && src_card.suit() == dst_card.suit() - // value is +1? - && dst_card.value().checked_add(1) == Some(src_card.value()) - { - true + if let Some(src_card) = self.pile(src).face_up().last() { + match self.pile(dst).face_up().last() { + // destination card exists + Some(dst_card) => { + // suit matches? + src_card.suit() == dst_card.suit() + // value is +1? + && dst_card.value().checked_add(1) == Some(src_card.value()) + } + // only ace is allowed to go onto empty foundation + None => src_card.value() == CardValue::ACE, + } } else { false }