public bool IsFourOfAKind(IHand hand)
        {
            if (!IsValidHand(hand))
            {
                return false;
            }

            int equalCardFaceCount = 0;

            for (int i = 0; i < hand.Cards.Count - 1; i++)
            {
                for (int j = i + 1; j < hand.Cards.Count; j++)
                {
                    if (hand.Cards[i].Face == hand.Cards[j].Face)
                    {
                        equalCardFaceCount++;
                        if (equalCardFaceCount == FourOfAKindCount)
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
Beispiel #2
0
        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <returns>
        /// A signed integer that indicates the relative values of <paramref name="x"/> and <paramref name="y"/>, as shown in the following table.Value Meaning Less than zero<paramref name="x"/> is less than <paramref name="y"/>.Zero<paramref name="x"/> equals <paramref name="y"/>.Greater than zero<paramref name="x"/> is greater than <paramref name="y"/>.
        /// </returns>
        /// <param name="x">The first object to compare.</param><param name="y">The second object to compare.</param>
        public int Compare(IHand x, IHand y)
        {
            Argument.IsNotNull(() => x);
            Argument.IsNotNull(() => y);
            if (x.Count != y.Count)
                throw new InvalidOperationException("Cannot compare hands with a different number of cards");

            if (x.Type != Hands.Straight &&
                y.Type != Hands.Straight)
                throw new InvalidOperationException("Cannot compare hands if neither hand is a Straight.");

            if (x.Type != Hands.Straight &&
                y.Type == Hands.Straight)
                return -1 * Constants.SortDirection;

            if (x.Type == Hands.Straight &&
                y.Type != Hands.Straight)
                return 1 * Constants.SortDirection;

            if (x.HighCard < y.HighCard)
                return -1 * Constants.SortDirection;
            if (x.HighCard > y.HighCard)
                return 1 * Constants.SortDirection;

            return 0;
        }
 private void ValidExceptionHelper(IHand hand)
 {
     if (!this.IsValidHand(hand))
     {
         throw new ArgumentException("Hand is not valid!");
     }
 }
        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <returns>
        /// A signed integer that indicates the relative values of <paramref name="x"/> and <paramref name="y"/>, as shown in the following table.Value Meaning Less than zero<paramref name="x"/> is less than <paramref name="y"/>.Zero<paramref name="x"/> equals <paramref name="y"/>.Greater than zero<paramref name="x"/> is greater than <paramref name="y"/>.
        /// </returns>
        /// <param name="x">The first object to compare.</param><param name="y">The second object to compare.</param>
        public int Compare(IHand x, IHand y)
        {
            Argument.IsNotNull(() => x);
            Argument.IsNotNull(() => y);
            if (x.Count != y.Count)
                throw new InvalidOperationException("Cannot compare hands with a different number of cards");

            if (x.Type != Hands.ThreeOfAKind &&
                y.Type != Hands.ThreeOfAKind)
                throw new InvalidOperationException("Cannot compare hands if neither hand has Three-of-a-kind.");

            if (x.Type != Hands.ThreeOfAKind &&
                y.Type == Hands.ThreeOfAKind)
                return -1 * Constants.SortDirection;

            if (x.Type == Hands.ThreeOfAKind &&
                y.Type != Hands.ThreeOfAKind)
                return 1 * Constants.SortDirection;

            if (x.HighCard < y.HighCard)
                return -1 * Constants.SortDirection;
            if (x.HighCard > y.HighCard)
                return 1 * Constants.SortDirection;

            // compare kicker cards.
            return _comparer.Compare(x, y);
        }
Beispiel #5
0
        public static int CompareHighCards(IHand firstHand, IHand secondHand)
        {
            List<CardFace> facesFirstHand = GetSortedFaces(firstHand);
            List<CardFace> facesSecondHand = GetSortedFaces(secondHand);

            return CompareFaceListsOfEqualLength(facesFirstHand, facesSecondHand);
        }
Beispiel #6
0
        /// <summary>
        /// Compares the specified hand to determine if it is of the same type as this comparer.
        /// </summary>
        /// <param name="hand">The hand to be compared.</param>
        /// <returns>
        /// a tuple specifying information about the comparison.
        /// <para>The first value returns the type of this comparer if the specified hand matches, otherwise it returns <see cref="Hands.HighCard"/>.</para>
        /// <para>The second value indicates the high card of the hand if there was a match, otherwise it returns <see cref="Rank.Two"/>.</para>
        /// </returns>
        public Tuple<Hands, Rank> Compare(IHand hand)
        {
            Argument.IsNotNull(() => hand);
            if (hand.Count < 5)
                return Tuple.Create<Hands, Rank>(0, 0);

            // create a map of the rank and count for each card in the hand.
            var map = new Dictionary<Rank, int>(hand.Count);
            for (int i = 0; i < hand.Count; i++)
            {
                if (map.ContainsKey(hand[i].Rank))
                    map[hand[i].Rank]++;
                else
                    map[hand[i].Rank] = 1;
            }

            // If the map has more than three entries, then we have more than 3 kinds of cards in the hand, can't be two pair
            if (map.Count > 3)
                return Tuple.Create<Hands, Rank>(0, 0);

            Rank high = Rank.Two;
            foreach (var kvp in map)
            {
                // Grab the highest pair.
                if (kvp.Value == 2 && high < kvp.Key)
                    high = kvp.Key;
            }

            return Tuple.Create(Hands.TwoPair, high);
        }
Beispiel #7
0
        public static int CompareOnePair(IHand firstHand, IHand secondHand)
        {
            List<CardFace> firstHandFaces = GetSortedFaces(firstHand);
            List<CardFace> secondHandFaces = GetSortedFaces(secondHand);

            CardFace firstHandPairFace = GetPairFace(firstHandFaces);
            CardFace secondHandPairFace = GetPairFace(secondHandFaces);

            if (firstHandPairFace > secondHandPairFace)
            {
                return -1;
            }
            else if (firstHandPairFace < secondHandPairFace)
            {
                return 1;
            }
            else
            {
                firstHandFaces.RemoveAll(face => face == firstHandPairFace);
                secondHandFaces.RemoveAll(face => face == secondHandPairFace);

                // Reverse so that the strongest card is in front
                firstHandFaces.Sort();
                firstHandFaces.Reverse();

                secondHandFaces.Sort();
                secondHandFaces.Reverse();

                return CompareFaceListsOfEqualLength(firstHandFaces, secondHandFaces);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Compares the specified hand to determine if it is of the same type as this comparer.
        /// </summary>
        /// <param name="hand">The hand to be compared.</param>
        /// <returns>
        /// a tuple specifying information about the comparison.
        /// <para>The first value returns the type of this comparer if the specified hand matches, otherwise it returns <see cref="Hands.HighCard"/>.</para>
        /// <para>The second value indicates the high card of the hand if there was a match, otherwise it returns <see cref="Rank.Two"/>.</para>
        /// </returns>
        public Tuple<Hands, Rank> Compare(IHand hand)
        {
            Argument.IsNotNull(() => hand);
            if (hand.Count < 5)
                return Tuple.Create<Hands, Rank>(0, 0);

            // create a map of the rank and count for each card in the hand.
            var map = new Dictionary<Rank, int>(hand.Count);
            for (int i = 0; i < hand.Count; i++)
            {
                if (map.ContainsKey(hand[i].Rank))
                    map[hand[i].Rank]++;
                else
                    map[hand[i].Rank] = 1;
            }

            // If count does not have two elements, then we have more than 2 kinds of cards in the hand, can't be full house.
            if (map.Count != 2)
                return Tuple.Create<Hands, Rank>(0, 0);

            // Get the entry with 3
            foreach (var kvp in map)
            {
                if (kvp.Value == 3)
                    return Tuple.Create(Hands.FullHouse, kvp.Key);
            }

            // Should never get here, but compiler.
            return Tuple.Create<Hands, Rank>(0, 0);
        }
Beispiel #9
0
        public static bool IsStraight(IHand hand)
        {
            List<int> ranks = new List<int>();
            for (int i = 0; i < hand.Cards.Count; i++)
            {
                ranks.Add(hand.Cards[i].Rank);
            }
            ranks.Sort();

            if (ranks[0] != 1)
            {
                for (int i = 1; i < ranks.Count; i++)
                    if (ranks[i] != (ranks[i - 1] + 1)) return false;
            }
            else
            {
                if (ranks[1] != 10)
                {
                    for (int i = 1; i < ranks.Count; i++)
                        if (ranks[i] != (ranks[i - 1] + 1)) return false;
                }
                else
                {
                    ranks.RemoveAt(0);
                    ranks.Add(14);
                    for (int i = 1; i < ranks.Count; i++)
                        if (ranks[i] != (ranks[i - 1] + 1)) return false;
                }
            }
            return true;
        }
        public bool IsFullHouse(IHand hand)
        {
            bool haveThreeOfAKind = this.IsRightTimesOfRepeat(hand, 3);
            bool haveOtherOnePair = false;

            int counter = 0;

            for (int i = 0; i < hand.Cards.Count; i++)
            {
                for (int j = 0; j < hand.Cards.Count; j++)
                {
                    if (hand.Cards[i].Face == hand.Cards[j].Face)
                    {
                        counter++;
                    }
                }

                if (counter == 2)
                {
                    haveOtherOnePair = true;
                    break;
                }

                counter = 0;
            }

            return haveThreeOfAKind && haveOtherOnePair;
        }
        /// <summary>
        /// Checks if <paramref name="hand"/> is a valid poker hand.
        /// </summary>
        /// <param name="hand">The hand to check.</param>
        /// <returns>True if the hand is valid, otherwise - false.</returns>
        public bool IsValid(IHand hand)
        {
            if (hand == null)
            {
                return false;
            }

            ICard[] cards = hand.Cards;

            if (cards.Length != HandSize)
            {
                return false;
            }

            for (int i = 0; i < cards.Length - 1; i++)
            {
                for (int j = i + 1; j < cards.Length; j++)
                {
                    if (cards[i].Equals(cards[j]))
                    {
                        return false;
                    }
                }
            }

            return true;
        }
        public bool IsFourOfAKind(IHand hand)
        {
            bool isHandValid = IsValidHand(hand);
            byte count = 1;
            for (int i = 0; i < hand.Cards.Count; i++)
            {
                for (int j = i+1; j < hand.Cards.Count; j++)
                {
                    if (hand.Cards[i].Face == hand.Cards[j].Face)
                    {
                        count++;
                    }
                }
                if (count == 4)
                {
                    break;
                }
                count = 0;
            }

            bool isFourCardsFromTheSameKind = false;
            if (isHandValid == true && count == 4)
            {
                isFourCardsFromTheSameKind = true;
            }

            return isFourCardsFromTheSameKind;
        }
        public bool IsHand(IHand hand)
        {
            var threeOfAKind = false;
            var pair = false;

            for (var i = Value.Two; i <= Value.Ace; i++)
            {
                IEnumerable<Card> cardsOfSameValue = hand.GetCards().Where(obj => obj.GetCardValue() == i);

                if (cardsOfSameValue.Count() > 2 && !threeOfAKind)
                {
                    threeOfAKind = true;

                }
                else if (cardsOfSameValue.Count() > 1 && !pair)
                {
                    pair = true;

                }
                if (threeOfAKind && pair)
                {
                    hand.SetRank(Rank.FullHouse);
                    return true;
                }
            }
            return false;
        }
Beispiel #14
0
 public bool IsFourOfAKind(IHand hand)
 {
     int counter = 1;
     if (IsValidHand(hand))
     {
         for (int i = 0; i < 2; i++)
         {
             for (int j = i+1; j < 5; j++)
             {
                 if (hand.Cards[i].Face == hand.Cards[j].Face)
                 {
                     counter++;
                 }
                 if (counter == 4)
                 {
                     return true;
                 }
             }
         }
         return false;
     }
     else
     {
         throw new ArgumentException("Hand is invalid", "hand");
     }
 }
        public bool IsFlush(IHand hand)
        {
            if (!IsValidHand(hand))
            {
                return false;
            }

            if (hand.Cards.Count != 5)
            {
                return false;
            }

            for (int i = 0; i < 5; i++)
            {
                if (hand.ToString().IndexOf(hand.Cards[i].ToString()) != hand.ToString().LastIndexOf(hand.Cards[i].ToString()))
                {
                    return false;
                }
            }
            CardSuit cardsSuit = hand.Cards[0].Suit;

            for (int j = 0; j < 5; j++)
            {
                if (hand.Cards[j].Suit != cardsSuit)
                {
                    return false;
                }
            }
            return true;
        }
Beispiel #16
0
        public bool IsHighCard(IHand hand)
        {
            bool isValidHand = this.IsValidHand(hand);
            bool isHighCard = !this.IsFlush(hand) && !this.IsStraight(hand) && !this.IsStraightFlush(hand) && !this.IsFullHouse(hand) && !this.IsOnePair(hand) && !this.IsTwoPair(hand) && !this.IsThreeOfAKind(hand) && !this.IsFourOfAKind(hand);

            return isValidHand && isHighCard;
        }
Beispiel #17
0
        public bool IsFlush(IHand hand)
        {
            var handCards = hand.Cards;

            if (!IsValidHand(hand))
            {
                return false;
            }

            for (var i = 0; i < handCards.Count; i += 5)
            {
                var firstCard = handCards[i];
                var secondCard = handCards[i + 1];
                var thirdCard = handCards[i + 2];
                var fourthCard = handCards[i + 3];
                var fifthCard = handCards[i + 4];
                if (firstCard.Suit == secondCard.Suit &&
                    secondCard.Suit == thirdCard.Suit &&
                    thirdCard.Suit == fourthCard.Suit &&
                    fourthCard.Suit == fifthCard.Suit)
                {
                    return true;
                }
            }

            return false;
        }
        public bool IsValidHand(IHand hand)
        {
            bool hasDuplicate = false;
            if (hand.Cards.Count < handCount)
            {
                return false;
            }
            for (int i = 0; i < hand.Cards.Count; i++)
            {
                if (!hasDuplicate)
                {
                    ICard cardToCheck = hand.Cards[i];
                    for (int j = i + 1; j < hand.Cards.Count; j++)
                    {
                        if (cardToCheck.ToString() == hand.Cards[j].ToString())
                        {
                            hasDuplicate = true;
                            break;
                        }
                    } 
                }
                else
                {
                    break;
                }
            }

            return !hasDuplicate;
        }
        public bool IsFourOfAKind(IHand hand)
        {
            if (!this.IsValidHand(hand))
            {
                return false;
            }

            int sameFaceCount = 1;

            for (int i = 0; i < hand.Cards.Count - 3; i++)
            {
                for (int j = i + 1; j < hand.Cards.Count; j++)
                {
                    if (hand.Cards[i].Face == hand.Cards[j].Face)
                    {
                        sameFaceCount++;
                    }
                }

                if (sameFaceCount == 4)
                {
                    return true;
                }

                sameFaceCount = 1;
            }

            return true;
        }
Beispiel #20
0
        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <returns>
        /// A signed integer that indicates the relative values of <paramref name="x"/> and <paramref name="y"/>, as shown in the following table.Value Meaning Less than zero<paramref name="x"/> is less than <paramref name="y"/>.Zero<paramref name="x"/> equals <paramref name="y"/>.Greater than zero<paramref name="x"/> is greater than <paramref name="y"/>.
        /// </returns>
        /// <param name="x">The first object to compare.</param><param name="y">The second object to compare.</param>
        public int Compare(IHand x, IHand y)
        {
            Argument.IsNotNull(() => x);
            Argument.IsNotNull(() => y);
            if (x.Count != y.Count)
                throw new InvalidOperationException("Cannot compare hands with a different number of cards");

            if (x.Type != Hands.FullHouse &&
                y.Type != Hands.FullHouse)
                throw new InvalidOperationException("Cannot compare hands if neither hand is a FullHouse");

            if (x.Type != Hands.FullHouse &&
                y.Type == Hands.FullHouse)
                return -1 * Constants.SortDirection;

            if (x.Type == Hands.FullHouse &&
                y.Type != Hands.FullHouse)
                return 1 * Constants.SortDirection;

            // HighCard is 3 of a kind
            if (x.HighCard < y.HighCard)
                return -1 * Constants.SortDirection;
            if (x.HighCard > y.HighCard)
                return 1 * Constants.SortDirection;

            // High cards equal, just compare the rest of the cards.
            return _comparer.Compare(x, y);
        }
Beispiel #21
0
        public bool IsStraightFlush(IHand hand)
        {
            throw new NotImplementedException();
            //var faces = new List<int>(5);
            //if (!this.IsFlush(hand))
            //{
            //    return false;
            //}
            //else
            //{
            //    foreach (var card in hand.Cards)
            //    {
            //        faces.Add((int)card.Suit);
            //    }
            //}

            //faces.Sort();
            //for (int i = 0; i < faces.Count - 1; i++)
            //{
            //    if (faces[i] + 1 != faces[i+1])
            //    {
            //        return false;
            //    }
            //}

            //return true;
        }
        public bool IsFourOfAKind(IHand hand)
        {
            if (!this.IsValidHand(hand))
            {
                return false;
            }

            for (int i = 0; i < ValidCardCount; i++)
            {
                int count = 0;
                for (int j = 0; j < ValidCardCount; j++)
                {
                    if (hand.Cards[i].Face == hand.Cards[j].Face)
                    {
                        count++;
                    }

                    if (count == 4)
                    {
                        return true;
                    }
                }
            }

            return false;
        }
        public bool IsFlush(IHand hand)
        {
            if (hand.Cards.GroupBy(c => c.Suit).Count() > 1)
            {
                return false;
            }

            var tempCards = hand.Cards.OrderBy(c => (int)c.Face).ToList();
            for (int i = 0; i < tempCards.Count; i++)
            {
                if (i + 1 < tempCards.Count)
                {
                    if (((int)tempCards[i].Face + 1) == (int)tempCards[i + 1].Face)
                    {
                        if (i + 1 == 4)
                        {
                            return false;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return true;
        }
        public bool IsFourOfAKind(IHand hand)
        {
            var handCards = hand.Cards;
            if (handCards.Count != 5)
            {
                return false;
            }
            for (var i = 0; i < handCards.Count; i += 5)
            {
                var firstCard = handCards[i];
                var secondCard = handCards[i + 1];
                var thirdCard = handCards[i + 2];
                var fourthCard = handCards[i + 3];
                if (firstCard.Suit != secondCard.Suit &&
                    secondCard.Suit != thirdCard.Suit &&
                    thirdCard.Suit != fourthCard.Suit &&
                    secondCard.Face == firstCard.Face &&
                    thirdCard.Face == secondCard.Face &&
                    fourthCard.Face == thirdCard.Face)
                {
                    return true;
                }
            }

            return false;
        }
        public bool IsFourOfAKind(IHand hand)
        {
            if (!IsValidHand(hand))
            {
                return false;
            }

            int faceCounter = 1;
            int maxCounter = 0;
            String[] splitedHand = hand.ToString().Split(' ');

            for (int i = 0, len = splitedHand.Length; i < len; i++)
            {
                for (int j = i + 1; j < len; j++)
                {
                    if (splitedHand[i][0] == splitedHand[j][0])
                    {
                        faceCounter++;
                    }
                }

                if (faceCounter > maxCounter)
                {
                    maxCounter = faceCounter;
                    faceCounter = 1;
                }
            }

            if (maxCounter == 4)
                return true;
            else
                return false;
        }
        public bool IsValidHand(IHand hand)
        {
            var handMaxLenght = 5;
            var cards = hand.Cards;
            if (cards.Count != handMaxLenght)
            {
                return false;
            }

            for (int i = 0; i < cards.Count; i++)
            {
                var checkerCard = cards[i];
                for (int j = 0; j < cards.Count; j++)
                {
                    if (i != j)
                    {
                        var currentCard = cards[j];
                        if (checkerCard.Face == currentCard.Face && checkerCard.Suit == currentCard.Suit)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        internal static int CompareTwoFlushHands(IHand firstHand, IHand secondHand)
        {
            if (checker.IsFlush(firstHand) && !checker.IsFlush(secondHand))
            {
                return 1;
            }
            else if (!checker.IsFlush(firstHand) && checker.IsFlush(secondHand))
            {
                return -1;
            }

            var firstHandSortedCards = firstHand.Cards;
            var secondHandSortedCards = secondHand.Cards;

            PokerHandsChecker.SortCardsByFace(firstHandSortedCards);
            PokerHandsChecker.SortCardsByFace(secondHandSortedCards);

            for (int i = firstHandSortedCards.Count - 1; i >= 0; i--)
            {
                if ((int)firstHandSortedCards[i].Face > (int)secondHandSortedCards[i].Face)
                {
                    return 1;
                }
                else if ((int)firstHandSortedCards[i].Face < (int)secondHandSortedCards[i].Face)
                {
                    return -1;
                }
            }

            return 0;
        }
Beispiel #28
0
        public bool IsFourOfAKind(IHand hand)
        {
            for (int firstCard = 0; firstCard < hand.Cards.Count; firstCard++)
            {
                for (int secondCard = 0; secondCard < hand.Cards.Count; secondCard++)
                {
                    for (int thirdCard = 0; thirdCard < hand.Cards.Count; thirdCard++)
                    {
                        for (int fourthCard = 0; fourthCard < hand.Cards.Count; fourthCard++)
                        {
                            if (firstCard != secondCard && secondCard != thirdCard && thirdCard != fourthCard)
                            {
                                if (hand.Cards[firstCard].Face == hand.Cards[secondCard].Face && 
                                    hand.Cards[secondCard].Face == hand.Cards[thirdCard].Face &&
                                    hand.Cards[thirdCard].Face == hand.Cards[fourthCard].Face)
                                {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }

            return false;
        }
        public bool IsValidHand(IHand hand)
        {
            try
            {
                if (hand.Cards.Count != 5)
                {
                    return false;
                }
            }
            catch (NullReferenceException e)
            {
                return false;
            }

            for (int i = 0; i < hand.Cards.Count; i++)
            {
                for (int j = 0; j < hand.Cards.Count; j++)
                {
                    if (i != j && hand.Cards[i].Face.Equals(hand.Cards[j].Face) && hand.Cards[i].Suit.Equals(hand.Cards[j].Suit))
                    {
                        return false;
                    }
                }
            }

            return true;
        }
        public bool IsFourOfAKind(IHand hand)
        {
            PokerHandsChecker checker = new PokerHandsChecker();
            if (!checker.IsValidHand(hand))
            {
                return false;
            }

            int counter = 0;
            for (int i = 0; i < hand.Cards.Count; i++)
            {
                for (int j = 0; j < hand.Cards.Count; j++)
                {
                    if (hand.Cards[i].Face == hand.Cards[j].Face)
                    {
                        counter++;
                    }
                }
                if (counter == 4)
                {
                    return true;
                }

                counter = 0;
            }

            return false;
        }
Beispiel #31
0
        private WinningHand CompareStraightHands(IHand firstHand, IHand secondHand)
        {
            var firstHandOrderedCards  = firstHand.Cards.OrderBy(x => x.Face).ToList();
            var secondHandOrderedCards = secondHand.Cards.OrderBy(x => x.Face).ToList();

            if (firstHandOrderedCards.Count(x => x.Face == CardFace.Ace) == 1)
            {
                firstHandOrderedCards.Insert(0, new Card(CardFace.Two, CardSuit.Clubs));
            }

            if (secondHandOrderedCards.Count(x => x.Face == CardFace.Ace) == 1)
            {
                secondHandOrderedCards.Insert(0, new Card(CardFace.Two, CardSuit.Clubs));
            }

            if (firstHandOrderedCards[2].Face != secondHandOrderedCards[2].Face)
            {
                return(firstHandOrderedCards[2].Face > secondHandOrderedCards[2].Face ? WinningHand.FirstHandWins : WinningHand.SeconHandWins);
            }
            else
            {
                return(WinningHand.HandsAreEqueal);
            }
        }
        public int BothHighCard(IHand firstHand, IHand secondHand, int index)
        {
            var firstSorted  = firstHand.Cards.ToArray().OrderBy(card => card.Face).ToArray();
            var secondSorted = secondHand.Cards.ToArray().OrderBy(card => card.Face).ToArray();

            if (index == 0)
            {
                if (firstSorted[0].Face > secondSorted[0].Face)
                {
                    return(-1);
                }
                else if (firstSorted[0].Face < secondSorted[0].Face)
                {
                    return(1);
                }
                else if (firstSorted[0].Face == secondSorted[0].Face)
                {
                    return(0);
                }
            }

            if (firstSorted[index].Face > secondSorted[index].Face)
            {
                return(-1);
            }
            else if (firstSorted[index].Face < secondSorted[index].Face)
            {
                return(1);
            }
            else if (firstSorted[index].Face == secondSorted[index].Face)
            {
                return(BothHighCard(firstHand, secondHand, index - 1));
            }

            throw new System.InvalidOperationException("Error");
        }
        public bool IsFlush(IHand hand)
        {
            int count = 1;

            for (int i = 0; i < hand.Cards.Count - 1; i++)
            {
                if (hand.Cards[i].Suit == hand.Cards[i + 1].Suit)
                {
                    count++;
                }
                else
                {
                    break;
                }
            }
            if (count == ValidHand)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #34
0
        public bool IsValidHand(IHand hand)
        {
            if (hand?.Cards.Count != 5)
            {
                return(false);
            }

            var sortedHand = hand.Cards
                             .OrderBy(x => x.Face)
                             .ThenBy(x => x.Suit)
                             .ToList();

            var curentCard = sortedHand[0];

            for (int i = 1; i < sortedHand.Count; i++)
            {
                if (curentCard.Face == sortedHand[i].Face && curentCard.Suit == sortedHand[i].Suit)
                {
                    return(false);
                }
                curentCard = sortedHand[i];
            }
            return(true);
        }
Beispiel #35
0
        public bool IsStraightFlush(IHand hand)
        {
            var desiredSuit = hand.Cards[0].Suit;

            for (int i = 1; i < hand.Cards.Count; i++)
            {
                if (hand.Cards[i].Suit != desiredSuit)
                {
                    return(false);
                }
            }

            var faces = new List <int>();

            for (int i = 0; i < hand.Cards.Count; i++)
            {
                faces.Add((int)hand.Cards[i].Face);
            }
            faces.Sort();

            var currentFace = faces[0];

            for (int i = 1; i < faces.Count; i++)
            {
                var nextFace = faces[i];
                if (nextFace - currentFace != 1)
                {
                    return(false);
                }

                currentFace = nextFace;
            }


            return(true);
        }
Beispiel #36
0
    public Board(IActiveCards active, IDrawPile draw, IDiscardPile discard, IHand hand)
    {
        if (hand == null)
        {
            throw new ArgumentNullException("hand");
        }
        if (draw == null)
        {
            throw new ArgumentNullException("draw");
        }
        if (active == null)
        {
            throw new ArgumentNullException("active");
        }
        if (discard == null)
        {
            throw new ArgumentNullException("discard");
        }

        this.hand        = hand;
        this.drawPile    = draw;
        this.activeCards = active;
        this.discardPile = discard;
    }
        public bool IsFullHouse(IHand hand)
        {
            HashSet <CardFace> cardFaceToNumber = new HashSet <CardFace>();
            HashSet <CardSuit> cardSuitToNumber = new HashSet <CardSuit>();

            foreach (var card in hand.Cards)
            {
                cardFaceToNumber.Add(card.Face);
                cardSuitToNumber.Add(card.Suit);
            }

            var cardSuitToNumberCount = cardSuitToNumber.Count;
            var cardFaceToNumberCount = cardFaceToNumber.Count;


            if ((cardSuitToNumberCount > 1) && (cardFaceToNumberCount == 2))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public bool IsFullHouse(IHand hand)
        {
            if (!IsThreeOfAKind(hand))
            {
                return(false);
            }
            CardFace setFace = CardFace.Joker;
            var      count   = 0;

            foreach (var card in hand.Cards)
            {
                foreach (var c in hand.Cards)
                {
                    if (card.Face.Equals(c.Face))
                    {
                        count++;
                    }
                    if (count > 2)
                    {
                        setFace = card.Face;
                    }
                }
                count = 0;
            }
            foreach (var card in hand.Cards)
            {
                foreach (var c in hand.Cards)
                {
                    if (!card.Face.Equals(setFace) && !card.Equals(c) && card.Face.Equals(c.Face))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #39
0
        public PlayerAction NextAction(IHand otherPlayersHand)
        {
            int otherTotal = otherPlayersHand.Score(false);

            if (otherPlayersHand.HiddenCardsCount() == 1 && otherTotal < 12)
            {
                // assume a hidden card is 10
                otherTotal += 10;
            }
            else if (otherPlayersHand.HiddenCardsCount() > 1 && otherTotal < 12)
            {   // assume hidden cards total at least 10, probably never more than 1 hidden card
                otherTotal += 10;
            }

            int thisTotal = _hand.Score(true);

            if (thisTotal > 21)
            {
                return(PlayerAction.Busted);
            }
            if (thisTotal >= otherTotal)
            {
                if (thisTotal > 10)
                {
                    return(PlayerAction.Stand);
                }
                else
                {
                    return(PlayerAction.Hit);
                }
            }
            else
            {
                return(PlayerAction.Hit);
            }
        }
        public bool IsHighCard(IHand hand)
        {
            if (!this.IsValidHand(hand))
            {
                return(false);
            }

            if (this.IsFlush(hand))
            {
                return(false);
            }

            if (this.IsStraight(hand) || this.IsStraightFlush(hand))
            {
                return(false);
            }

            if (this.MaxNumberOfSameCards(hand) == 1)
            {
                return(true);
            }

            return(false);
        }
Beispiel #41
0
 void IDeckEvents <TElement> .DrewInto(IHand <TElement> hand, TElement element)
 {
 }
Beispiel #42
0
 void IDeckEvents <TElement> .Playing(IHand <TElement> hand, TElement element)
 {
 }
Beispiel #43
0
 void IDeckEvents <TElement> .DrawingInto(IHand <TElement> hand)
 {
 }
Beispiel #44
0
 public bool IsHighCard(IHand hand)
 {
     throw new NotImplementedException();
 }
Beispiel #45
0
 public int CompareHands(IHand firstHand, IHand secondHand)
 {
     throw new NotImplementedException();
 }
 public GambleCardHand(IHand <IGambleCard> hand)
     : base(hand)
 {
 }
 public bool IsFlush(IHand hand)
 {
     throw new NotImplementedException();
 }
Beispiel #48
0
 public bool IsFullHouse(IHand hand)
 {
     throw new NotImplementedException();
 }
Beispiel #49
0
 public bool IsStraight(IHand hand)
 {
     throw new NotImplementedException();
 }
Beispiel #50
0
 public override void TearDown()
 {
     base.TearDown();
     _hand = null;
 }
Beispiel #51
0
 public override void Create() => _hand = new Hand(_id, Parameters, Dispatcher);
Beispiel #52
0
 public void OnCreateHand(IHand hand, PlayerId id)
 {
     _handCreated = true;
     Assert.IsTrue(id == _id);
 }
Beispiel #53
0
 public TexasHoldemPokerHand(IHand <IGambleCard> hand, TexasHoldemPokerRank rank = TexasHoldemPokerRank.HighCard)
     : base(hand, rank)
 {
     this.Rank = rank;
 }
Beispiel #54
0
 public TexasHoldemPokerHand(IHand <T> hand, TexasHoldemPokerRank rank = TexasHoldemPokerRank.HighCard)
     : base(hand)
 {
     this.Rank = rank;
 }
Beispiel #55
0
 void IDeckEvents <TElement> .DrewInto(ITableau <TElement> tableau, IHand <TElement> hand, TElement element)
 {
 }
Beispiel #56
0
 public bool IsThreeOfAKind(IHand hand)
 {
     throw new NotImplementedException();
 }
Beispiel #57
0
 void IDeckEvents <TElement> .Mucking(IHand <TElement> hand)
 {
 }
Beispiel #58
0
 public bool IsOnePair(IHand hand)
 {
     throw new NotImplementedException();
 }
 public GambleCardHand(IHand <T> hand)
     : base(new List <T>(hand))
 {
 }
 public bool IsValidHand(IHand hand)
 {
     throw new NotImplementedException();
 }