int[] ThreeOfAkind(Card[] hand) { var ordered = hand.OrderBy(c => c.Value).Select(c => c.Value).ToList(); if (ordered[0] == ordered[2] || ordered[1] == ordered[3] || ordered[2] == ordered[4]) { var result = new[] { 4, ordered[2] }; return result.Concat(GetOrderedCardValues(hand.Where(c => c.Value != ordered[2]))).ToArray(); } return null; }
private int[] TwoPairs(Card[] hand) { var ordered = hand.OrderBy(c => c.Value).Select(c => c.Value).ToList(); if (ordered.GroupBy(c => c).Count(c => c.Count() > 1) == 2) { IEnumerable<int> result = new[] {3}; return result.Concat( ordered.GroupBy(c => c).Where(c => c.Count() > 1).Select(c => c.Key).OrderByDescending(c => c)) .Concat(ordered.GroupBy(c => c).Where(c => c.Count() == 1).Select(c => c.Key)).ToArray(); } return null; }
int[] Straight(Card[] hand) { if (IsStraight(hand)) { var result = new[] { 5 }; if (hand.Select(c => c.Value).Contains(ACE) && hand.Select(c => c.Value).Contains(2)) return result.Concat(new int[] {5}).ToArray(); return result.Concat(new int[] {hand.Select(c=>c.Value).Max()}).ToArray(); } return null; }
int[] StraightFlush(Card[] hand) { if (IsFlush(hand) && IsStraight(hand)) return new[] { 9, hand.Select(c => c.Value).Where(v => v!=ACE).Max() }; return null; }
private int[] OnePair(Card[] hand) { var ordered = hand.OrderBy(c => c.Value).Select(c => c.Value).ToList(); if (ordered.GroupBy(c => c).Count(c => c.Count() > 1) == 1) { var result = new[] { 2 }; return result.Concat( ordered.GroupBy(c => c).Where(c => c.Count() > 1).Select(c => c.Key).OrderByDescending(c => c)). Concat(ordered.GroupBy(c => c).Where(c => c.Count() == 1).OrderByDescending(c=>c.Key).Select(c=>c.Key)).ToArray(); } return null; }
int[] RoyalFlush(Card[] hand) { if (IsFlush(hand) && IsStraight(hand) && hand.Select(c => c.Value).Max() == ACE && hand.Select(c => c.Value).Contains(KING)) return new[] {10}; return null; }
bool IsFlush(Card[] hand) { return hand.Select(c => c.Color).Distinct().Count() == 1; }
bool IsStraight(Card[] hand) { var ordered = hand.OrderBy(c => c.Value).Select(c=>c.Value).ToList(); if (ordered[0] + 1 == ordered[1] && ordered[1] + 1 == ordered[2] && ordered[2] + 1 == ordered[3] && ordered[3] + 1 == ordered[4]) { return true; } if (ordered[0] == 2 && ordered[1] == 3 && ordered[2] == 4 && ordered[3] == 5 && ordered[4] == ACE) { return true; } return false; }
int[] FullHouse(Card[] hand) { var ordered = hand.OrderBy(c => c.Value).Select(c => c.Value).ToList(); if (ordered[0] == ordered[2] && ordered[3] == ordered[4]) return new[] { 7, ordered[0], ordered[3] }; if (ordered[0] == ordered[1] && ordered[2] == ordered[4]) return new[] { 7, ordered[2], ordered[0] }; return null; }
private int[] HighCard(Card[] hand) { return (new int[] {1}).Concat(GetOrderedCardValues(hand)).ToArray(); }
int[] FourOfKind(Card[] hand) { var ordered = hand.OrderBy(c => c.Value).Select(c => c.Value).ToList(); if (ordered[0] == ordered[3] || ordered[1] == ordered[4]) return new[] { 8, ordered[1] }; return null; }
int[] Flush(Card[] hand) { if (IsFlush(hand)) { var result = new[] {6}; return result.Concat(GetOrderedCardValues(hand)).ToArray(); } return null; }
public int CompareHands(string line) { var cards = line.Split(' '); var hand1 = new Card[] {new Card(cards[0]), new Card(cards[1]), new Card(cards[2]), new Card(cards[3]), new Card(cards[4])}; var hand2 = new Card[] { new Card(cards[5]), new Card(cards[6]), new Card(cards[7]), new Card(cards[8]), new Card(cards[9]) }; return Compare(CalculateHandValue(hand1), CalculateHandValue(hand2)); }
public int[] CalculateHandValue(Card[] hand) { return Ranks.Select(r => r(hand)).First(r => r != null); }
public static int Compare(Card a, Card b) { return a.value.CompareTo(b.value); }