Beispiel #1
0
        public void CalculateHandValue()
        {
            var tempHand = new List <Card>(PlayerHand);

            handValue = HandValues.Nothing;

            //For testing

            /*Card[] cards = { new Card(11,Card.SuitType.Clubs), new Card(11,Card.SuitType.Clubs), new Card(2,Card.SuitType.Clubs),
             *                          new Card(8,Card.SuitType.Clubs),new Card(7,Card.SuitType.Spades )};
             * tempHand = new List<Card>(cards);
             * PlayerHand = tempHand;
             * PrintHand();*/

            //Sort by card value ascending
            tempHand.Sort((a, b) => a.Value.CompareTo(b.Value));

            //Check for straights
            if (HasStraight(tempHand))
            {
                handValue = HandValues.Straight;
            }

            //Check for Flush
            if (HasFlush())
            {
                //If a straight was found as well, then it is either a royal flush or a straight flush
                if (handValue == HandValues.Straight)
                {
                    //Check if it is a 10 J Q K A straight
                    if (tempHand[0].Value == 10)
                    {
                        handValue = HandValues.RoyalFlush;
                    }
                    else
                    {
                        handValue = HandValues.StraightFlush;
                    }
                }
                else //Otherwise it is just a flush
                {
                    handValue = HandValues.Flush;
                }
            }

            //Check for matching value cards. Cards are already sorted by value
            var pairValue = CheckForPairs(tempHand);

            //Set the hand score to the highest value
            if (handValue < pairValue)
            {
                handValue = pairValue;
            }
        }
Beispiel #2
0
        private HandValues CheckForPairs(List <Card> sortedHand)
        {
            HandValues pairValue  = HandValues.Nothing;
            int        matchValue = 0; // Stores the value of the matched card. Used if it is only a pair to check for Jacks or Better.

            int[] matches = CountPairs(sortedHand, ref matchValue);

            //Checks if the hand value is a pair or better, not counting hands of a single pair worse than jacks.
            bool handFound = matches[0] != 0 && !(matchValue <= 10 && matches[1] == 0 && matches[0] == 1);

            //if a hand is found, check if its a two/three/four of a kind or a full house/two pair hand
            if (handFound)
            {
                //if more than two types of cards were matched it's either a full house or two pair
                if (matches[1] != 0)
                {
                    if (matches[0] + matches[1] == 3)
                    {
                        pairValue = HandValues.FullHouse;
                    }
                    else
                    {
                        pairValue = HandValues.TwoPair;
                    }
                }
                else
                {
                    switch (matches[0])
                    {
                    case 1:
                        pairValue = HandValues.JacksOrBetter;
                        break;

                    case 2:
                        pairValue = HandValues.ThreeOfAKind;
                        break;

                    case 3:
                        pairValue = HandValues.FourOfAKind;
                        break;

                    default:
                        Console.WriteLine("Single card pair check error.");
                        break;
                    }
                }
            }
            return(pairValue);
        }
Beispiel #3
0
 public GameManager()
 {
     deck       = new Deck();
     PlayerHand = new List <Card>();
     handValue  = HandValues.Nothing;
 }