Beispiel #1
0
        private static MadeHand TryMakeFullHouse(Card[] cards, HandGrouping groups)
        {
            List <Card> high = null;

            if (groups.ThreeOfAKind.Count > 1)
            {
                high = groups.ThreeOfAKind[1].Take(2).ToList();
            }
            else if (groups.ThreeOfAKind.Count == 1)
            {
                if (groups.Pair.Count <= 0)
                {
                    return(null);
                }
                high = groups.Pair[0];
            }
            else
            {
                return(null);
            }

            var played = groups.ThreeOfAKind[0]
                         .Concat(high)
                         .ToArray();

            return(new MadeHand(HandTypes.FullHouse, played, GetFaceAlternates(played, cards, 2)));
        }
Beispiel #2
0
        private static MadeHand TryMakePair(HandGrouping groups)
        {
            if (groups.Pair.Count != 1)
            {
                return(null);
            }
            if (groups.Single.Count < 3)
            {
                return(null);
            }

            var played = groups.Pair[0]
                         .Concat(groups.Single.SelectMany(_ => _).Take(3))
                         .ToArray();

            return(new MadeHand(HandTypes.Pair, played));
        }
Beispiel #3
0
        private static MadeHand TryMakeFourOfAKind(Card[] cards, HandGrouping groups)
        {
            if (groups.FourOfAKind.Count <= 0)
            {
                return(null);
            }

            var high = groups.ThreeOfAKind.SelectMany(_ => _)
                       .Concat(groups.Pair.SelectMany(_ => _))
                       .Concat(groups.Single.SelectMany(_ => _))
                       .SortByFace();

            var played = groups.FourOfAKind[0]
                         .Concat(high)
                         .Take(5)
                         .ToArray();

            return(new MadeHand(HandTypes.FourOfAKind, played, GetFaceAlternates(played, cards, 1)));
        }
Beispiel #4
0
        private static MadeHand TryMakeThreeOfAKind(HandGrouping groups)
        {
            if (groups.ThreeOfAKind.Count < 1)
            {
                return(null);
            }

            var high = (groups.ThreeOfAKind.Count > 1 ? groups.ThreeOfAKind[1] : new List <Card>())
                       .Concat(groups.Pair.SelectMany(_ => _))
                       .Concat(groups.Single.SelectMany(_ => _))
                       .SortByFace()
                       .Take(2);

            var played = groups.ThreeOfAKind[0]
                         .Concat(high)
                         .ToArray();

            return(new MadeHand(HandTypes.ThreeOfAKind, played));
        }
Beispiel #5
0
        private static MadeHand TryMakeTwoPair(Card[] cards, HandGrouping groups)
        {
            if (groups.Pair.Count < 2)
            {
                return(null);
            }

            var high = (groups.Pair.Count > 2 ? groups.Pair[2] : new List <Card>())
                       .Concat(groups.Single.SelectMany(_ => _))
                       .SortByFace()
                       .Take(1);

            var played = groups.Pair[0]
                         .Concat(groups.Pair[1])
                         .Concat(high)
                         .ToArray();

            return(new MadeHand(HandTypes.TwoPair, played, GetFaceAlternates(played, cards, 1)));
        }
Beispiel #6
0
        public static MadeHand MakeHand(Card[] cards)
        {
            Guards.Assert(cards.Length >= 5 && cards.Length <= 7, $"Can't make a hand with {cards.Length} cards");

            // Ensure they are sorted
            cards = cards.SortByFace();

            // Group them all by face
            var groups = new HandGrouping(cards);

            var straightFlush = TryMakeStraightFlush(cards);

            if (straightFlush != null)
            {
                return(straightFlush);
            }

            var fourOfAKind = TryMakeFourOfAKind(cards, groups);

            if (fourOfAKind != null)
            {
                return(fourOfAKind);
            }

            var fullHouse = TryMakeFullHouse(cards, groups);

            if (fullHouse != null)
            {
                return(fullHouse);
            }

            var flush = TryMakeFlush(cards);

            if (flush != null)
            {
                return(flush);
            }

            var straight = TryMakeStraight(cards);

            if (straight != null)
            {
                return(straight);
            }

            var threeOfAKind = TryMakeThreeOfAKind(groups);

            if (threeOfAKind != null)
            {
                return(threeOfAKind);
            }

            var twoPair = TryMakeTwoPair(cards, groups);

            if (twoPair != null)
            {
                return(twoPair);
            }

            var pair = TryMakePair(groups);

            if (pair != null)
            {
                return(pair);
            }

            var highCard = TryMakeHighCard(cards);

            if (highCard != null)
            {
                return(highCard);
            }

            throw new InvalidOperationException("Impossible poker hand");
        }