Ejemplo n.º 1
0
        private void ComputeHand()
        {
            if (Cards.Count < 7)
            {
                Hand = Hand.FoldedHand();
                return;
            }

            Straight[] straights = FindAllStraights();
            Flush[]    flushes   = FindAllFlushes();
            Pair[]     pairs     = FindAllPairs();
            Triple[]   triples   = FindAllThreeOfAKind();
            Quadruple  quadruple = FindFourOfAKind();


            if (flushes.Length > 0)
            {
                foreach (Flush f in flushes)
                {
                    try
                    {
                        // Will try to make a royal flush throw error if it is not a proper one
                        RoyalFlush rf = new RoyalFlush(f.AllCards);
                        Hand = new Hand(new[] { rf });
                        return;
                    }
                    catch (CardCombinationValidationException) { }

                    try
                    {
                        // Will try to make a straight flush throw error if it is not a proper one
                        StraightFlush sf = new StraightFlush(f.AllCards);
                        Hand = new Hand(new[] { sf });
                        return;
                    }
                    catch (CardCombinationValidationException) { }
                }
            }

            List <IValuable> combs = new List <IValuable>();

            if (quadruple != null)
            {
                combs.Add(quadruple);
            }

            else if (triples.Length > 0 && pairs.Length > 0)
            {
                Hand = new Hand(new[] { new FullHouse(triples.Max(), pairs.Max()) });
                return;
            }

            else if (flushes.Length > 0)
            {
                Hand = new Hand(new[] { flushes.Max() });
                return;
            }

            else if (straights.Length > 0)
            {
                Hand = new Hand(new[] { straights.Max() });
                return;
            }

            else if (triples.Length > 0)
            {
                combs.Add(triples.Max());
            }

            else if (pairs.Length > 1)
            {
                Pair[] twoHighestPairs = pairs.OrderByDescending(p => p).Take(2).ToArray();
                combs.Add(new TwoPairs(pairs[0], pairs[1]));
            }

            else if (pairs.Length == 1)
            {
                combs.Add(pairs[0]);
            }


            HashSet <Card> cardsUsed = new HashSet <Card>();

            foreach (ICardCollection comb in combs)
            {
                cardsUsed.UnionWith(comb.AllCards);
            }

            int missingCardAmount = 5 - cardsUsed.Count;

            combs.AddRange(Cards.Except(cardsUsed).OrderByDescending(c => c).Take(missingCardAmount));

            Hand = new Hand(combs);
        }
        private static decimal EvaluateDeck(List <Card> deck, out string playerHand)
        {
            deck.Sort();
            SortBySuit(ref deck);
            playerHand = string.Empty;
            decimal     res = 0;
            Combination var;
            var         keys = new List <Card>();

            if (Combination.IsStraight(deck) && Combination.IsFlush(deck))
            {
                var        = new StraightFlush(deck);
                playerHand = var.PlayerHand;
                res        = var.Score;
            }
            else if (Combination.IsNofAKind(4, deck, out keys))
            {
                var        = new FourOfAKind(deck);
                playerHand = var.PlayerHand;
                res        = var.Score;
            }
            else if (Combination.IsFullHouse(deck, out Card key))
            {
                var        = new FullHouse(deck);
                playerHand = var.PlayerHand;
                res        = var.Score;
            }
            else if (Combination.IsFlush(deck))
            {
                var        = new Flush(deck);
                playerHand = var.PlayerHand;
                res        = var.Score;
            }
            else if (Combination.IsStraight(deck))
            {
                var        = new Straight(deck);
                playerHand = var.PlayerHand;
                res        = var.Score;
            }
            else if (Combination.IsNofAKind(3, deck, out keys))
            {
                var        = new ThreeOfAKind(deck);
                playerHand = var.PlayerHand;
                res        = var.Score;
            }
            else if (Combination.IsTwoPairs(deck, out keys))
            {
                var        = new TwoPairs(deck);
                playerHand = var.PlayerHand;
                res        = var.Score;
            }
            else if (Combination.IsNofAKind(2, deck, out keys))
            {
                var        = new Pair(deck);
                playerHand = var.PlayerHand;
                res        = var.Score;
            }
            else
            {
                deck.Sort();
                deck.Reverse();
                var        = new HighCard(deck);
                playerHand = var.PlayerHand;
                res        = var.Score;
            }
            return(res);
        }