From 10e7da7b429085b86b7d157514e4eedb9f426bd8 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Thu, 13 Jul 2023 12:24:56 -0700 Subject: [PATCH 1/1] Add jokers --- src/lib.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b7dc954..c3736e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,8 @@ use rand::Rng; pub const NUM_RANKS: u8 = 13; pub const NUM_SUITS: u8 = 4; -pub const NUM_CARDS: u8 = NUM_RANKS * NUM_SUITS; +pub const NUM_JOKERS: u8 = 2; +pub const NUM_CARDS: u8 = NUM_RANKS * NUM_SUITS + NUM_JOKERS; #[derive(Clone, Copy, Eq, PartialEq)] pub struct Rank(u8); @@ -14,18 +15,32 @@ pub struct Suit(u8); pub struct Card(u8); impl Card { #[must_use] - pub fn rank(&self) -> Rank { - Rank(self.0 >> 2) + pub fn is_joker(&self) -> bool { + self.0 >= NUM_RANKS * NUM_SUITS } #[must_use] - pub fn suit(&self) -> Suit { - Suit(self.0 & 3) + pub fn rank(&self) -> Option { + (!self.is_joker()).then_some(Rank(self.0 >> 2)) } + #[must_use] + pub fn suit(&self) -> Option { + (!self.is_joker()).then_some(Suit(self.0 & 3)) + } +} + +#[derive(Clone, Copy)] +pub enum WithOrWithoutJokers { + WithJokers, + WithoutJokers, } #[must_use] -pub fn deck() -> Vec { - (0..NUM_CARDS).map(Card).collect() +pub fn deck(j: WithOrWithoutJokers) -> Vec { + let limit = match j { + WithOrWithoutJokers::WithJokers => NUM_CARDS, + WithOrWithoutJokers::WithoutJokers => NUM_SUITS * NUM_RANKS, + }; + (0..limit).map(Card).collect() } #[derive(Clone, Copy)] @@ -87,6 +102,8 @@ mod tests { #[test] fn test_deck() { - let _d = deck(); + use WithOrWithoutJokers::*; + let _d = deck(WithoutJokers); + let _dj = deck(WithJokers); } } -- 2.44.1