public List <Card> CardsPlayed(HandRanks handRank) { List <Card> cardsSentToBattle = new List <Card>(); CardsInSet cardSet; switch (handRank) { case HandRanks.HighCard: cardSet = handsToPlay.Find(x => x.handRank == HandRanks.HighCard); cardsSentToBattle.AddRange(cardSet.cardsInSet); break; case HandRanks.Pair: cardSet = handsToPlay.Find(x => x.handRank == HandRanks.Pair); cardsSentToBattle.AddRange(cardSet.cardsInSet); break; case HandRanks.TwoPair: //cardsToDestory = bestPair; // ?? hmmm two? might need hign and los pairs instea of best break; case HandRanks.ThreeOfAKind: cardSet = handsToPlay.Find(x => x.handRank == HandRanks.ThreeOfAKind); cardsSentToBattle.AddRange(cardSet.cardsInSet); break; case HandRanks.Straight: cardSet = handsToPlay.Find(x => x.handRank == HandRanks.Straight); cardsSentToBattle.AddRange(cardSet.cardsInSet); break; case HandRanks.Flush: cardSet = handsToPlay.Find(x => x.handRank == HandRanks.Flush); cardsSentToBattle.AddRange(cardSet.cardsInSet); break; case HandRanks.FullHouse: cardSet = handsToPlay.Find(x => x.handRank == HandRanks.Pair); cardsSentToBattle.AddRange(cardSet.cardsInSet); cardSet = handsToPlay.Find(x => x.handRank == HandRanks.ThreeOfAKind); cardsSentToBattle.AddRange(cardSet.cardsInSet); break; case HandRanks.FourOfAKind: cardSet = handsToPlay.Find(x => x.handRank == HandRanks.FourOfAKind); cardsSentToBattle.AddRange(cardSet.cardsInSet); break; case HandRanks.StraightFlush: break; default: throw new ArgumentOutOfRangeException(nameof(handRank), handRank, null); } return(cardsSentToBattle); }
private void EvaluateHand() { if (IsFiveOfAKind()) { rank = HandRanks.FiveOfAKind; ValueRanks = new List <int> { Cards[0].Value }; } else if (IsFlush() && IsStraight()) { rank = HandRanks.StraightFlush; ValueRanks = new List <int> { Cards.Max(c => c.Value) }; } else if (IsFourOfAKind()) { rank = HandRanks.FourOfAKind; } else if (IsFullHouse()) { rank = HandRanks.FullHouse; } else if (IsFlush()) { rank = HandRanks.Flush; ValueRanks = Cards.OrderByDescending(c => c.Value).Select(c => c.Value).ToList(); } else if (IsStraight()) { rank = HandRanks.Straight; ValueRanks = new List <int> { Cards.Max(c => c.Value) }; } else if (IsThreeOfAKind()) { rank = HandRanks.ThreeOfAKind; } else if (IsTwoPair()) { rank = HandRanks.TwoPair; } else if (IsPair()) { rank = HandRanks.Pair; } else { rank = HandRanks.HighCard; ValueRanks = Cards.OrderByDescending(c => c.Value).Select(c => c.Value).ToList(); } }
public void CreateInstanceHelper(IPokerHand pokerHand, HandRanks expectedHandRank, IEnumerable <Card> cards, bool isValid) { if (isValid) { Assert.NotNull(pokerHand); Assert.AreEqual(expectedHandRank, pokerHand.HandRank); CollectionAssert.AreEquivalent(cards, pokerHand.Cards); } else { Assert.IsNull(pokerHand); } }
public void BuildHandEval() { highCardValue = 0; handsToPlay.Clear(); ValueListSetup(); HighCard(); Pairs(); ValueRun(); SuitRun(); handRank = DefineHandRank(); }
public PokerHand(string[] currentPokerHand) { // The expectation is that the class is being // initalized with an array of five cards // There really should be more error handling, but this // is just supposed to be a quick exercise pokerHand = currentPokerHand; pokerHandSuit = Suits.Unknown; pokerHandRank = HandRanks.Unknown; pokerHandPrimaryCard = Cards.Unknown; pokerHandSecondaryCard = Cards.Unknown; // Now that the poker hand is set we are going // to set first the list of cards and the suit SetPokerHandCardsAndSuit(); // Now we set the rank and card values for primary and secondary cards SetPokerHandRankAndValues(); }
private void SetPokerHandRankAndValues() { // Since we have a sorted list, knowing the // endpoints should help determine the rank Cards lowCard = pokerHandCards.FirstOrDefault(); Cards highCard = pokerHandCards.LastOrDefault(); // The cardSpan calculation is probably not the best approach // but since we know the enum is in order, it can be // assured that the values are good for now int cardSpan = (int)highCard - (int)lowCard; // distinctCards will help with determining a number of things int distinctCards = pokerHandCards.Distinct().Count(); // If there is only one suit (ie, the suits are not mixed) // then the worst case scenario is a Flush // so the check will start with the suit // Also note, it can't be anything else since there can't be // duplicates in the same suit, so it's not any of the other hands if (pokerHandSuit != Suits.Mixed) { // If there are no duplicate cards then the hand is // either a Flush, Straight Flush, or Royal Flush // If the difference between the highest and lowest card is five then // it must be a straight // Technically don't need to check for 5 distinct cards // because it's a flush, but it's needed below so I updated // both locations if (cardSpan == 5 && distinctCards == 5) { // If the high card is an Ace then it's a Royal Flush // otherwise it's just a Stright Flush if (highCard == Cards.Ace) { pokerHandRank = HandRanks.RoyalFlush; } else { pokerHandRank = HandRanks.StraightFlush; } } else { // If it's not a straight, then it's just a flush // It can't be a Four of a Kind or Full House // which would beat it otherwise pokerHandRank = HandRanks.Flush; } // We'll set the primary card to the high card // and leave the secondary card alone pokerHandPrimaryCard = highCard; // No need to process further return; } // If the number of distinct cards is two, // then the hand is either four of a kind or a full house if (distinctCards == 2) { // We'll just kind of "brute force" this for now // If the second, third, and fourth cards in our sorted lists are all equal, then // it is Four of a Kind, otherwise it's a Full House if (pokerHandCards[1] == pokerHandCards[2] && pokerHandCards[2] == pokerHandCards[3]) { pokerHandRank = HandRanks.FourOfaKind; // If the first two cards are equal then that is the Four of a Kind // and the primary card is the low card, otherwise the primary card // is the high card because the list is sorted if (pokerHandCards[0] == pokerHandCards[1]) { pokerHandPrimaryCard = lowCard; pokerHandSecondaryCard = highCard; } else { pokerHandPrimaryCard = highCard; pokerHandSecondaryCard = lowCard; } } else { pokerHandRank = HandRanks.FullHouse; // If the second and third cards are equal then that is the Three of a Kind // in the Full House, otherwise it's the last three cards if (pokerHandCards[1] == pokerHandCards[2]) { pokerHandPrimaryCard = pokerHandCards[1]; pokerHandSecondaryCard = highCard; } else { pokerHandPrimaryCard = pokerHandCards[3]; pokerHandSecondaryCard = lowCard; } } // No need to process further return; } // If the card span is 5 and there are 5 unique cards then it's a straight if (cardSpan == 5 && distinctCards == 5) { pokerHandRank = HandRanks.Straight; pokerHandPrimaryCard = highCard; // No need to process further return; } // If there are three distinct cards it is either two pair or three of a kind if (distinctCards == 3) { // Similar to how Four of a Kind vs Full House was determined if (pokerHandCards[0] == pokerHandCards[1] && pokerHandCards[2] == pokerHandCards[3]) { pokerHandRank = HandRanks.TwoPairs; pokerHandPrimaryCard = pokerHandCards[2]; pokerHandSecondaryCard = pokerHandCards[0]; } else if (pokerHandCards[0] == pokerHandCards[1] && pokerHandCards[3] == pokerHandCards[4]) { pokerHandRank = HandRanks.TwoPairs; pokerHandPrimaryCard = pokerHandCards[3]; pokerHandSecondaryCard = pokerHandCards[0]; } else if (pokerHandCards[1] == pokerHandCards[2] && pokerHandCards[3] == pokerHandCards[4]) { pokerHandRank = HandRanks.TwoPairs; pokerHandPrimaryCard = pokerHandCards[3]; pokerHandSecondaryCard = pokerHandCards[1]; } else { // It's Three of a Kind pokerHandRank = HandRanks.ThreeOfaKind; // The middle card is always part of the Three of a Kind pokerHandPrimaryCard = pokerHandCards[2]; } // No need to process further return; } // If there are four distinct cards it's One Pair if (distinctCards == 4) { pokerHandRank = HandRanks.OnePair; // Need to determine what the pair is now if (pokerHandCards[0] == pokerHandCards[1]) { pokerHandPrimaryCard = pokerHandCards[0]; } else if (pokerHandCards[1] == pokerHandCards[2]) { pokerHandPrimaryCard = pokerHandCards[1]; } else if (pokerHandCards[2] == pokerHandCards[3]) { pokerHandPrimaryCard = pokerHandCards[2]; } else { pokerHandPrimaryCard = pokerHandCards[3]; } // No need to process further return; } // The only option left is that it's a nothing hand, which is called High Card here pokerHandRank = HandRanks.HighCard; pokerHandPrimaryCard = highCard; }