New features

+ Add check for straight flush
+ Add check for four of a kind
+ Add launch.json
This commit is contained in:
2024-03-22 16:34:46 -04:00
parent 5398e2b443
commit b01c5209f5
2 changed files with 123 additions and 5 deletions

View File

@@ -1,8 +1,9 @@
use rand::Rng;
use std::collections::HashMap;
use std::fmt;
use std::vec;
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Eq, PartialEq)]
enum CardSuit {
Diamonds,
Clubs,
@@ -15,6 +16,59 @@ struct PlayingCard {
suit: CardSuit,
}
fn is_straight_flush(hand: &Vec<PlayingCard>) -> bool {
assert!(hand.len() == 5);
let suit = hand[0].suit;
let mut min_val = 15;
let mut max_val = 1;
// 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;
}
}
if max_val - min_val > 4 {
return false;
}
true
}
fn is_four_of_a_kind(hand: &Vec<PlayingCard>) -> bool {
let mut count: HashMap<u8, u8> = HashMap::new();
for card in hand {
*count.entry(card.value).or_insert(0) += 1;
}
for e in count {
if e.1 == 4 {
return true;
}
}
false
}
impl fmt::Display for PlayingCard {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let val = match self.value {
@@ -73,9 +127,28 @@ fn create_deck() -> Vec<PlayingCard> {
fn main() {
let mut deck = create_deck();
let card1 = draw_card(&mut deck);
let card2 = draw_card(&mut deck);
let card1: PlayingCard = PlayingCard {
suit: CardSuit::Clubs,
value: 2,
};
let card2: PlayingCard = PlayingCard {
suit: CardSuit::Clubs,
value: 2,
};
let card3: PlayingCard = PlayingCard {
suit: CardSuit::Clubs,
value: 2,
};
let card4: PlayingCard = PlayingCard {
suit: CardSuit::Clubs,
value: 2,
};
let card5: PlayingCard = PlayingCard {
suit: CardSuit::Clubs,
value: 3,
};
println!("Card 1: {}", card1);
println!("Card 2: {}", card2);
let hand = vec![card1, card2, card3, card4, card5];
println!("{}", is_four_of_a_kind(&hand));
}