From 55fbeb43a0370bbfb7e2646b06491310f54621c4 Mon Sep 17 00:00:00 2001 From: nalydmerc Date: Sat, 23 Mar 2024 13:50:38 -0400 Subject: [PATCH] Finish implimenting PartialOrd for PokerHand --- src/hand.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/src/hand.rs b/src/hand.rs index cb152f7..d6a80a5 100644 --- a/src/hand.rs +++ b/src/hand.rs @@ -89,12 +89,55 @@ impl PartialOrd for PokerHand { self.cards.partial_cmp(&other.cards) } - HandType::ThreeOfAKind => todo!(), - HandType::Straight => todo!(), - HandType::Flush => todo!(), - HandType::FullHouse => todo!(), - HandType::FourOfAKind => todo!(), - HandType::StraightFlush => todo!(), + HandType::ThreeOfAKind => { + let selfpairs: Vec<(&CardValue, &u8)> = + selfmap.iter().filter(|&p| *p.1 == 3).collect(); + let otherpairs: Vec<(&CardValue, &u8)> = + othermap.iter().filter(|&p| *p.1 == 3).collect(); + + let pairvalue = selfpairs[0].0; + let otherpairvalue = otherpairs[0].0; + + if pairvalue != otherpairvalue { + return pairvalue.partial_cmp(otherpairvalue); + } + + self.cards.partial_cmp(&other.cards) + } + HandType::Straight => self.cards.partial_cmp(&other.cards), + HandType::Flush => self.cards.partial_cmp(&other.cards), + HandType::FullHouse => { + let threes: Vec<(&CardValue, &u8)> = + selfmap.iter().filter(|&p| *p.1 == 3).collect(); + let otherthrees: Vec<(&CardValue, &u8)> = + othermap.iter().filter(|&p| *p.1 == 3).collect(); + + if threes[0].0 != otherthrees[0].0 { + return threes[0].0.partial_cmp(otherthrees[0].0); + } + + let twos: Vec<(&CardValue, &u8)> = selfmap.iter().filter(|&p| *p.1 == 2).collect(); + let othertwos: Vec<(&CardValue, &u8)> = + othermap.iter().filter(|&p| *p.1 == 2).collect(); + + if twos[0].0 != othertwos[0].0 { + return twos[0].0.partial_cmp(othertwos[0].0); + } + + self.cards.partial_cmp(&other.cards) + } + HandType::FourOfAKind => { + let fours: Vec<(&CardValue, &u8)> = selfmap.iter().filter(|&p| *p.1 == 4).collect(); + let otherfours: Vec<(&CardValue, &u8)> = + othermap.iter().filter(|&p| *p.1 == 4).collect(); + + if fours[0].0 != otherfours[0].0 { + return fours[0].0.partial_cmp(otherfours[0].0); + } + + self.cards.partial_cmp(&other.cards) + } + HandType::StraightFlush => self.cards.partial_cmp(&other.cards), } } }