modes:triple: better Triple::new
Using Card::rank we can do something similar to Pair::new.
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
use crate::{
|
||||
card::{Card, PlayingCard},
|
||||
helper::ordered,
|
||||
};
|
||||
use crate::{card::Card, helper::ordered};
|
||||
|
||||
#[derive(Eq, Debug, Copy, Clone)]
|
||||
pub struct Triple(Card, Card, Card);
|
||||
@@ -10,21 +7,17 @@ impl Triple {
|
||||
pub fn new(c1: Card, c2: Card, c3: Card) -> Option<Triple> {
|
||||
let [c1, c2, c3] = ordered([c1, c2, c3]);
|
||||
|
||||
match (c1, c2, c3) {
|
||||
(Card::Joker(_), Card::Joker(_), Card::Joker(_)) => None,
|
||||
// NOTE: c3 should not be a joker now.
|
||||
(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)),
|
||||
match [c1, c2, c3].map(|c| c.rank()) {
|
||||
// Two Jokers + any PlayingCard
|
||||
[None, None, Some(_)] => Some(Triple(c1, c2, c3)),
|
||||
|
||||
(
|
||||
Card::PlayingCard(PlayingCard { rank: r1, .. }),
|
||||
Card::PlayingCard(PlayingCard { rank: r2, .. }),
|
||||
Card::PlayingCard(PlayingCard { rank: r3, .. }),
|
||||
) if r1 == r2 && r2 == r3 => Some(Triple(c1, c2, c3)),
|
||||
// One Joker + two PlayingCards of the same rank
|
||||
[None, Some(r2), Some(r3)] if r2 == r3 => Some(Triple(c1, c2, c3)),
|
||||
|
||||
// Three PlayingCards of the same rank
|
||||
[Some(r1), Some(r2), Some(r3)] if r1 == r2 && r2 == r3 => {
|
||||
Some(Triple(c1, c2, c3))
|
||||
}
|
||||
|
||||
_ => None,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user