public override PokerHand CheckHand(List <PlayingCard> cards, int handSize) { if (!EnoughCards(cards, handSize)) { return(null); } cardsInStraight.Clear(); GetCardRanks(cards); // Go through ranks and see if there are 5 in a row. foreach (var value in PlayingCard.Values.Reverse()) { if (RankCards.ContainsKey(value)) { // Add a card of this rank to the building straight. cardsInStraight.Add(RankCards[value][0]); if (cardsInStraight.Count == handSize) { // Found a long enough straight! return(new PokerHand(cardsInStraight, PokerHandRank.Straight)); } } else { cardsInStraight.Clear(); } } // If we are one short and end at 2, check for an ace. if (cardsInStraight.Count == handSize - 1) { if (cardsInStraight.Last().Value == PlayingCard.CardValue.Two) { if (RankCards.ContainsKey(PlayingCard.CardValue.Ace)) { cardsInStraight.Add(RankCards[PlayingCard.CardValue.Ace][0]); return(new PokerHand(cardsInStraight, PokerHandRank.Straight)); } } } // Did not find HandSize cards in a row. return(null); }
public bool?IsBetterRank(IRank other) { if (other.RankValue < RankValue) { return(true); } if (other.RankValue > RankValue) { return(false); } // Rank Values are the same, then test the card var thisCardValue = RankCards.FirstOrDefault(c => c.Key == Card[0]).Value; var otherCardValue = RankCards.FirstOrDefault(c => c.Key == other.Card[0]).Value; if (otherCardValue == thisCardValue) { return(null); // Tie } return(otherCardValue < thisCardValue); }
public override PokerHand CheckHand(List <PlayingCard> cards, int handSize) { if (!EnoughCards(cards, handSize)) { return(null); } GetCardRanks(cards); // Find the largest and second largest set value. foreach (var pair in RankCards) { if (pair.Value.Count > HighSet.Count) { SecondHighSet = HighSet; HighSet = pair.Value; } else if (pair.Value.Count > SecondHighSet.Count) { SecondHighSet = pair.Value; } } // If first or second set isn't large enough to qualify, we're done. if (HighSet == null || HighSet.Count < SetSizeOne) { return(null); } if (SetSizeTwo > 0 && (SecondHighSet == null || SecondHighSet.Count < SetSizeTwo)) { return(null); } // Add high sets to cards in hand. var cardsInHand = new List <PlayingCard>(); // If sets are same size, higher set is one of higher value. if (HighSet.Count == SecondHighSet.Count) { if (HighSet[0].Value < SecondHighSet[1].Value) { cardsInHand.AddRange(SecondHighSet); cardsInHand.AddRange(HighSet); } else { cardsInHand.AddRange(HighSet); cardsInHand.AddRange(SecondHighSet); } } // No need for RankCounts or sets anymore. RankCards.Clear(); HighSet.Clear(); SecondHighSet.Clear(); // If we have HandSize or more cards, return hand as is. // Otherwise, we need to take non-set high cards until our hand is correct size. if (cardsInHand.Count > handSize) { return(new PokerHand(cardsInHand.GetRange(0, 5), Rank)); } if (cardsInHand.Count == handSize) { return(new PokerHand(cardsInHand, Rank)); } foreach (var card in cards) { if (cardsInHand.Contains(card)) { continue; } cardsInHand.Add(card); if (cardsInHand.Count == handSize) { return(new PokerHand(cardsInHand, Rank)); } } // Should never get here. throw new InvalidOperationException("Could not build poker hand from cards provided."); }