From e1db8af26c1a7507812e20bf41f3bd3844cddf33 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Thu, 2 Apr 2026 06:00:31 +0100 Subject: [PATCH] modes:pair: Added pair type Slightly more complex pattern since we've got two cards, but still relatively trivial. --- src/modes/pair.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/modes/pair.rs diff --git a/src/modes/pair.rs b/src/modes/pair.rs new file mode 100644 index 0000000..f7050ba --- /dev/null +++ b/src/modes/pair.rs @@ -0,0 +1,33 @@ +use crate::card::{Card, PlayingCard}; +use crate::helper::ordered; + +#[derive(Debug, Copy, Clone)] +pub struct Pair(Card, Card); + +impl Pair { + fn new(c1: Card, c2: Card) -> Option { + // Order the cards. This means if xor(c1 is joker, c2 is joker) c1 will + // be that joker. + let (c1, c2) = ordered(c1, c2); + + match (c1, c2) { + // Can't be a pair if you got two jokers homie. + (Card::Joker(_), Card::Joker(_)) => None, + + // NOTE: c2 cannot be a joker because of prev condition. If you've + // got a joker you're automatically a pair. + (Card::Joker(_), _) => Some(Pair(c1, c2)), + + // NOTE: c1 and c2 cannot be jokers. In which case, check their + // ranks are equivalent. + ( + Card::PlayingCard(PlayingCard { rank: r1, .. }), + Card::PlayingCard(PlayingCard { rank: r2, .. }), + ) => (r1 == r2).then_some(Pair(c1, c2)), + + // Not necessary since the previous patterns technically cover all + // cases. But I love the compiler too much to tell them... 💔 + _ => None, + } + } +}