Skip to content

Commit

Permalink
Implementing ThreeOfAKind
Browse files Browse the repository at this point in the history
  • Loading branch information
davassi committed Sep 16, 2023
1 parent fdec970 commit a90c7cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,7 @@ Any sequence of five consecutive cards all of the same suit. For instance, a han
6. Straight
Five cards in a sequence, but not all of the same suit. An example would be a hand containing a 2, 3, 4, 5, and 6, of various suits.
7. Three of a Kind (Trips or Set)
Three cards of the same rank and two unrelated cards. An example would be three Queens and two unrelated cards.
8. Two Pair
Two different pairs of cards and one unrelated card. For example, a hand with two 10s, two 9s, and an unrelated card would be "two pair."
Expand Down
16 changes: 15 additions & 1 deletion src/match_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub enum Rank {
/// Any five cards of the same suit, but not in sequence. For instance, if a player has five heart cards, they have a flush.
Flush,
Straight,
/// 7. Three of a Kind (Trips or Set)
///
/// Three cards of the same rank and two unrelated cards. An example would be three Queens and two unrelated cards.
ThreeOfAKind,
TwoPair,
OnePair,
Expand All @@ -42,7 +45,7 @@ impl MatchHandEvaluator {
/// It evaluates the [Rank] of a [Hand]
///
pub fn match_eval(hand: &mut Hand) -> Rank {
// first let's sort the hand, that's the reason we need a mutable reference
// first let's sort the hand, that's the reason we need here a mutable reference
hand.sort();

println!("Sorted: {}", hand);
Expand Down Expand Up @@ -71,6 +74,11 @@ impl MatchHandEvaluator {
{
Rank::Flush
}
[Card { val : v1, .. }, Card { val: v2, .. }, Card { val: v3, .. }, Card { val: v4, .. }, Card { val: v5, .. }]
if (v1 == v2 && v2 == v3 || v3 == v4 && v4 == v5) =>
{
Rank::ThreeOfAKind
}

hand => Rank::HighCard(hand[0]),
}
Expand Down Expand Up @@ -117,4 +125,10 @@ mod test {
assert_rank!(hand!["Kd", "Kh", "Kc", "8s", "8d"], Rank::FullHouse);
assert_rank!(hand!["2d", "2h", "Qc", "Qs", "Qd"], Rank::FullHouse);
}

#[test]
fn rank_3_of_a_kind() {
assert_rank!(hand!["Kd", "Kh", "Kc", "10s", "8d"], Rank::ThreeOfAKind);
assert_rank!(hand!["2d", "Jh", "Qc", "Qs", "Qd"], Rank::ThreeOfAKind);
}
}

0 comments on commit a90c7cd

Please sign in to comment.