The simplest algorithm for poker hand evaluation

后端 未结 12 1024
独厮守ぢ
独厮守ぢ 2020-12-04 12:39

I am thinking about poker hand (5 cards) evaluation in Java. Now I am looking for simplicity and clarity rather than performance and efficiency. I probably can

12条回答
  •  春和景丽
    2020-12-04 13:20

     public class Line
    {
        private List _cardsToAnalyse;
    
        public Line()
        {
            Cards = new List(5);
        }
    
        public List Cards { get; }
    
    
        public string PriceName { get; private set; }
    
    
        public int Result()
        {
            _cardsToAnalyse = Cards;
            var valueComparer = new CardValueComparer();
    
    
            _cardsToAnalyse.Sort(valueComparer);
    
            if (ContainsStraightFlush(_cardsToAnalyse))
            {
                PriceName = "Straight Flush";
                return PayTable.StraightFlush;
            }
    
            if (ContainsFourOfAKind(_cardsToAnalyse))
            {
                PriceName = "Quadra";
                return PayTable.FourOfAKind;
            }
    
            if (ContainsStraight(_cardsToAnalyse))
            {
                PriceName = "Straight";
                return PayTable.Straight;
            }
    
            if (ContainsFullen(_cardsToAnalyse))
            {
                PriceName = "Full House";
                return PayTable.Fullen;
            }
    
            if (ContainsFlush(_cardsToAnalyse))
            {
                PriceName = "Flush";
                return PayTable.Flush;
            }
    
            if (ContainsThreeOfAKind(_cardsToAnalyse))
            {
                PriceName = "Trinca";
                return PayTable.ThreeOfAKind;
            }
    
            if (ContainsTwoPairs(_cardsToAnalyse))
            {
                PriceName = "Dois Pares";
                return PayTable.TwoPairs;
            }
    
            if (ContainsPair(_cardsToAnalyse))
            {
                PriceName = "Um Par";
                return PayTable.Pair;
            }
    
            return 0;
        }
    
        private bool ContainsFullen(List _cardsToAnalyse)
        {
            var valueOfThree = 0;
    
            // Search for 3 of a kind
            Card previousCard1 = null;
            Card previousCard2 = null;
    
            foreach (var c in Cards)
            {
                if (previousCard1 != null && previousCard2 != null)
                    if (c.Value == previousCard1.Value && c.Value == previousCard2.Value)
                        valueOfThree = c.Value;
                previousCard2 = previousCard1;
                previousCard1 = c;
            }
    
            if (valueOfThree > 0)
            {
                Card previousCard = null;
    
                foreach (var c in Cards)
                {
                    if (previousCard != null)
                        if (c.Value == previousCard.Value)
                            if (c.Value != valueOfThree)
                                return true;
                    previousCard = c;
                }
    
                return false;
            }
    
            return false;
        }
    
        private bool ContainsPair(List Cards)
        {
            Card previousCard = null;
    
            foreach (var c in Cards)
            {
                if (previousCard != null)
                    if (c.Value == previousCard.Value)
                        return true;
                previousCard = c;
            }
    
            return false;
        }
    
        private bool ContainsTwoPairs(List Cards)
        {
            Card previousCard = null;
            var countPairs = 0;
            foreach (var c in Cards)
            {
                if (previousCard != null)
                    if (c.Value == previousCard.Value)
                        countPairs++;
                previousCard = c;
            }
    
            if (countPairs == 2)
                return true;
    
            return false;
        }
    
        private bool ContainsThreeOfAKind(List Cards)
        {
            Card previousCard1 = null;
            Card previousCard2 = null;
    
            foreach (var c in Cards)
            {
                if (previousCard1 != null && previousCard2 != null)
                    if (c.Value == previousCard1.Value && c.Value == previousCard2.Value)
                        return true;
                previousCard2 = previousCard1;
                previousCard1 = c;
            }
    
            return false;
        }
    
        private bool ContainsFlush(List Cards)
        {
            return Cards[0].Naipe == Cards[1].Naipe &&
                   Cards[0].Naipe == Cards[2].Naipe &&
                   Cards[0].Naipe == Cards[3].Naipe &&
                   Cards[0].Naipe == Cards[4].Naipe;
        }
    
        private bool ContainsStraight(List Cards)
        {
            return Cards[0].Value + 1 == Cards[1].Value &&
                   Cards[1].Value + 1 == Cards[2].Value &&
                   Cards[2].Value + 1 == Cards[3].Value &&
                   Cards[3].Value + 1 == Cards[4].Value
                   ||
                   Cards[1].Value + 1 == Cards[2].Value &&
                   Cards[2].Value + 1 == Cards[3].Value &&
                   Cards[3].Value + 1 == Cards[4].Value &&
                   Cards[4].Value == 13 && Cards[0].Value == 1;
        }
    
        private bool ContainsFourOfAKind(List Cards)
        {
            Card previousCard1 = null;
            Card previousCard2 = null;
            Card previousCard3 = null;
    
            foreach (var c in Cards)
            {
                if (previousCard1 != null && previousCard2 != null && previousCard3 != null)
                    if (c.Value == previousCard1.Value &&
                        c.Value == previousCard2.Value &&
                        c.Value == previousCard3.Value)
                        return true;
                previousCard3 = previousCard2;
                previousCard2 = previousCard1;
                previousCard1 = c;
            }
    
            return false;
        }
    
        private bool ContainsStraightFlush(List Cards)
        {
            return ContainsFlush(Cards) && ContainsStraight(Cards);
        }
    }
    

提交回复
热议问题