Much work fleshing out cards and hands.
This commit is contained in:
174
src/hand.rs
Normal file
174
src/hand.rs
Normal file
@@ -0,0 +1,174 @@
|
||||
use crate::card::{CardValue, PlayingCard};
|
||||
use core::fmt;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
|
||||
pub enum HandType {
|
||||
HighCard,
|
||||
OnePair,
|
||||
TwoPair,
|
||||
ThreeOfAKind,
|
||||
Straight,
|
||||
Flush,
|
||||
FullHouse,
|
||||
FourOfAKind,
|
||||
StraightFlush,
|
||||
}
|
||||
|
||||
struct PokerHand {
|
||||
cards: Vec<PlayingCard>,
|
||||
}
|
||||
|
||||
impl fmt::Display for PokerHand {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let s = match self.hand_type() {
|
||||
HandType::HighCard => write!(f, "{}", self.cards[4]),
|
||||
HandType::OnePair => {
|
||||
for val in self.hash_map() {
|
||||
if val.1 == 2 {
|
||||
return write!(f, "Pair of {:?}'s", val);
|
||||
}
|
||||
}
|
||||
return write!(f, "???");
|
||||
}
|
||||
HandType::TwoPair => todo!(),
|
||||
HandType::ThreeOfAKind => todo!(),
|
||||
HandType::Straight => todo!(),
|
||||
HandType::Flush => todo!(),
|
||||
HandType::FullHouse => todo!(),
|
||||
HandType::FourOfAKind => todo!(),
|
||||
HandType::StraightFlush => todo!(),
|
||||
};
|
||||
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
impl PokerHand {
|
||||
fn new(mut cards: Vec<PlayingCard>) -> Self {
|
||||
assert!(cards.len() == 5);
|
||||
cards.sort_unstable();
|
||||
PokerHand { cards }
|
||||
}
|
||||
|
||||
fn hash_map(&self) -> HashMap<CardValue, u8> {
|
||||
let mut count: HashMap<CardValue, u8> = HashMap::new();
|
||||
|
||||
for card in &self.cards {
|
||||
*count.entry(card.value).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
count
|
||||
}
|
||||
|
||||
fn hand_type(&self) -> HandType {
|
||||
if is_straight_flush(self) {
|
||||
return HandType::StraightFlush;
|
||||
}
|
||||
|
||||
if is_four_of_a_kind(self) {
|
||||
return HandType::FourOfAKind;
|
||||
}
|
||||
|
||||
if is_full_house(self) {
|
||||
return HandType::FullHouse;
|
||||
}
|
||||
|
||||
if is_flush(self) {
|
||||
return HandType::Flush;
|
||||
}
|
||||
|
||||
if is_straight(self) {
|
||||
return HandType::Straight;
|
||||
}
|
||||
|
||||
if is_three_of_a_kind(self) {
|
||||
return HandType::ThreeOfAKind;
|
||||
}
|
||||
|
||||
if is_two_pair(self) {
|
||||
return HandType::TwoPair;
|
||||
}
|
||||
|
||||
if is_one_pair(self) {
|
||||
return HandType::OnePair;
|
||||
}
|
||||
|
||||
return HandType::HighCard;
|
||||
}
|
||||
}
|
||||
|
||||
fn is_straight_flush(hand: &PokerHand) -> bool {
|
||||
let hand = &hand.cards;
|
||||
assert!(hand.len() == 5);
|
||||
|
||||
let suit = hand[0].suit;
|
||||
let mut min_val = CardValue::Two;
|
||||
let mut max_val = CardValue::Ace;
|
||||
|
||||
// Must all be the same suit
|
||||
for i in 0..5 {
|
||||
let card = &hand[i];
|
||||
|
||||
for k in (i + 1)..5 {
|
||||
if hand[k].value == card.value {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if card.value < min_val {
|
||||
min_val = card.value;
|
||||
};
|
||||
|
||||
if card.value > max_val {
|
||||
max_val = card.value;
|
||||
}
|
||||
|
||||
if card.suit != suit {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
let max_val = max_val as u8;
|
||||
let min_val = min_val as u8;
|
||||
|
||||
if max_val - min_val > 4 {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn is_four_of_a_kind(hand: &PokerHand) -> bool {
|
||||
for e in hand.hash_map() {
|
||||
if e.1 == 4 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn is_full_house(hand: &PokerHand) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn is_flush(hand: &PokerHand) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn is_straight(hand: &PokerHand) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn is_three_of_a_kind(hand: &PokerHand) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn is_two_pair(hand: &PokerHand) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn is_one_pair(hand: &PokerHand) -> bool {
|
||||
false
|
||||
}
|
||||
Reference in New Issue
Block a user