Esempio n. 1
0
    public static IList <string> Evaluate(IDictionary <string, PokerHand> hands)
    {
        var      len         = Enum.GetValues(typeof(HandType)).Length;
        var      winners     = new List <string>();
        HandType winningType = HandType.HighCard;

        foreach (var name in hands.Keys)
        {
            for (var handType = HandType.RoyalFlush;
                 (int)handType < len; handType = handType + 1)
            {
                var hand = hands[name];
                if (hand.IsValid(handType))
                {
                    int compareHands = 0, compareCards = 0;
                    if (winners.Count == 0 ||
                        (compareHands = winningType.CompareTo(handType)) > 0 ||
                        compareHands == 0 &&
                        (compareCards = hand.CompareTo(hands[winners[0]])) >= 0)
                    {
                        if (compareHands > 0 || compareCards > 0)
                        {
                            winners.Clear();
                        }
                        winners.Add(name);
                        winningType = handType;
                    }
                    break;
                }
            }
        }
        return(winners);
    }
Esempio n. 2
0
        public int CompareTo(object?obj)
        {
            if (!(obj is Hand other))
            {
                throw new ArgumentException("Comparison of hand with non-hand");
            }

            int handComparison = HandType.CompareTo(other.HandType);

            if (handComparison != 0)
            {
                return(handComparison);
            }

            int rankComparison = Rank.CompareTo(other.Rank);

            if (rankComparison != 0)
            {
                return(rankComparison);
            }

            if (_sets == null)
            {
                return(0);
            }

            // if _sets isn't null under these conditions, other._sets must also be non-null
            return(_sets.CompareTo(other._sets !));
        }
Esempio n. 3
0
        public static IList <Player> Evaluate(List <Player> players)
        {
            HasPlayersSameCards(players);
            var      len         = Enum.GetValues(typeof(HandType)).Length;
            var      winners     = new List <Player>();
            HandType winningType = HandType.HighCard;

            foreach (var player in players)
            {
                for (var handType = HandType.RoyalFlush; (int)handType < len; handType = handType + 1)
                {
                    if (player.Hand.IsValid(handType))
                    {
                        int compareHands = 0;
                        int compareCards = 0;
                        if (winners.Count == 0 || (compareHands = winningType.CompareTo(handType)) > 0 || compareHands == 0 && (compareCards = player.Hand.CompareTo(winners[0].Hand)) >= 0)
                        {
                            if (compareHands > 0 || compareCards > 0)
                            {
                                winners.Clear();
                            }
                            winningType     = handType;
                            player.HandType = winningType;
                            winners.Add(player);
                        }
                        break;
                    }
                }
            }
            return(winners);
        }
Esempio n. 4
0
        public static KeyValuePair <string, string> Evaluate(IDictionary <string, PokerHand> hands)
        {
            if (HasDuplicates(hands.Values.ToList()))
            {
                throw new Exception("There are duplicate cards");
            }

            var      len         = Enum.GetValues(typeof(HandType)).Length;
            var      winners     = new List <string>();
            HandType winningType = HandType.HighCard;

            foreach (var name in hands.Keys)
            {
                for (var handType = HandType.RoyalFlush; (int)handType < len; handType = handType + 1)
                {
                    var hand = hands[name];
                    if (hand.IsValid(handType)) // Is hand one of the valid types?
                    {
                        int compareHands = 0, compareCards = 0;
                        if (winners.Count == 0 || // Are there any winning hands? If no winning hands, add one.
                            (compareHands = winningType.CompareTo(handType)) > 0 || // Proceed to next check if current handType does not beat winning handType. If it does, add to winners.
                            compareHands == 0 && // Proceed to next check if hands are equal. If current hand is worse than winning hand, do nothing.
                            (compareCards = hand.CompareTo(hands[winners[0]])) >= 0)  // Check to see if current hand beats winning hand. New Winner?
                        {
                            if (compareHands > 0 || compareCards > 0)
                            {
                                winners.Clear();
                            }
                            winners.Add(name);
                            winningType = handType;
                        }
                        break;
                    }
                }
            }
            KeyValuePair <string, string> winner = new KeyValuePair <string, string>(winners.First(), winningType.ToString());

            return(winner);
        }
Esempio n. 5
0
        public int CompareTo(Hand otherHand)
        {
            HandType handType1 = this.GetHandType();
            HandType handType2 = otherHand.GetHandType();

            if (handType1 == HandType.Undefined || handType2 == HandType.Undefined)
            {
                // TODO: throw new Exception("");
            }
            if (handType1 == handType2)
            {
                Card[] hand1CardsArray = this.Cards.ToArray();
                Card[] hand2CardsArray = otherHand.Cards.ToArray();
                Array.Sort(hand1CardsArray);
                Array.Sort(hand2CardsArray);
                CardValue hand1TopCardRank    = hand1CardsArray[MaxCardsInHand - 1].Value;
                CardValue hand2TopCardRank    = hand2CardsArray[MaxCardsInHand - 1].Value;
                CardValue hand1BottomCardRank = hand1CardsArray[0].Value;
                CardValue hand2BottomCardRank = hand2CardsArray[0].Value;

                switch (handType1)
                {
                case HandType.StraightFlush:
                case HandType.Straight:
                    return(hand1TopCardRank.CompareTo(hand2TopCardRank));

                case HandType.Flush:
                    for (int i = MaxCardsInHand - 1; i >= 0; i--)
                    {
                        CardValue hand1CardRank         = hand1CardsArray[i].Value;
                        CardValue hand2CardRank         = hand2CardsArray[i].Value;
                        int       flushComparisonResult = hand1CardRank.CompareTo(hand2CardRank);
                        if (flushComparisonResult != 0)
                        {
                            return(flushComparisonResult);
                        }
                    }
                    return(0);

                case HandType.FullHouse:
                    // Get the triple ranks
                    CardValue hand1TripleRank;
                    CardValue hand2TripleRank;
                    CardValue hand1PairRank;
                    CardValue hand2PairRank;
                    if (this._handHistogram[(int)hand1TopCardRank] == 3)
                    {
                        hand1TripleRank = hand1TopCardRank;
                        hand1PairRank   = hand1BottomCardRank;
                    }
                    else
                    {
                        hand1TripleRank = hand1BottomCardRank;
                        hand1PairRank   = hand1TopCardRank;
                    }

                    if (otherHand._handHistogram[(int)hand2TopCardRank] == 3)
                    {
                        hand2TripleRank = hand2TopCardRank;
                        hand2PairRank   = hand2BottomCardRank;
                    }
                    else
                    {
                        hand2TripleRank = hand2BottomCardRank;
                        hand2PairRank   = hand2TopCardRank;
                    }
                    // Compare triples. If they are equal, compare pairs
                    int fhComparisonResult = hand1TripleRank.CompareTo(hand2TripleRank);
                    if (fhComparisonResult != 0)
                    {
                        return(fhComparisonResult);
                    }
                    else
                    {
                        fhComparisonResult = hand1PairRank.CompareTo(hand2PairRank);
                        return(fhComparisonResult);
                    }

                case HandType.FourOfAKind:
                    // Compare the two remaining cards and find which one of them is kicker
                    CardValue hand1Card;
                    CardValue hand2Card;
                    if (this._handHistogram[(int)hand1TopCardRank] != 4)
                    {
                        hand1Card = hand1TopCardRank;
                    }
                    else
                    {
                        hand1Card = hand1BottomCardRank;
                    }

                    if (otherHand._handHistogram[(int)hand2TopCardRank] != 4)
                    {
                        hand2Card = hand2TopCardRank;
                    }
                    else
                    {
                        hand2Card = hand2BottomCardRank;
                    }
                    return(hand1Card.CompareTo(hand2Card));

                case HandType.ThreeOfAKind:
                    // Find triples
                    CardValue?       hand1ThreeOfAKindRank = null; // TODO
                    CardValue?       hand2ThreeOfAKindRank = null;
                    List <CardValue> hand1RemainingCards   = new List <CardValue>(2);
                    List <CardValue> hand2RemainingCards   = new List <CardValue>(2);

                    for (int i = MaxCardsInHand - 1; i >= 0; i--)
                    {
                        CardValue hand1CardRank = hand1CardsArray[i].Value;
                        CardValue hand2CardRank = hand2CardsArray[i].Value;
                        if (this._handHistogram[(int)hand1CardRank] == 3)
                        {
                            hand1ThreeOfAKindRank = hand1CardRank;
                        }
                        else
                        {
                            hand1RemainingCards.Add(hand1CardRank);
                        }
                        if (otherHand._handHistogram[(int)hand2CardRank] == 3)
                        {
                            hand2ThreeOfAKindRank = hand2CardRank;
                        }
                        else
                        {
                            hand2RemainingCards.Add(hand2CardRank);
                        }
                    }
                    // FIXME: Workaround
                    int hand1ThreeOfAKindRankIntVal = hand1ThreeOfAKindRank.HasValue ? (int)hand1ThreeOfAKindRank.Value : -1;
                    int hand2ThreeOfAKindRankIntVal = hand2ThreeOfAKindRank.HasValue ? (int)hand2ThreeOfAKindRank.Value : -1;
                    int threeOfAKindComparison      = hand1ThreeOfAKindRankIntVal.CompareTo(hand2ThreeOfAKindRankIntVal);
                    if (threeOfAKindComparison != 0)
                    {
                        return(threeOfAKindComparison);
                    }
                    else
                    {
                        // Compare remaining cards
                        hand1RemainingCards.Sort();
                        hand2RemainingCards.Sort();
                        int remainingComparisonResult = hand1RemainingCards[1].CompareTo(hand2RemainingCards[1]);
                        if (remainingComparisonResult == 0)
                        {
                            remainingComparisonResult = hand1RemainingCards[0].CompareTo(hand2RemainingCards[0]);
                        }
                        return(remainingComparisonResult);
                    }

                case HandType.TwoPair:
                    List <CardValue> hand1PairRanks         = new List <CardValue>(2);
                    List <CardValue> hand2PairRanks         = new List <CardValue>(2);
                    CardValue?       hand1RemainingCardRank = null;
                    CardValue?       hand2RemainingCardRank = null;
                    for (int i = MaxCardsInHand - 1; i >= 0; i--)
                    {
                        CardValue hand1CardRank = hand1CardsArray[i].Value;
                        CardValue hand2CardRank = hand2CardsArray[i].Value;
                        if (this._handHistogram[(int)hand1CardRank] == 2)
                        {
                            hand1PairRanks.Add(hand1CardRank);
                        }
                        else
                        {
                            hand1RemainingCardRank = hand1CardRank;
                        }

                        if (otherHand._handHistogram[(int)hand2CardRank] == 2)
                        {
                            hand2PairRanks.Add(hand2CardRank);
                        }
                        else
                        {
                            hand2RemainingCardRank = hand2CardRank;
                        }
                    }

                    hand1PairRanks.Sort();
                    hand2PairRanks.Sort();
                    int twoPairComparisonResult = hand1PairRanks[1].CompareTo(hand2PairRanks[1]);
                    if (twoPairComparisonResult == 0)
                    {
                        twoPairComparisonResult = hand1PairRanks[0].CompareTo(hand2PairRanks[0]);
                    }

                    // FIXME: Workaround
                    int hand1RemainingCardRankIntVal = hand1RemainingCardRank.HasValue ? (int)hand1RemainingCardRank.Value : -1;
                    int hand2RemainingCardRankIntVal = hand2RemainingCardRank.HasValue ? (int)hand2RemainingCardRank.Value : -1;
                    // if the pairs are equal, determine the kicker
                    if (twoPairComparisonResult == 0)
                    {
                        twoPairComparisonResult = hand1RemainingCardRankIntVal.CompareTo(hand2RemainingCardRankIntVal);
                    }
                    return(twoPairComparisonResult);

                case HandType.OnePair:
                    CardValue?       hand1OnePairRank        = null;
                    CardValue?       hand2OnePairRank        = null;
                    List <CardValue> hand1RemainingCardRanks = new List <CardValue>(3);
                    List <CardValue> hand2RemainingCardRanks = new List <CardValue>(3);
                    for (int i = MaxCardsInHand - 1; i >= 0; i--)
                    {
                        CardValue hand1CardRank = hand1CardsArray[i].Value;
                        CardValue hand2CardRank = hand2CardsArray[i].Value;
                        if (this._handHistogram[(int)hand1CardRank] == 2)
                        {
                            hand1OnePairRank = hand1CardRank;
                        }
                        else
                        {
                            hand1RemainingCardRanks.Add(hand1CardRank);
                        }

                        if (otherHand._handHistogram[(int)hand2CardRank] == 2)
                        {
                            hand2OnePairRank = hand2CardRank;
                        }
                        else
                        {
                            hand2RemainingCardRanks.Add(hand2CardRank);
                        }
                    }
                    // if the pairs are equal, determine the kicker
                    // FIXME: Workaround
                    int onePairComparisonResult = (hand1OnePairRank.HasValue ? (int)hand1OnePairRank.Value : -1)
                                                  .CompareTo(hand2OnePairRank.HasValue ? (int)hand2OnePairRank.Value : -1);
                    if (onePairComparisonResult == 0)
                    {
                        hand1RemainingCardRanks.Sort();
                        hand2RemainingCardRanks.Sort();
                        onePairComparisonResult = hand1RemainingCardRanks[2].CompareTo(hand2RemainingCardRanks[2]);
                        if (onePairComparisonResult == 0)
                        {
                            onePairComparisonResult = hand1RemainingCardRanks[1].CompareTo(hand2RemainingCardRanks[1]);
                        }
                        if (onePairComparisonResult == 0)
                        {
                            onePairComparisonResult = hand1RemainingCardRanks[0].CompareTo(hand2RemainingCardRanks[0]);
                        }
                    }
                    return(onePairComparisonResult);

                case HandType.HighCard:
                    for (int i = MaxCardsInHand - 1; i >= 0; i--)
                    {
                        CardValue hand1CardRank = hand1CardsArray[i].Value;
                        CardValue hand2CardRank = hand2CardsArray[i].Value;
                        if (hand1CardRank == hand2CardRank)
                        {
                            continue;
                        }
                        if (hand1CardRank > hand2CardRank)
                        {
                            return(1);
                        }
                    }
                    break;
                }
            }
            else
            {
                return(handType1.CompareTo(handType2));
            }
            return(0);
        }
Esempio n. 6
0
 public int CompareTo(BoardSpectrumGridUnit other)
 {
     return(HandType.CompareTo(other.HandType));
 }