Moved all flush checks to single function and assigned values to hands with flushes
This commit is contained in:
parent
79978235b9
commit
765b292e24
2 changed files with 29 additions and 46 deletions
|
@ -12,7 +12,7 @@ pub enum HandSort {
|
||||||
pub struct Hand {
|
pub struct Hand {
|
||||||
pub cards: Vec<Card>,
|
pub cards: Vec<Card>,
|
||||||
pub card_sort: HandSort,
|
pub card_sort: HandSort,
|
||||||
pub hand_value: u8,
|
pub value: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hand {
|
impl Hand {
|
||||||
|
@ -20,7 +20,7 @@ impl Hand {
|
||||||
Hand {
|
Hand {
|
||||||
cards: Vec::new(),
|
cards: Vec::new(),
|
||||||
card_sort: HandSort::None,
|
card_sort: HandSort::None,
|
||||||
hand_value: 0,
|
value: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,18 +139,6 @@ impl Hand {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the current hand has a flush. \
|
|
||||||
/// A flush is a hand that contains five cards, all the same suit,
|
|
||||||
/// and not all of sequential rank, such as `K♣ 10♣ 7♣ 6♣ 4♣`
|
|
||||||
pub fn has_flush(&self) -> bool {
|
|
||||||
for i in 1..self.cards.len() {
|
|
||||||
if self.cards[i].suit != self.cards[i - 1].suit {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Checks if the current hand has a full house. \
|
/// Checks if the current hand has a full house. \
|
||||||
/// A full house is a hand that contains three cards of one
|
/// A full house is a hand that contains three cards of one
|
||||||
|
@ -198,36 +186,12 @@ impl Hand {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the current hand has a straight flush. \
|
|
||||||
/// A straight flush is a hand that contains five cards of sequential rank,
|
|
||||||
/// all the same suit, such as `Q♥ J♥ 10♥ 9♥ 8♥`
|
|
||||||
pub fn has_straight_flush(&mut self) -> bool {
|
|
||||||
if !matches!(self.card_sort, HandSort::Value) {
|
|
||||||
self.sort(HandSort::Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut has_flush = true;
|
/// Checks if the current hand has a flush \
|
||||||
let mut has_straight = true;
|
/// A flush is a hand that contains five cards of the same suit, such as `5♥ 8♥ 2♥ 10♥ 3♥` \
|
||||||
|
/// A straight flush is a hand that contains five cards of sequential rank, all the same suit, such as `Q♥ J♥ 10♥ 9♥ 8♥` \
|
||||||
let suit = &self.cards[0].suit;
|
|
||||||
|
|
||||||
for i in 1..self.cards.len() {
|
|
||||||
if &self.cards[i].suit != suit {
|
|
||||||
has_flush = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if self.cards[i].value != self.cards[i - 1].value + 1 {
|
|
||||||
has_straight = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
has_flush && has_straight
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Checks if the current hand has a royal flush, the best possible hand. \
|
|
||||||
/// A royal flush is a hand that contains an ace-high straight flush, such as `A♦ K♦ Q♦ J♦ 10♦`
|
/// A royal flush is a hand that contains an ace-high straight flush, such as `A♦ K♦ Q♦ J♦ 10♦`
|
||||||
pub fn has_royal_flush(&mut self) -> bool {
|
pub fn has_flush(&mut self) -> bool {
|
||||||
if !matches!(self.card_sort, HandSort::Value) {
|
if !matches!(self.card_sort, HandSort::Value) {
|
||||||
self.sort(HandSort::Value);
|
self.sort(HandSort::Value);
|
||||||
}
|
}
|
||||||
|
@ -247,6 +211,21 @@ impl Hand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
has_flush && has_straight && self.cards[0].value == 10
|
let mut hand_value = 0;
|
||||||
|
if has_flush && has_straight && self.cards[0].value == 10 { // Royal flush
|
||||||
|
hand_value = hand_value + 9;
|
||||||
|
} else if has_flush && has_straight { // Straight flush
|
||||||
|
hand_value = hand_value + 8;
|
||||||
|
} else if has_flush { // Flush
|
||||||
|
hand_value = hand_value + 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
for card in self.cards.iter() {
|
||||||
|
hand_value = hand_value + card.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.value = hand_value;
|
||||||
|
|
||||||
|
has_flush
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,7 +116,8 @@ mod tests {
|
||||||
hand.cards.push(Card { rank: "5".to_string(), value: 5, suit: "♥".to_string() });
|
hand.cards.push(Card { rank: "5".to_string(), value: 5, suit: "♥".to_string() });
|
||||||
hand.cards.push(Card { rank: "8".to_string(), value: 8, suit: "♥".to_string() });
|
hand.cards.push(Card { rank: "8".to_string(), value: 8, suit: "♥".to_string() });
|
||||||
|
|
||||||
assert_eq!(hand.has_flush(), true)
|
assert_eq!(hand.has_flush(), true);
|
||||||
|
assert_eq!(hand.value, 34);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -142,7 +143,8 @@ mod tests {
|
||||||
hand.cards.push(Card { rank: "5".to_string(), value: 5, suit: "♥".to_string() });
|
hand.cards.push(Card { rank: "5".to_string(), value: 5, suit: "♥".to_string() });
|
||||||
hand.cards.push(Card { rank: "6".to_string(), value: 6, suit: "♥".to_string() });
|
hand.cards.push(Card { rank: "6".to_string(), value: 6, suit: "♥".to_string() });
|
||||||
|
|
||||||
assert_eq!(hand.has_straight_flush(), true)
|
assert_eq!(hand.has_flush(), true);
|
||||||
|
assert_eq!(hand.value, 28);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -154,7 +156,9 @@ mod tests {
|
||||||
hand.cards.push(Card { rank: "Q".to_string(), value: 12, suit: "♥".to_string() });
|
hand.cards.push(Card { rank: "Q".to_string(), value: 12, suit: "♥".to_string() });
|
||||||
hand.cards.push(Card { rank: "K".to_string(), value: 13, suit: "♥".to_string() });
|
hand.cards.push(Card { rank: "K".to_string(), value: 13, suit: "♥".to_string() });
|
||||||
hand.cards.push(Card { rank: "A".to_string(), value: 14, suit: "♥".to_string() });
|
hand.cards.push(Card { rank: "A".to_string(), value: 14, suit: "♥".to_string() });
|
||||||
|
//9
|
||||||
|
|
||||||
assert_eq!(hand.has_royal_flush(), true)
|
assert_eq!(hand.has_flush(), true);
|
||||||
|
assert_eq!(hand.value, 69);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue