From 765b292e24fb74f9668137e35c114be7cf47f133 Mon Sep 17 00:00:00 2001 From: "Edward Tirado Jr." Date: Sat, 24 May 2025 17:03:44 -0500 Subject: [PATCH] Moved all flush checks to single function and assigned values to hands with flushes --- src/models/hand.rs | 65 ++++++++++++++---------------------------- src/tests/hand_test.rs | 10 +++++-- 2 files changed, 29 insertions(+), 46 deletions(-) diff --git a/src/models/hand.rs b/src/models/hand.rs index 11d12c9..3b155ba 100644 --- a/src/models/hand.rs +++ b/src/models/hand.rs @@ -12,7 +12,7 @@ pub enum HandSort { pub struct Hand { pub cards: Vec, pub card_sort: HandSort, - pub hand_value: u8, + pub value: u8, } impl Hand { @@ -20,7 +20,7 @@ impl Hand { Hand { cards: Vec::new(), card_sort: HandSort::None, - hand_value: 0, + value: 0, } } @@ -139,18 +139,6 @@ impl Hand { 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. \ /// A full house is a hand that contains three cards of one @@ -198,36 +186,12 @@ impl Hand { 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; - let mut has_straight = true; - - 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. \ + /// Checks if the current hand has a flush \ + /// 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♥` \ /// 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) { 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 } } diff --git a/src/tests/hand_test.rs b/src/tests/hand_test.rs index 506ba9f..12d0653 100644 --- a/src/tests/hand_test.rs +++ b/src/tests/hand_test.rs @@ -116,7 +116,8 @@ mod tests { 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() }); - assert_eq!(hand.has_flush(), true) + assert_eq!(hand.has_flush(), true); + assert_eq!(hand.value, 34); } #[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: "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] @@ -154,7 +156,9 @@ mod tests { 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: "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); } } \ No newline at end of file