add more code for 54 (WIP)

This commit is contained in:
Dr. Matthias Ratajczak
2022-08-17 21:02:59 +02:00
parent 66d884e298
commit 3d3170117c
+68 -38
View File
@@ -1,12 +1,26 @@
use std::collections::{HashMap, HashSet}; #![allow(dead_code, unused_imports)]
#[allow(dead_code)] use std::{
cmp::max,
collections::{HashMap, HashSet},
};
type Cards = [Card; 5];
type Values = [Value; 5];
const ROYAL_FLUSH: [Value; 5] = [
Value::Ten,
Value::Jack,
Value::Queen,
Value::King,
Value::Ace,
];
fn main() { fn main() {
println!("Hello, world!"); println!("Hello, world!");
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone)]
struct Card { struct Card {
value: Value, value: Value,
suit: Suit, suit: Suit,
@@ -22,7 +36,7 @@ impl Card {
} }
} }
#[derive(Debug, PartialEq, Hash, Eq, Clone)] #[derive(Debug, PartialEq, Hash, Eq, Clone, PartialOrd, Ord)]
enum Suit { enum Suit {
Diamonds, Diamonds,
Hearts, Hearts,
@@ -42,7 +56,7 @@ impl Suit {
} }
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone)]
enum Value { enum Value {
Two, Two,
Three, Three,
@@ -90,6 +104,7 @@ struct Game {
player2: Hand, player2: Hand,
} }
#[derive(PartialEq, PartialOrd)]
enum Evaluation { enum Evaluation {
High, High,
OnePair, OnePair,
@@ -99,7 +114,7 @@ enum Evaluation {
Flush, Flush,
FullHouse, FullHouse,
FourOfAKind, FourOfAKind,
StraightFlush, StraightFlush(Card),
RoyalFlush, RoyalFlush,
} }
@@ -129,13 +144,13 @@ impl Hand {
} }
trait Contains<T> { trait Contains<T> {
fn contains_value(&self, x: T) -> bool; fn contains_value(&self, x: &T) -> bool;
} }
impl Contains<Value> for &[Card] { impl Contains<Value> for &Cards {
fn contains_value(&self, x: Value) -> bool { fn contains_value(&self, x: &Value) -> bool {
for card in self.iter() { for card in self.iter() {
if card.value == x { if card.value == *x {
return true; return true;
} }
} }
@@ -143,39 +158,54 @@ impl Contains<Value> for &[Card] {
} }
} }
trait CardTrait {
fn have_matching_suits(&self) -> bool;
fn contain_all_values_of(&self, values: &Values) -> bool;
fn get_highest_card(&self) -> Card;
fn have_consecutive_values(&self) -> bool;
}
impl CardTrait for Cards {
fn have_matching_suits(&self) -> bool {
self[1..]
.iter()
.zip(self[..4].iter())
.all(|(c1, c2)| c1.suit == c2.suit)
}
fn contain_all_values_of(&self, values: &Values) -> bool {
values.iter().all(|v| self.contains_value(v))
}
fn get_highest_card(&self) -> Card {
self.iter()
.reduce(|accum, item| max(accum, item))
.unwrap()
.clone()
}
fn have_consecutive_values(&self) -> bool {
self[0].value + 1
}
}
impl Evaluation { impl Evaluation {
fn from_cards(cards: &[Card]) -> Self { fn from_cards(cards: &Cards) -> Self {
todo!() if Self::is_royal_flush(cards) {
} Self::RoyalFlush
} else if Self::is_straight_flush(cards) {
fn is_royal_flush(cards: &[Card]) -> Option<Suit> { Self::StraightFlush(cards.get_highest_card())
let values = [
Value::Ten,
Value::Jack,
Value::Queen,
Value::King,
Value::Ace,
];
for value in values {
if !cards.contains_value(value) {
return None;
}
}
if Self::have_matching_suits(cards) {
Some(cards[0].suit.clone())
} else { } else {
None todo!()
} }
} }
fn have_matching_suits(cards: &[Card]) -> bool { fn is_straight_flush(cards: &Cards) -> bool {
let first_suit = &cards[0].suit; cards.have_matching_suits() && cards.have_consecutive_values()
for card in cards { }
if &card.suit != first_suit {
return false; fn is_royal_flush(cards: &Cards) -> bool {
} cards.have_matching_suits() && cards.contain_all_values_of(&ROYAL_FLUSH)
}
true
} }
} }