modes:triple: better Triple::new

Using Card::rank we can do something similar to Pair::new.
This commit is contained in:
2026-04-09 19:19:45 +01:00
committed by oreodave
parent d993f72958
commit 6a4860643f

View File

@@ -1,7 +1,4 @@
use crate::{ use crate::{card::Card, helper::ordered};
card::{Card, PlayingCard},
helper::ordered,
};
#[derive(Eq, Debug, Copy, Clone)] #[derive(Eq, Debug, Copy, Clone)]
pub struct Triple(Card, Card, Card); pub struct Triple(Card, Card, Card);
@@ -10,21 +7,17 @@ impl Triple {
pub fn new(c1: Card, c2: Card, c3: Card) -> Option<Triple> { pub fn new(c1: Card, c2: Card, c3: Card) -> Option<Triple> {
let [c1, c2, c3] = ordered([c1, c2, c3]); let [c1, c2, c3] = ordered([c1, c2, c3]);
match (c1, c2, c3) { match [c1, c2, c3].map(|c| c.rank()) {
(Card::Joker(_), Card::Joker(_), Card::Joker(_)) => None, // Two Jokers + any PlayingCard
// NOTE: c3 should not be a joker now. [None, None, Some(_)] => Some(Triple(c1, c2, c3)),
(Card::Joker(_), Card::Joker(_), _) => Some(Triple(c1, c2, c3)),
(
Card::Joker(_),
Card::PlayingCard(PlayingCard { rank: r1, .. }),
Card::PlayingCard(PlayingCard { rank: r2, .. }),
) if r1 == r2 => Some(Triple(c1, c2, c3)),
( // One Joker + two PlayingCards of the same rank
Card::PlayingCard(PlayingCard { rank: r1, .. }), [None, Some(r2), Some(r3)] if r2 == r3 => Some(Triple(c1, c2, c3)),
Card::PlayingCard(PlayingCard { rank: r2, .. }),
Card::PlayingCard(PlayingCard { rank: r3, .. }), // Three PlayingCards of the same rank
) if r1 == r2 && r2 == r3 => Some(Triple(c1, c2, c3)), [Some(r1), Some(r2), Some(r3)] if r1 == r2 && r2 == r3 => {
Some(Triple(c1, c2, c3))
}
_ => None, _ => None,
} }