* Reorder PokerHand to be sorted highest first
+ Impliment PartialOrd for OnePair
This commit is contained in:
49
src/hand.rs
49
src/hand.rs
@@ -15,6 +15,7 @@ pub enum HandType {
|
||||
StraightFlush,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct PokerHand {
|
||||
cards: Vec<PlayingCard>,
|
||||
}
|
||||
@@ -44,10 +45,51 @@ impl fmt::Display for PokerHand {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for PokerHand {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
if self.hand_type() > other.hand_type() {
|
||||
return self.hand_type().partial_cmp(&other.hand_type());
|
||||
}
|
||||
let selfmap = self.hash_map();
|
||||
let othermap = other.hash_map();
|
||||
match self.hand_type() {
|
||||
HandType::HighCard => {
|
||||
// Compare high cards
|
||||
self.cards.partial_cmp(&other.cards)
|
||||
}
|
||||
HandType::OnePair => {
|
||||
let selfpair: Vec<(&CardValue, &u8)> =
|
||||
selfmap.iter().filter(|&p| *p.1 == 2).collect();
|
||||
let otherpair: Vec<(&CardValue, &u8)> =
|
||||
othermap.iter().filter(|&p| *p.1 == 2).collect();
|
||||
|
||||
let pairvalue = selfpair[0].0;
|
||||
let otherpairvalue = otherpair[0].0;
|
||||
|
||||
if pairvalue != otherpairvalue {
|
||||
return pairvalue.partial_cmp(otherpairvalue);
|
||||
}
|
||||
|
||||
self.cards.partial_cmp(&other.cards)
|
||||
}
|
||||
HandType::TwoPair => todo!(),
|
||||
HandType::ThreeOfAKind => todo!(),
|
||||
HandType::Straight => todo!(),
|
||||
HandType::Flush => todo!(),
|
||||
HandType::FullHouse => todo!(),
|
||||
HandType::FourOfAKind => todo!(),
|
||||
HandType::StraightFlush => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PokerHand {
|
||||
fn new(mut cards: Vec<PlayingCard>) -> Self {
|
||||
assert!(cards.len() == 5);
|
||||
|
||||
// Sorted highest card first
|
||||
cards.sort_unstable();
|
||||
cards.reverse();
|
||||
PokerHand { cards }
|
||||
}
|
||||
|
||||
@@ -190,12 +232,11 @@ fn is_flush(hand: &PokerHand) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// This function relies on the fact that the vector of cards is sotred
|
||||
fn is_straight(hand: &PokerHand) -> bool {
|
||||
let lowest_val = hand.cards[0].value;
|
||||
|
||||
for i in 1..4 {
|
||||
if (hand.cards[i].value >= lowest_val) {
|
||||
let current_card = hand.cards[i].value as u8;
|
||||
let previous_card = hand.cards[i].value as u8;
|
||||
if current_card != previous_card - 1 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user