add more code for 54 (WIP)

This commit is contained in:
Dr. Matthias Ratajczak
2022-08-17 20:21:01 +02:00
parent 9285c726a7
commit 66d884e298
2 changed files with 39 additions and 13 deletions
+1 -1
View File
@@ -9,5 +9,5 @@ fn main() {
ways[i] += ways[i - value];
}
}
println!("{:#?}", ways);
println!("{}", ways[TARGET]);
}
+38 -12
View File
@@ -6,6 +6,7 @@ fn main() {
println!("Hello, world!");
}
#[derive(Debug, PartialEq)]
struct Card {
value: Value,
suit: Suit,
@@ -21,6 +22,7 @@ impl Card {
}
}
#[derive(Debug, PartialEq, Hash, Eq, Clone)]
enum Suit {
Diamonds,
Hearts,
@@ -40,6 +42,7 @@ impl Suit {
}
}
#[derive(Debug, PartialEq)]
enum Value {
Two,
Three,
@@ -119,18 +122,33 @@ impl Hand {
fn from_line(line: &str) -> Self {
assert_eq!(line.len(), 14);
let vec: Vec<_> = line.split_whitespace().map(Card::from_str).collect();
let cards = vec.try_into().unwrap_or_else(|_| panic!());
let cards: [Card; 5] = vec.try_into().unwrap_or_else(|_| panic!());
let evaluation = Evaluation::from_cards(&cards);
Self { cards, evaluation }
}
}
trait Contains<T> {
fn contains_value(&self, x: T) -> bool;
}
impl Contains<Value> for &[Card] {
fn contains_value(&self, x: Value) -> bool {
for card in self.iter() {
if card.value == x {
return true;
}
}
false
}
}
impl Evaluation {
fn from_cards(cards: &[Card; 5]) -> Self {
fn from_cards(cards: &[Card]) -> Self {
todo!()
}
fn is_royal_flush(cards: &[Card; 5]) -> Option<Suit> {
fn is_royal_flush(cards: &[Card]) -> Option<Suit> {
let values = [
Value::Ten,
Value::Jack,
@@ -139,28 +157,36 @@ impl Evaluation {
Value::Ace,
];
for value in values {
if !cards.contains_val(value) {
if !cards.contains_value(value) {
return None;
}
}
let mut suits = HashSet::new();
for card in cards {
suits.insert(card.suit)
if Self::have_matching_suits(cards) {
Some(cards[0].suit.clone())
} else {
None
}
}
if suits.len() == 1 {
Some(suits.iter().next().unwrap())
} else { None }
fn have_matching_suits(cards: &[Card]) -> bool {
let first_suit = &cards[0].suit;
for card in cards {
if &card.suit != first_suit {
return false;
}
}
true
}
}
#[cfg(test)]
mod test {
use crate::Game;
use crate::{Card, Game};
#[test]
fn example1() {
let line = "5H 5C 6S 7S KD 2C 3S 8S 8D TD";
let game = Game::from_line(line);
assert_eq!(game.player1.cards[0], Card::from_str("5H"));
}
}