Esempio n. 1
0
        public CompleteDeckData(DeckData inputDeck)
        {
            Dictionary <string, int> cards = new Dictionary <string, int>(inputDeck.CardIdToCount);

            foreach (var kv in cards)
            {
                int      cardCount  = kv.Value;
                CardData simpleCard = null;
                if (!App.CastedInstance.CardLibrary.TryGetValue(kv.Key, out simpleCard))
                {
                    continue;
                }

                CompleteCardData completeCard = new CompleteCardData(simpleCard);

                for (int i = 0; i < cardCount; i++)
                {
                    Cards.Enqueue(completeCard);
                }
            }
        }
Esempio n. 2
0
        public List <CompleteCardData> GetFullCompleteCardDataList()
        {
            List <CompleteCardData> resultList = new List <CompleteCardData>();

            foreach (var kv in CardIdToCount)
            {
                int      cardCount  = kv.Value;
                CardData simpleCard = null;
                if (!App.CastedInstance.CardLibrary.TryGetValue(kv.Key, out simpleCard))
                {
                    continue;
                }

                CompleteCardData completeCard = new CompleteCardData(simpleCard);

                for (int i = 0; i < cardCount; i++)
                {
                    resultList.Add(completeCard);
                }
            }

            return(resultList);
        }
        public override ResultBase AnalyzeHandWithCommons(List <CompleteCardData> hand)
        {
            int highCard = -1;

            bool pair1      = false;
            int  pair1Value = -1;
            bool pair2      = false;
            int  pair2Value = -1;
            bool trips      = false;
            int  tripsValue = -1;
            bool quads      = false;
            int  quadsValue = -1;

            bool flush      = false;
            Suit flushSuit  = null;
            int  flushValue = -1;

            bool straight      = false;
            Suit straightSuit  = null;
            int  straightValue = -1;

            bool straightFlush      = false;
            int  straightFlushValue = -1;

            bool royalFlush = false;

            List <CompleteCardData> consolidated = new List <CompleteCardData>(m_CommonPool);

            foreach (var card in hand)
            {
                consolidated.Add(card);
            }

            List <int>             overallStrengthCount   = new List <int>(CommonsStrengthCount); // the number of cards of said strength
            Dictionary <Suit, int> overallSuitToCountDict = new Dictionary <Suit, int>(CommonsSuitToCountDict);

            // analyzing hand for card count for each suit and value.
            foreach (CompleteCardData cardData in hand)
            {
                overallStrengthCount[cardData.StrengthValue - m_MinStrength]++;
                if (overallSuitToCountDict.ContainsKey(cardData.Suit1))
                {
                    overallSuitToCountDict[cardData.Suit1]++;
                }
                else
                {
                    overallSuitToCountDict[cardData.Suit1] = 1;
                }
            }

            // matching patterns
            for (int i = 0; i < overallStrengthCount.Count; i++)
            {
                if (overallStrengthCount[i] > 0)
                {
                    highCard = i + m_MinStrength;
                }
                switch (overallStrengthCount[i])
                {
                case 2:
                    if (pair1)
                    {
                        pair2      = true;
                        pair2Value = i + m_MinStrength;
                    }
                    else
                    {
                        pair1      = true;
                        pair1Value = i + m_MinStrength;
                    }
                    break;

                case 3:
                    trips      = true;
                    tripsValue = i + m_MinStrength;
                    break;

                case 4:
                    quads      = true;
                    quadsValue = i + m_MinStrength;
                    break;
                }
            }

            // detecting flushes
            foreach (var kv in overallSuitToCountDict)
            {
                if (kv.Value >= 5)
                {
                    flush     = true;
                    flushSuit = kv.Key;

                    // Finding highest value of the flush suit
                    consolidated.ForEach(card =>
                    {
                        if (card.Suit1 == flushSuit && card.StrengthValue > flushValue)
                        {
                            flushValue = card.StrengthValue;
                        }
                    });
                    break;
                }
            }

            // detecting straights and straight flushes
            // Here, we check for a straight for every card by assuming that the card is the first in the pattern.
            // todo: Find a better solution for this.
            foreach (var card in consolidated)
            {
                int  startingValue = card.StrengthValue;
                Suit startingSuit  = card.Suit1;

                bool foundStraight = true;

                // start from aces
                if (startingValue == m_MaxStrength)
                {
                    startingValue = m_MinStrength - 1;
                }

                if (startingValue + 4 > m_MaxStrength)
                {
                    // overflow. not possible to have a straight with the given starting value.
                    continue;
                }

                for (int offset = 1; offset < 5; offset++)
                {
                    int desiredValue = startingValue + offset;
                    int index        = desiredValue - m_MinStrength;

                    if (overallStrengthCount[index] == 0)
                    {
                        foundStraight = false;
                        break;
                    }
                }

                // if straight is found AND the startingValue is higher than the current one (a stronger straight)
                if (foundStraight && startingValue > straightValue)
                {
                    straight      = true;
                    straightSuit  = startingSuit;
                    straightValue = startingValue + 4;
                }
            }

            // rare, but there's a chance that there is a straight flush
            // Brute force looking for a straight flush
            // todo: detect a royal flush
            if (straight && flush)
            {
                int suitCardCount = 0;
                if (overallSuitToCountDict.TryGetValue(straightSuit, out suitCardCount))
                {
                    if (suitCardCount >= 5)
                    {
                        // sort in increasing strength value
                        consolidated.Sort((c1, c2) => c1.StrengthValue.CompareTo(c2.StrengthValue));

                        consolidated.RemoveAll(card => card.Suit1 != straightSuit);

                        int startingIndex = 1;
                        int currentVal    = consolidated[0].StrengthValue;

                        // Special case: Aces (or highest card that needs to roll back)
                        // Check for Ace being used as the smallest card first.
                        // highest card being treated as lowest card.
                        if (consolidated.Last().StrengthValue == m_MaxStrength) // last card is highest value
                        {
                            currentVal    = m_MinStrength - 1;
                            startingIndex = 0;
                        }

                        // normal case.
                        // We go from first card to the last, and check that they are in a straight sequence
                        int straightPosition = 0;
                        for (int i = startingIndex; i < consolidated.Count; i++)
                        {
                            CompleteCardData next = consolidated[i];
                            if (next.StrengthValue - currentVal != 1)
                            {
                                currentVal       = next.StrengthValue;
                                straightPosition = 0;
                                continue; // NOT A STRAIGHT, reset.
                            }

                            currentVal = next.StrengthValue;
                            straightPosition++;

                            if (straightPosition >= 4) // straight flush found!
                            {
                                straightFlush      = true;
                                straightFlushValue = currentVal; // the last card.
                            }
                        }
                    }
                }
            }

            if (straightFlush && straightFlushValue == m_MaxStrength)
            {
                royalFlush = true;
            }

            if (royalFlush)
            {
                return(new Result(Pattern.RoyalFlush, 0));
            }
            else if (straightFlush)
            {
                return(new Result(Pattern.StraightFlush, straightValue));
            }
            else if (quads)
            {
                return(new Result(Pattern.FourOfAKind, quadsValue));
            }
            else if (trips && pair1)
            {
                return(new Result(Pattern.FullHouse, tripsValue));
            }
            else if (flush)
            {
                return(new Result(Pattern.Flush, highCard));
            }
            else if (straight)
            {
                return(new Result(Pattern.Straight, straightValue));
            }
            else if (trips)
            {
                return(new Result(Pattern.ThreeOfAKind, tripsValue));
            }
            else if (pair2)
            {
                return(new Result(Pattern.TwoPair, Math.Max(pair1Value, pair2Value)));
            }
            else if (pair1)
            {
                return(new Result(Pattern.OnePair, pair1Value));
            }
            else
            {
                return(new Result(Pattern.None, highCard));
            }
        }
 public void AddCardToHand(CompleteCardData card)
 {
     hand.Add(card);
 }