Esempio n. 1
0
        //HandEvaluator constructor passing the hand to be evaluated
        public HandEvaluator(Card[] sortedHand)
        {
            heartsSum  = 0;
            diamondSum = 0;
            clubSum    = 0;
            spadesSum  = 0;

            //determine the high cards for the hand of each player, important for if the hands match
            if ((int)sortedHand[0].myValue > (int)sortedHand[1].myValue)
            {
                HighCard       = (int)sortedHand[0].myValue;
                SecondHighCard = (int)sortedHand[1].myValue;
            }
            else if ((int)sortedHand[1].myValue > (int)sortedHand[0].myValue)
            {
                HighCard       = (int)sortedHand[1].myValue;
                SecondHighCard = (int)sortedHand[0].myValue;
            }

            /* copy the sorted hand to an array called cards
             * (nicer naming convention than "sortedHand", I am aware arrays are passed by ref)*/
            cards     = new Card[sortedHand.Length];
            Cards     = sortedHand;
            handValue = new HandValue();
        }
        internal HandValue GetHandValue()
        {
            var rankGroups = Cards.GroupBy(card => card.Rank);

            if (rankGroups.Count() == 5)
            {
                // todo: check exotics
                return(new HandValue
                {
                    HandType = HandType.HighCard,
                    HighCard = Cards.Select(card => GetCardValue(card.Rank)).Max()
                });
            }

            if (rankGroups.Count() == 4)
            {
                var pair = rankGroups.Single(group => group.Count() == 2);
                return(new HandValue
                {
                    HandType = HandType.SinglePair,
                    HighCard = GetCardValue(pair.Key)
                });
            }

            // todo, todo, todo, todo, todo, toodoooo

            // 1. check if all cards are different
            // hint use grouping
            return(HandValue.GetEmpty());
        }
 public HandEvaluator(Card[] sortedHand)
 {
     heartsSum  = 0;
     diamondSum = 0;
     clubSum    = 0;
     spadeSum   = 0;
     cards      = new Card[5];
     Cards      = sortedHand;
     handValue  = new HandValue();
 }
Esempio n. 4
0
 public HandEvaluator(Card[] sortedHand)
 {
     heartsSum  = 0;
     diamondSum = 0;
     clubSum    = 0;
     spadesSum  = 0;
     cards      = new Card[7];
     Cards      = sortedHand;
     handValue  = new HandValue();
     if ((int)cards[0].myValue > (int)cards[1].myValue)
     {
         handValue.HighCard       = (int)cards[0].myValue;
         handValue.SecondHighCard = (int)cards[1].myValue;
     }
     else if ((int)cards[1].myValue > (int)cards[0].myValue)
     {
         handValue.HighCard       = (int)cards[1].myValue;
         handValue.SecondHighCard = (int)cards[0].myValue;
     }
 }
Esempio n. 5
0
        internal HandValue GetHandValue()
        {
            var rankGroups  = Cards.GroupBy(card => card.Rank);
            var suiteGroups = Cards.GroupBy(card => card.Suit);

            // Flush, RoyalFlush, StraightFlush.
            if (rankGroups.Count() == 5 && suiteGroups.Count() == 1)
            {
                var max         = Cards.Select(card => GetCardValue(card.Rank)).Max();
                var min         = Cards.Select(card => GetCardValue(card.Rank)).Min();
                var getCards    = Cards.OrderBy(a => a.Rank).Select(card => GetCardValue(card.Rank)).ToList();
                var streetCards = $"{getCards[0]}{getCards[1]}{getCards[2]}{getCards[3]}{getCards[4]}";

                var royalCards         = $"{min}{getCards[2]}{getCards[3]}{getCards[4]}{max}";
                var convertToIntStreet = Convert.ToInt32(streetCards);
                var convertToIntRoyal  = Convert.ToInt32(royalCards);

                if (max == 14 && min == 10)
                {
                    if ((max - min) == 4)
                    {
                        return(new HandValue
                        {
                            HandType = HandType.RoyalFlush,
                            HighCard = convertToIntRoyal
                        });
                    }
                }
                if ((max - min) == 4)
                {
                    return(new HandValue
                    {
                        HandType = HandType.StraightFlush,
                        HighCard = convertToIntStreet
                    });
                }

                return(new HandValue
                {
                    HandType = HandType.Flush,
                    HighCard = Cards.Select(card => GetCardValue(card.Rank)).Max()
                });
            }

            // Straight, HighCard .
            if (rankGroups.Count() == 5)
            {
                var max          = Cards.Select(card => GetCardValue(card.Rank)).Max();
                var min          = Cards.Select(card => GetCardValue(card.Rank)).Min();
                var getCards     = Cards.OrderBy(a => a.Rank).Select(card => GetCardValue(card.Rank)).ToList();
                var streetCards  = $"{getCards[0]}{getCards[1]}{getCards[2]}{getCards[3]}{getCards[4]}";
                var convertToInt = Convert.ToInt32(streetCards);

                if ((max - min) == 4)
                {
                    return(new HandValue
                    {
                        HandType = HandType.Straight,
                        HighCard = convertToInt
                    });
                }

                return(new HandValue
                {
                    HandType = HandType.HighCard,
                    HighCard = max
                });
            }


            // Single Pair
            if (rankGroups.Count() == 4)
            {
                var pair = rankGroups.Single(group => group.Count() == 2);
                return(new HandValue
                {
                    HandType = HandType.SinglePair,
                    HighCard = GetCardValue(pair.Key)
                });
            }


            //  FullHouse, FourOfAKind
            if (rankGroups.Count() == 2)
            {
                var threeOfKind = rankGroups.Where(group => group.Count() == 3);

                var twoPair = rankGroups.Where(group => group.Count() == 2);
                if (threeOfKind.Count() == 1 && twoPair.Count() == 1)
                {
                    int getFullHouseArray = 0;
                    foreach (var item in threeOfKind)
                    {
                        getFullHouseArray = GetCardValue(item.Key);
                    }
                    return(new HandValue
                    {
                        HandType = HandType.FullHouse,
                        HighCard = getFullHouseArray
                    });
                }
                else
                {
                    var poker         = rankGroups.Where(group => group.Count() == 4);
                    int getKeyInArray = 0;
                    foreach (var item in poker)
                    {
                        getKeyInArray = GetCardValue(item.Key);
                    }
                    return(new HandValue
                    {
                        HandType = HandType.FourOfAKind,
                        HighCard = getKeyInArray
                    });
                }
            }

            // ThreeOfAKind, TwoPair.
            if (rankGroups.Count() == 3)
            {
                var threeOfKind = rankGroups.Where(group => group.Count() == 3);

                var twoPair = rankGroups.Where(group => group.Count() == 2).ToList();
                if (twoPair.Count() == 2)
                {
                    var getKeyInArrayForTwo = GetCardValue(twoPair[0].Key);

                    var getKeyInArrayForSecondPair = GetCardValue(twoPair[1].Key);

                    var twoPairs     = $"{getKeyInArrayForTwo}{getKeyInArrayForSecondPair}";
                    var ConvertToInt = Convert.ToInt32(twoPairs);



                    return(new HandValue
                    {
                        HandType = HandType.TwoPairs,
                        HighCard = ConvertToInt
                    });
                }

                int getKeyInArray = 0;
                foreach (var item in threeOfKind)
                {
                    getKeyInArray = GetCardValue(item.Key);
                }
                return(new HandValue
                {
                    HandType = HandType.ThreeOfAKind,
                    HighCard = getKeyInArray
                });
            }

            return(HandValue.GetEmpty());
        }