X-Git-Url: http://git.scottworley.com/pluta-lesnura/blobdiff_plain/57f490a06fe793a25172ea5f6a13945c363cc6aa..10e7da7b429085b86b7d157514e4eedb9f426bd8:/src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index a5148a0..c3736e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,48 @@ use rand::Rng; pub const NUM_RANKS: u8 = 13; +pub const NUM_SUITS: u8 = 4; +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); +#[derive(Clone, Copy, Eq, PartialEq)] +pub struct Suit(u8); + +#[derive(Clone, Copy, Eq, PartialEq)] +pub struct Card(u8); +impl Card { + #[must_use] + pub fn is_joker(&self) -> bool { + self.0 >= NUM_RANKS * NUM_SUITS + } + #[must_use] + 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(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)] pub struct PathLength(Rank); @@ -61,4 +99,11 @@ mod tests { } assert!(pli.reveal_random(length).is_none()); } + + #[test] + fn test_deck() { + use WithOrWithoutJokers::*; + let _d = deck(WithoutJokers); + let _dj = deck(WithJokers); + } }