New features
+ Add check for straight flush + Add check for four of a kind + Add launch.json
This commit is contained in:
45
.vscode/launch.json
vendored
Normal file
45
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug executable 'cards'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"build",
|
||||
"--bin=cards",
|
||||
"--package=cards"
|
||||
],
|
||||
"filter": {
|
||||
"name": "cards",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug unit tests in executable 'cards'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--bin=cards",
|
||||
"--package=cards"
|
||||
],
|
||||
"filter": {
|
||||
"name": "cards",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
||||
83
src/main.rs
83
src/main.rs
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user