card: implement PlayingCard|Card::{prev,next}
Returns the next or previous card based on deck ordering. Respects the deck of the current card (i.e. will return a card from the same deck) but will return None on the boundaries of the deck (3(D) and 2(S)).
This commit is contained in:
50
src/card.rs
50
src/card.rs
@@ -83,6 +83,36 @@ impl PlayingCard {
|
|||||||
((deck * 52)..((deck + 1) * 52))
|
((deck * 52)..((deck + 1) * 52))
|
||||||
.filter_map(|x| PlayingCard::try_from(x).ok())
|
.filter_map(|x| PlayingCard::try_from(x).ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Return the Playing Card after the current one in terms of ordering.
|
||||||
|
|
||||||
|
Returns None if self is 2 of Spades (the highest possible Playing Card).
|
||||||
|
Respects deck of self. */
|
||||||
|
pub fn next(&self) -> Option<PlayingCard> {
|
||||||
|
match *self {
|
||||||
|
PlayingCard {
|
||||||
|
rank: Rank::Two,
|
||||||
|
suit: Suit::Spade,
|
||||||
|
..
|
||||||
|
} => None,
|
||||||
|
card => PlayingCard::try_from(i64::from(card) + 1).ok(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return the Playing Card before the current one in terms of ordering.
|
||||||
|
|
||||||
|
Returns None if self is 3 of Diamonds (the lowest possible Playing Card).
|
||||||
|
Respects deck of self. */
|
||||||
|
pub fn prev(&self) -> Option<PlayingCard> {
|
||||||
|
match *self {
|
||||||
|
PlayingCard {
|
||||||
|
rank: Rank::Three,
|
||||||
|
suit: Suit::Diamond,
|
||||||
|
..
|
||||||
|
} => None,
|
||||||
|
card => PlayingCard::try_from(i64::from(card) - 1).ok(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Card {
|
impl Card {
|
||||||
@@ -115,6 +145,26 @@ impl Card {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Return the Card after the current one in terms of ordering.
|
||||||
|
|
||||||
|
Returns None if self is a joker, or based on PlayingCard::next.*/
|
||||||
|
pub fn next(&self) -> Option<Card> {
|
||||||
|
match *self {
|
||||||
|
Card::Joker(_) => None,
|
||||||
|
Card::PlayingCard(card) => card.next().map(Card::PlayingCard),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return the Card before the current one in terms of ordering.
|
||||||
|
|
||||||
|
Returns None if self is a joker, or based on PlayingCard::prev.*/
|
||||||
|
pub fn prev(&self) -> Option<Card> {
|
||||||
|
match *self {
|
||||||
|
Card::Joker(_) => None,
|
||||||
|
Card::PlayingCard(card) => card.prev().map(Card::PlayingCard),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_joker(&self) -> bool {
|
pub fn is_joker(&self) -> bool {
|
||||||
matches!(self, Self::Joker(_))
|
matches!(self, Self::Joker(_))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user