コード例 #1
0
        public PokerPlayerHandEvaluator(PlayingCardList playerCards, PlayingCardList tableCards)
            : base(null)
        {
            Debug.Assert(playerCards.Count == 2);
            Debug.Assert(tableCards.Count == 5);

            Tuple<HandType, ulong> bestHandValue = HandValue(tableCards);
            PlayingCardList bestHand = tableCards;

            // swap one, or the other player card for any one table card.
            foreach(var card in playerCards) {
                for(int i = 0; i < 5; i++) {
                    var cards = new PlayingCardList();
                    for(int iNewHand = 0; iNewHand < 5; iNewHand++) {
                        if(iNewHand == i) {
                            cards.Add(card);
                        } else {
                            cards.Add(tableCards[iNewHand]);
                        }
                    }
                    var newHand = new PlayingCardList(cards);
                    Tuple<HandType, ulong> newHandValue = HandValue(newHand);
                    if(newHandValue.Item2 > bestHandValue.Item2) {
                        bestHandValue = newHandValue;
                        bestHand = newHand;
                    }
                }
            }
            // swap both for any two table cards.
            for(int iCard1 = 0; iCard1 < 4; iCard1++) {
                for(int iCard2 = iCard1; iCard2 < 5; iCard2++) {
                    var cards = new PlayingCardList();
                    for(int iNewHand = 0; iNewHand < 5; iNewHand++) {
                        if(iNewHand == iCard1) {
                            cards.Add(playerCards[0]);
                        } else if(iNewHand == iCard2) {
                            cards.Add(playerCards[1]);
                        } else {
                            cards.Add(tableCards[iNewHand]);
                        }
                    }
                    var newHand = new PlayingCardList(cards);
                    Tuple<HandType, ulong> newHandValue = HandValue(newHand);
                    if(newHandValue.Item2 > bestHandValue.Item2) {
                        bestHandValue = newHandValue;
                        bestHand = newHand;
                    }
                }
            }

            this.cards = bestHand;
            this.handType = bestHandValue.Item1;
            this.handValue = bestHandValue.Item2;
        }
コード例 #2
0
ファイル: Deck.cs プロジェクト: DarkDevelCoding/HardlyBot
        static PlayingCardList GenerateStandardDeck(bool includeJokers)
        {
            int cardCount = 52 + (includeJokers ? 2 : 0);
            PlayingCardList cards = new PlayingCardList();
            for(int i = 0; i < cardCount; i++) {
                PlayingCard.Suit suit = (PlayingCard.Suit)(i % 4);
                PlayingCard.Value value = (PlayingCard.Value)((i - (int)suit) / 4);
                cards.Add(new PlayingCard(suit, value));
            }

            return cards;
        }
コード例 #3
0
        public void HandValueTest()
        {
            var straightFlush = new PlayingCardList(new[] {
                new PlayingCard(PlayingCard.Suit.Clubs, PlayingCard.Value.Jack),
                new PlayingCard(PlayingCard.Suit.Clubs, PlayingCard.Value.Ten),
                new PlayingCard(PlayingCard.Suit.Clubs, PlayingCard.Value.Seven),
                new PlayingCard(PlayingCard.Suit.Clubs, PlayingCard.Value.Nine),
                new PlayingCard(PlayingCard.Suit.Clubs, PlayingCard.Value.Eight),
            });
            var fourOfAKind = new PlayingCardList(new[] {
                new PlayingCard(PlayingCard.Suit.Clubs, PlayingCard.Value.Six),
                new PlayingCard(PlayingCard.Suit.Clubs, PlayingCard.Value.Jack),
                new PlayingCard(PlayingCard.Suit.Diamonds, PlayingCard.Value.Six),
                new PlayingCard(PlayingCard.Suit.Clubs, PlayingCard.Value.Six),
                new PlayingCard(PlayingCard.Suit.Clubs, PlayingCard.Value.Six),
            });

               // Assert.IsTrue(PokerPlayerHand.HandValue(straightFlush) > PokerPlayerHand.HandValue(fourOfAKind));
        }
コード例 #4
0
 public BlackjackCardListEvaluator(PlayingCardList cards)
     : base(cards)
 {
 }
コード例 #5
0
        static Tuple<HandType, ulong> HandValue(PlayingCardList cards)
        {
            Debug.Assert(cards.Count == 5);

            cards.Sort();

            PlayingCard.Value? firstPairValue, secondPairValue;
            uint firstPairCardCount, secondPairCardCount;
            bool isStraight;
            bool isFlush;
            CalcHandStats(cards, out isFlush, out firstPairValue, out secondPairValue, out firstPairCardCount, out secondPairCardCount, out isStraight);

            HandType myHandType;
            myHandType = GetHandType(firstPairCardCount, secondPairCardCount, isStraight, isFlush);

            ulong myHandValue;
            switch(myHandType) {
            case HandType.StraightFlush:
            case HandType.Flush:
            case HandType.HighCard:
            case HandType.Straight:
                if(myHandType == HandType.Straight && cards[4].value == PlayingCard.Value.Ace) {
                    myHandValue = 0;
                } else {
                    myHandValue = GetValue(cards[4].value, cards[3].value, cards[2].value, cards[1].value, cards[0].value);
                }
                break;
            case HandType.FourOfAKind:
            case HandType.ThreeOfAKind:
            case HandType.OnePair:
                int iStartOfPair = 0;
                for(int i = 0; i < 5; i++) {
                    if(cards[i].value == firstPairValue.Value) {
                        iStartOfPair = i;
                        break;
                    }
                }
                myHandValue = GetValue(cards[iStartOfPair].value, cards[iStartOfPair + 1].value,
                    cards[(iStartOfPair + 2) % 5].value, cards[(iStartOfPair + 3) % 5].value, cards[(iStartOfPair + 4) % 5].value);
                break;
            case HandType.FullHouse:
                PlayingCard.Value largerValue = firstPairValue.Value;
                PlayingCard.Value smallerValue = secondPairValue.Value;
                if(secondPairCardCount > firstPairCardCount) {
                    largerValue = secondPairValue.Value;
                    smallerValue = firstPairValue.Value;
                }
                myHandValue = GetValue(largerValue, largerValue, largerValue, smallerValue, smallerValue);
                break;
            case HandType.TwoPair:
                PlayingCard.Value lastCard = 0;
                foreach(var card in cards) {
                    if(card.value != firstPairValue.Value && card.value != secondPairValue.Value) {
                        lastCard = card.value;
                        break;
                    }
                }
                myHandValue = GetValue(secondPairValue.Value, secondPairValue.Value, firstPairValue.Value, firstPairValue.Value, lastCard);
                break;
            default:
                myHandValue = 0;
                Debug.Fail();
                break;
            }

            return Tuple.Create(myHandType, (ulong)myHandType * 100000000 + myHandValue);
        }
コード例 #6
0
        static bool CheckForFlush(PlayingCardList playerCards)
        {
            PlayingCard.Suit flushSuit = playerCards[0].suit;
            foreach(var card in playerCards) {
                if(card.suit != flushSuit) {
                    return false;
                }
            }

            return true;
        }
コード例 #7
0
        static void CalcHandStats(PlayingCardList playerCards, out bool isFlush, out PlayingCard.Value? firstPairValue, out PlayingCard.Value? secondPairValue, out uint firstPairCardCount, out uint secondPairCardCount, out bool isStraight)
        {
            Debug.Assert(playerCards.Count == 5);

            PlayingCard.Value? lastCardValue = null;
            firstPairValue = null;
            secondPairValue = null;
            firstPairCardCount = 0;
            secondPairCardCount = 0;
            isStraight = true;
            isFlush = CheckForFlush(playerCards);

            foreach(var card in playerCards) {
                if(lastCardValue != null) {
                    if(!((card.value == lastCardValue.Value + 1) || (card.value == PlayingCard.Value.Ace && lastCardValue.Value == PlayingCard.Value.Five))) {
                        isStraight = false;
                    }

                    if(card.value == lastCardValue.Value) {
                        if(firstPairValue == null) {
                            firstPairValue = card.value;
                            firstPairCardCount = 2;
                        } else if(card.value == firstPairValue) {
                            firstPairCardCount++;
                        } else if(card.value == secondPairValue) {
                            secondPairCardCount++;
                        } else {
                            Debug.Assert(secondPairValue == null);
                            secondPairValue = card.value;
                            secondPairCardCount = 2;
                        }
                    }
                }
                lastCardValue = card.value;
            }
        }
コード例 #8
0
 public PlayingCardListEvaluator(PlayingCardList cards)
 {
     this.cards = cards;
 }