示例#1
0
        private static IEnumerable <PokerDuel> ReadHandsFromFile()
        {
            const string filePath = "Data/Problem54Data.txt";

            string[] rawText = File.ReadAllLines(filePath);

            // Convert each line into a poker hand.
            // Expecting 10 cards per line, separated by spaces in the format 'VS' where V is the value, S is the suit.
            foreach (string line in rawText)
            {
                var cardsInLine = new List <Card>();

                for (int i = 0; i < line.Length; i += 3)
                {
                    Card.FaceValue value = GetValue(line[i]);
                    Card.CardSuit  suit  = GetSuit(line[i + 1]);
                    cardsInLine.Add(new Card(value, suit));
                }

                var handOne = new PokerHand(cardsInLine[0], cardsInLine[1], cardsInLine[2], cardsInLine[3], cardsInLine[4]);
                var handTwo = new PokerHand(cardsInLine[5], cardsInLine[6], cardsInLine[7], cardsInLine[8], cardsInLine[9]);

                yield return(new PokerDuel(handOne, handTwo));
            }
        }
示例#2
0
        public bool WillBeatHand(PokerHand opposingHand)
        {
            // Royal Flush
            if (HasRoyalFlush())
            {
                return(!opposingHand.HasRoyalFlush());
            }
            if (opposingHand.HasRoyalFlush())
            {
                return(false);
            }

            // Straight flush
            if (HasStraightFlush())
            {
                if (!opposingHand.HasStraightFlush())
                {
                    return(true);
                }

                return(Cards[4] > opposingHand.Cards[4]);
            }
            if (opposingHand.HasStraightFlush())
            {
                return(false);
            }

            // Four of a Kind
            if (HasFourOfAKind())
            {
                if (!opposingHand.HasFourOfAKind())
                {
                    return(true);
                }

                // The middle card will be part of the 4 - the hand is sorted.
                return(Cards[2] > opposingHand.Cards[2]);
            }
            if (opposingHand.HasFourOfAKind())
            {
                return(false);
            }

            // Full House
            if (HasFullHouse())
            {
                if (!opposingHand.HasFullHouse())
                {
                    return(true);
                }

                // The middle card will be part of the 3 - the hand is sorted.
                return(Cards[2] > opposingHand.Cards[2]);
            }
            if (opposingHand.HasFullHouse())
            {
                return(false);
            }

            // Flush
            if (HasFlush())
            {
                if (!opposingHand.HasFlush())
                {
                    return(true);
                }

                return(Cards[4] > opposingHand.Cards[4]);
            }
            if (opposingHand.HasFlush())
            {
                return(false);
            }

            // Straight
            if (HasStraight())
            {
                if (!opposingHand.HasStraight())
                {
                    return(true);
                }

                return(Cards[4] > opposingHand.Cards[4]);
            }
            if (opposingHand.HasStraight())
            {
                return(false);
            }

            // Three of a Kind
            if (HasThreeOfAKind())
            {
                if (!opposingHand.HasThreeOfAKind())
                {
                    return(true);
                }

                // The middle card will be part of the 3 - the hand is sorted.
                return(Cards[2] > opposingHand.Cards[2]);
            }
            if (opposingHand.HasThreeOfAKind())
            {
                return(false);
            }

            // Two Pairs
            if (PairCount() == 2)
            {
                if (opposingHand.PairCount() < 2)
                {
                    return(true);
                }

                // If the final card is part of the higher pair, compare it to the high pair in the other hand.
                if (Cards[4].Value == Cards[3].Value)
                {
                    if (opposingHand.Cards[4].Value == opposingHand.Cards[3].Value)
                    {
                        return(Cards[4] > opposingHand.Cards[4]);
                    }

                    return(Cards[4] > opposingHand.Cards[3]);
                }

                if (opposingHand.Cards[4].Value == opposingHand.Cards[3].Value)
                {
                    return(Cards[3] > opposingHand.Cards[4]);
                }

                return(Cards[3] > opposingHand.Cards[3]);

                //TODO: account for KKQQ7 vs KKJJ9
            }
            if (opposingHand.PairCount() == 2)
            {
                return(false);
            }

            // Pair
            if (PairCount() == 1)
            {
                if (opposingHand.PairCount() == 0)
                {
                    return(true);
                }

                Card.FaceValue pairCard    = GetHighestPairValue();
                Card.FaceValue oppPairCard = opposingHand.GetHighestPairValue();

                // If the final card is part of the higher pair, compare it to the high pair in the other hand.
                if (pairCard > oppPairCard)
                {
                    return(true);
                }
                if (pairCard < oppPairCard)
                {
                    return(false);
                }

                Card.FaceValue highestSingle    = GetHighestSingleValue();
                Card.FaceValue oppHighestSingle = opposingHand.GetHighestSingleValue();

                if (highestSingle > oppHighestSingle)
                {
                    return(true);
                }

                if (highestSingle < oppHighestSingle)
                {
                    return(false);
                }

                // TODO: check lower 2 cards, if the highest is identical
            }
            if (opposingHand.PairCount() == 1)
            {
                return(false);
            }

            // Highest Card
            return(GetHighestSingleValue() > opposingHand.GetHighestSingleValue());
        }