Exemplo n.º 1
0
        /// <summary>
        /// Gets the winning hand.
        /// </summary>
        /// <param name="hands">The poker hands.</param>
        /// <returns>List of winning hands. Is a list since ties are possible. Chopped pots.</returns>
        /// <exception cref="ArgumentNullException">hands</exception>
        /// <exception cref="ArgumentException">hands</exception>
        public List <PokerHandDto> GetWinningHand(List <PokerHandDto> hands)
        {
            //null check
            if (hands == null)
            {
                throw new ArgumentNullException(nameof(hands));
            }
            //empty check
            if (!hands.Any())
            {
                throw new ArgumentException(nameof(hands));
            }

            //first winner to first item in list
            List <PokerHandDto> winners = new List <PokerHandDto> {
                hands[0]
            };
            var winnerWinPriority = _handTypes.GetHandTypeByTypeName(winners[0].Type).WinPriority;

            for (int i = 1; i < hands.Count(); i++)
            {
                var currentWinPriority = _handTypes.GetHandTypeByTypeName(hands[i].Type).WinPriority;
                if (currentWinPriority < winnerWinPriority)
                {
                    winnerWinPriority = currentWinPriority;
                    winners           = new List <PokerHandDto> {
                        hands[i]
                    };
                }
                else if (currentWinPriority == winnerWinPriority)
                {
                    winners = ResolveWinPriorityTie(winners, hands[i], currentWinPriority);
                }
            }
            return(winners);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the type of the hand.
        /// </summary>
        /// <param name="hand">The hand.</param>
        /// <returns>HandType.</returns>
        /// <exception cref="ArgumentNullException">Provided poker hand is null.</exception>
        public HandType GetHandType(PokerHand hand)
        {
            //null check
            if (hand == null)
            {
                throw new ArgumentNullException(nameof(hand));
            }

            List <Card> cardsInHand = GetListOfCardsFromHand(hand);

            //check if hand is Straight
            bool isHandStraight = IsHandStraight(cardsInHand);
            //check if hand is flush
            bool isHandFlush = IsHandFlush(cardsInHand);

            //check is hand straight flush
            if (isHandStraight && isHandFlush)
            {
                return(_handTypes.GetHandTypeByTypeName("Straight Flush"));
            }

            //return if flush
            if (isHandFlush)
            {
                return(_handTypes.GetHandTypeByTypeName("Flush"));
            }

            //return if straight
            if (isHandStraight)
            {
                return(_handTypes.GetHandTypeByTypeName("Straight"));
            }

            //Get list of card frequencies
            List <CardFrequency> repeatedHandRankList = new CardFrequencyList().GetCardFrequencyList(cardsInHand);

            //check for quads
            var quads = repeatedHandRankList.Where(cr => cr.Frequency == 4).FirstOrDefault();

            if (quads != null)
            {
                return(_handTypes.GetHandTypeByTypeName("Four of a Kind"));
            }

            //check for full house
            var trips = repeatedHandRankList.Where(cr => cr.Frequency == 3).FirstOrDefault();

            if (trips != null)
            {
                //check for full house
                var pairNeededForBoat = repeatedHandRankList.Where(cr => cr.Frequency == 2).FirstOrDefault();
                if (pairNeededForBoat != null)
                {
                    return(_handTypes.GetHandTypeByTypeName("Full House"));
                }
            }

            //check for trips
            if (trips != null)
            {
                return(_handTypes.GetHandTypeByTypeName("Three of a Kind"));
            }

            //check for two pair
            var pairs = repeatedHandRankList.Where(cr => cr.Frequency == 2);

            if (pairs.Any())
            {
                if (pairs.Count() == 2)
                {
                    return(_handTypes.GetHandTypeByTypeName("Two Pair"));
                }
                //must be pair
                return(_handTypes.GetHandTypeByTypeName("Pair"));
            }

            //With all other options exausted hand must be a high card
            return(_handTypes.GetHandTypeByTypeName("High Card"));
        }