Skip to content

Instantly share code, notes, and snippets.

@EdCharbeneau
Forked from mikebranstein/card-counting.cs
Created August 1, 2016 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdCharbeneau/34df27682f70ad713cab986831c513b6 to your computer and use it in GitHub Desktop.
Save EdCharbeneau/34df27682f70ad713cab986831c513b6 to your computer and use it in GitHub Desktop.
public HandRank GetHandRank() =>
IsRoyalFlush() ? HandRank.RoyalFlush :
IsStraightFlush() ? HandRank.StraightFlush :
IsFourOfAKind() ? HandRank.FourOfAKind :
IsFullHouse() ? HandRank.FullHouse :
IsFlush() ? HandRank.Flush :
IsStraight() ? HandRank.Straight :
IsThreeOfAKind() ? HandRank.ThreeOfAKind :
IsTwoPair() ? HandRank.TwoPair :
IsPair() ? HandRank.Pair :
HandRank.HighCard;
private bool IsRoyalFlush() => IsFlush() && Cards.All(x => x.Value >= CardValue.Ten);
private bool IsStraightFlush() => IsStraight() && IsFlush();
private bool IsFourOfAKind() =>
Cards.GroupBy(x => x.Value).Any(x => x.Count() == 4);
private bool IsFullHouse() =>
Cards.GroupBy(x => x.Value).Any(x => x.Count() == 3) &&
Cards.GroupBy(x => x.Value).Any(x => x.Count() == 2);
private bool IsFlush() => Cards.All(x => x.Suit == Cards.First().Suit);
private bool IsStraight() =>
Cards.GroupBy(x => x.Value).Count() == 5 &&
Cards.Max(x => x.Value) - Cards.Min(x => x.Value) == 4;
private bool IsThreeOfAKind() => Cards.GroupBy(x => x.Value).Any(x => x.Count() == 3);
private bool IsTwoPair() => Cards.GroupBy(x => x.Value).Select(grp => new { grp.Key, Count = grp.Count() })
.GroupBy(x => x.Count).Any(x => x.Count() == 2);
private bool IsPair() => Cards.GroupBy(x => x.Value).Any(x => x.Count() == 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment