// Anything where the Ace is > King private static bool HighStraight(Hand hand, Hand bestHand) { if (hand.Count < 5) { return(false); } // Explicitly checking for a high-straight, so look for an ACE and set the value to 14 var checkHand = hand.Copy(); var hasAce = checkHand.Count(c => c.Value == 1) > 0; if (!hasAce) { return(false); } foreach (var card in checkHand) { if (card.Value == 1) { card.Value = 14; } } var ordered = checkHand.OrderByDescending(a => a.Value).ToList(); if (ordered[0].Value == ordered[4].Value + 4) { bestHand.Clear(); bestHand.AddRange(ordered.Take(5)); return(true); } return(false); }
// Anything where the Ace is not > King private static bool LowStraight(Hand hand, Hand bestHand) { if (hand.Count < 5) { return(false); } // order by descending ensures that the highest value is at the beginning // of the list, making it easier to take the highest low straight. var ordered = hand.Copy().OrderByDescending(a => a.Value).ToList(); // remove duplicates from the ordered list. ordered = ordered.Distinct(new CardValueComparer()).ToList(); if (ordered.Count > 5) { for (int i = 0; i < ordered.Count - 4; i++) { if (ordered[i].Value == ordered[i + 4].Value + 4) { bestHand.AddRange(ordered.Skip(i).Take(5)); return(true); } } } else if (ordered.Count == 5 && ordered[0].Value == ordered[4].Value + 4) { bestHand.AddRange(ordered.Take(5)); return(true); } return(false); }
private static bool HighestStraight(Hand hand, Hand bestHand) { if (hand.Count < 5) { return(false); } var ordered = hand.Copy().OrderByDescending(a => a.Value).ToList(); if (IsStraight(ordered)) { return(true); } for (var i = 0; i < ordered.Count - 5; i++) { var skipped = ordered.Skip(i); var possibleStraight = skipped.Take(5); if (IsStraight(possibleStraight)) { bestHand.Clear(); bestHand.AddRange(possibleStraight); return(true); } } return(false); }
private static bool Straight(Hand hand, Hand bestHand) { if (hand.Count < 5) { return(false); } var checkHand = hand.Copy(); var bestLow = new Hand(); var bestHigh = new Hand(); var hasLowStraight = LowStraight(checkHand, bestLow); var hasHighStraight = HighStraight(checkHand, bestHigh); var hasStraight = HighestStraight(checkHand, bestHand); bestHand.Clear(); if (hasHighStraight) { bestHand.AddRange(bestHigh); } else if (hasLowStraight) { bestHand.AddRange(bestLow); } return(hasLowStraight || hasHighStraight); }
private static bool Flush(Hand hand, Hand bestHand) { if (hand.Count < 5) { return(false); } var checkHand = hand.Copy(); var hasAce = checkHand.Count(c => c.Value == 1) > 0; // Create a copy of the Ace but use the high-value if (hasAce) { var aces = checkHand.Where(c => c.IsAce).ToList(); foreach (var ace in aces) { checkHand.Add(new Card(14, ace.Suit)); } } //see if 5 or more cards card the same rank. var ordered = checkHand.OrderByDescending(a => a.Value).ToList(); var suitGroups = ordered.GroupBy(card => card.Suit); var hasFlush = suitGroups.Count(group => group.Count() >= 5) == 1; var flush = suitGroups.Where(group => group.Count() >= 5); if (!flush.Any()) { return(false); } flush = flush.OrderByDescending(group => group.OrderByDescending(c => c.Value)); var bestFlush = flush.First().ToList(); bestHand.Clear(); bestHand.AddRange(bestFlush.Take(5)); return(hasFlush); }
private static bool StraightFlush(Hand hand, Hand bestHand) { if (hand.Count < 5) { return(false); } var checkHand = hand.Copy(); // check if flush and straight are true. var bestFlush = new Hand(); var bestStraight = new Hand(); var hasFlush = false; var hasStraight = false; while (checkHand.Count() > 4 && !(hasFlush && hasStraight)) { hasFlush = Flush(checkHand, bestFlush); // Make sure any ACEs are accounted for on both ends var ace = GetHighCard(bestFlush); if (ace != null && ace.Value == 14) { bestFlush.Add(new Card(1, ace.Suit)); } hasStraight = Straight(bestFlush, bestStraight); checkHand.Remove(GetHighCard(checkHand)); } bestHand.Clear(); bestHand.AddRange(bestFlush); return(hasFlush && hasStraight); }