Created
May 27, 2016 15:05
-
-
Save imjacobclark/318f84c21b2a99a5a44f5e1b5d571169 to your computer and use it in GitHub Desktop.
Poker Hands TDD in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
struct Hand { | |
cards: Vec<&'static str> | |
} | |
enum HandType { | |
HIGH_CARD, | |
PAIR, | |
TWO_PAIRS, | |
THREE_OF_A_KIND, | |
STRAIGHT, | |
FLUSH, | |
FULL_HOUSE, | |
FOUR_OF_A_KIND, | |
STRAIGHT_FLUSH | |
} | |
impl Hand { | |
fn new(cardsVec: Vec<&'static str>) -> Hand { | |
Hand { | |
cards: cardsVec | |
} | |
} | |
} | |
fn is_flush(hand: Hand) -> bool { | |
let mut card_types = HashSet::new(); | |
for i in 0..hand.cards.len() { | |
let mut hand = String::from(hand.cards[i]); | |
let mut card_type_option = hand.pop(); | |
let mut card_type = match card_type_option.as_mut() { | |
Some(v) => v.clone(), | |
None => panic!("None"), | |
}; | |
card_types.insert(card_type); | |
} | |
card_types.len() == 1 | |
} | |
fn is_straight(hand: Hand) -> bool { | |
let mut card_values: [i32; 5] = [0; 5]; | |
for i in 0..hand.cards.len() { | |
let mut hand = String::from(hand.cards[i]); | |
card_values[i] = get_card_score(hand.chars().nth(0).unwrap().to_string()); | |
} | |
// for i in 0..card_values.len() { | |
// if card_values[i].mt(& card_values[i] + 1) { | |
// println!("{} {}", card_values[i], card_values[i]+1); | |
// continue | |
// }else{ | |
// return false | |
// } | |
// } | |
true | |
} | |
fn get_card_score(card_value: String) -> i32 { | |
if(card_value.to_string() == "A"){ | |
return 14 | |
} | |
if(card_value.to_string() == "K"){ | |
return 13 | |
} | |
if(card_value.to_string() == "Q"){ | |
return 12 | |
} | |
if(card_value.to_string() == "J"){ | |
return 11 | |
} | |
let input_number: Option<i32> = card_value.trim().parse().ok(); | |
let number = match input_number{ | |
Some(num) => num, | |
None => { | |
panic!("Wrong input data! Input a number."); | |
} | |
}; | |
number | |
} | |
fn main(){ | |
let black = Hand::new(vec!("2H", "3D", "5S", "9C", "KD")); | |
let eval = is_straight(black); | |
} | |
#[test] | |
fn is_hand_flush() { | |
let black = Hand::new(vec!("2H", "2H", "2H", "2H", "2H")); | |
let flush = is_flush(black); | |
assert_eq!(flush, true); | |
} | |
#[test] | |
fn is_hand_not_flush() { | |
let black = Hand::new(vec!("2D", "2H", "2H", "2H", "2H")); | |
let flush = is_flush(black); | |
assert_eq!(flush, false); | |
} | |
#[test] | |
fn is_hand_straight() { | |
let black = Hand::new(vec!("2H", "3H", "4H", "5H", "6H")); | |
let flush = is_flush(black); | |
assert_eq!(flush, true); | |
} | |
#[test] | |
fn is_hand_not_straight() { | |
let black = Hand::new(vec!("2H", "7H", "4H", "5H", "6H")); | |
let flush = is_flush(black); | |
assert_eq!(flush, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment