基本信息
源码名称:德州扑克核心算法
源码大小:3.36KB
文件格式:.rar
开发语言:C#
更新时间:2018-03-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
源码不完整,仅供参考

using System.Collections.Generic;
using Ourgame.Define;
using System.Linq;

namespace Ourgame.Utils
{
    public enum Pattern
    {
        // 皇家同花顺
        RoyalFlush,
        // 同花顺
        StraightFlush,
        // 四条
        FourKind,
        // 葫芦
        FullHouse,
        // 同花
        Flush,
        // 顺子
        Straight,
        // 三条
        ThreeKind,
        // 两对
        TwoPair,
        // 一对
        OnePair,
        // 高牌
        HighCard,
    }

    public class CardGroup
    {
        // 牌型
        public Pattern Pattern;

        // 符合牌型的牌值列表(四条、三条、两对、一对的使用牌值,高牌为空数组)
        public List<Card> PatternCards;

        // 组成牌组的五张牌(若不足五张时,Cards == PatternCards)
        public List<Card> Cards;
    }

    public class PatternAnalyzer
    {
        private List<Card> _cardList = new List<Card>();

        private Pattern cardType = Pattern.HighCard;

        private List<Card> PatternCards = new List<Card>();

        private List<Card> Cards = new List<Card>();

        public CardGroup GetGroup(List<Card> cards)
        {
            CardGroup cardGroup = new CardGroup();

            _cardList = cards;
            SortByIDOrPoint(2);

            if (HaveRoyalFlush())
            {
                cardType = Pattern.RoyalFlush;
            }
            else if (HaveStraightFlush())
            {
                cardType = Pattern.StraightFlush;
            }
            else if (HaveFourKind())
            {
                cardType = Pattern.FourKind;
            }
            else if (HaveFullHouse())
            {
                cardType = Pattern.FullHouse;
            }
            else if (HaveFlush())
            {
                cardType = Pattern.Flush;
            }
            else if (HaveStraight())
            {
                cardType = Pattern.Straight;
            }
            else if (HaveThreeKind())
            {
                cardType = Pattern.ThreeKind;
            }
            else if (HaveTwoPair())
            {
                cardType = Pattern.TwoPair;
            }
            else if (HaveOnePair())
            {
                cardType = Pattern.OnePair;
            }
            else
            {
                FindHigh();
                cardType = Pattern.HighCard;
            }

            cardGroup.Pattern = cardType;
            cardGroup.PatternCards = PatternCards;
            cardGroup.Cards = Cards;

            return cardGroup;
        }

        // 皇家同花顺
        public bool HaveRoyalFlush()
        {
            if (_cardList.Count < 5)  // 条件不满足
                return false;

            int num = 0;
            int max = 0;
            PokerColor pokerColor = PokerColor.UNKNOW;
            for (int i = 0; i < _cardList.Count; i  )
            {
                for (int j = 0; j < _cardList.Count; j  )
                {
                    if (_cardList[i].Color == _cardList[j].Color)
                        num  ;
                }
                if (num > max)  // 找到花色出现最多的那种
                {
                    max = num;
                    pokerColor = (PokerColor)_cardList[i].Color;
                }
                num = 0;
            }

            List<Card> tempList = new List<Card>();
            for (int i = 0; i < _cardList.Count; i  )   // 寻找同花色牌
            {
                if (_cardList[i].Color == (int)pokerColor)
                    tempList.Add(_cardList[i]);
            }

            if (tempList.Count < 5)  // 同花色牌小于5张
                return false;

            if (tempList[0].Point != 0)   // 必须为A
                return false;
            else
                Cards.Add(tempList[0]);

            if (tempList.Count == 6)
            {
                tempList.RemoveRange(0, 2);
            }
            else if (tempList.Count == 7)
            {
                tempList.RemoveRange(0, 3);
            }
            else
            {
                tempList.RemoveAt(0);
            }
            Cards.AddRange(tempList);

            Card card = new Card();
            card.Point = 13;
            tempList.Add(card);

            // 是否连续
            if (!IsContinuous(tempList))
                return false;

            return true;
        }

        // 同花顺
        public bool HaveStraightFlush()
        {
            if(HaveFlush() && HaveStraight())
                return true;
            return false;
        }

        // 四条
        public bool HaveFourKind()
        {
            if (_cardList.Count < 4)   // 条件不满足
                return false;
            Cards = new List<Card>();
            PatternCards = new List<Card>();
            SortByIDOrPoint(1);

            bool isEqual = true;
            for (int i = 0; i < _cardList.Count - 3; i  )   // -3: 整体后三位不需要再比
            {
                for (int j = i; j < i   4; j  )   //  3 : 四位与后三位进行比较
                {
                    if (_cardList[i].Point != _cardList[j].Point)
                    {
                        isEqual = false;
                        break;
                    }
                }
                if (isEqual)
                {
                    for (int j = i; j < i   4; j  )   //  3 : 四位与后三位进行比较
                    {
                        PatternCards.Add(_cardList[j]);
                    }
                    Cards.AddRange(PatternCards);
                    PatternCards.AddRange(Cards);
                    return true;
                }
                isEqual = true;
            }
            return false;
        }

        // 葫芦
        public bool HaveFullHouse()
        {
            if (_cardList.Count < 5)  // 条件不满足
                return false;
            Cards = new List<Card>();
            PatternCards = new List<Card>();
            List<Card> tempList = new List<Card>();
            int three = -1;
            tempList = IsContainThree(_cardList, out three);

            if (tempList != null)   // 葫芦的三
                Cards.AddRange(tempList);
            else
                return false;

            int two = -1;
            tempList = IsContainTwoAndBesides(_cardList, out two, three);
            if (tempList != null)   // 葫芦的二
                Cards.AddRange(tempList);
            else
                return false;

            return true;
        }

        // 同花
        public bool HaveFlush()
        {
            if (_cardList.Count < 5) // 条件不满足
                return false;
            Cards = new List<Card>();
            PatternCards = new List<Card>();
            SortByIDOrPoint(2);

            bool isEqual = true;
            for (int i = 0; i < _cardList.Count - 4; i  )
            {
                isEqual = true;
                for (int j = i   1; j < i   5; j  )
                {
                    if (_cardList[i].Color != _cardList[j].Color)
                    {
                        isEqual = false;
                        break;
                    }
                }

                if (isEqual)
                {
                    Cards.Add(_cardList[i]);
                    for (int j = i   1; j < i   5; j  )
                    {
                        Cards.Add(_cardList[j]);
                    }
                    return true;
                }
                    
            }

            return false;
        }

        // 顺子
        public bool HaveStraight()
        {
            if (_cardList.Count < 5) // 条件不满足
                return false;
            Cards = new List<Card>();
            PatternCards = new List<Card>();
            SortByIDOrPoint(1);

            List<Card> cardList = KillTheSame(_cardList);
            List<Card> tempList = new List<Card>();

            cardList = cardList.OrderByDescending(a => a.Point).ToList();

            for (int i = 0; i < cardList.Count; i  )
            {
                if(cardList[i].Point == 0) // 有A
                {
                    tempList.Add(cardList[i]);
                    for (int j = cardList.Count - 1; j > cardList.Count - 5; j--)
                    {
                        tempList.Add(cardList[j]);
                    }
                    if (IsContinuous(tempList))
                    {
                        Cards.AddRange(tempList);
                        return true;
                    }
                }
            }

            tempList.Clear();
            for (int i = 0; i < cardList.Count - 4; i  )
            {
                for (int j = i; j < i 5; j  )
                {
                    tempList.Add(cardList[j]);
                }

                if (IsContinuous(tempList))
                {
                    Cards.AddRange(tempList);
                    return true;
                }
                tempList.Clear();
            }
            return false;
        }

        // 三条
        public bool HaveThreeKind()
        {
            if (_cardList.Count < 3)  // 条件不满足
                return false;
            Cards = new List<Card>();
            PatternCards = new List<Card>();
            SortByIDOrPoint(1);

            int three = -1;
            List<Card> tempList = new List<Card>();
            tempList = IsContainThree(_cardList, out three);
            if (tempList != null) // 存在三位
            {
                PatternCards.AddRange(tempList);
                Cards.AddRange(PatternCards);
                return true;
            }
            return false;
        }

        // 二对
        public bool HaveTwoPair()
        {
            if (_cardList.Count < 2)   // 条件不满足
                return false;
            Cards = new List<Card>();
            PatternCards = new List<Card>();
            int two = -1;
            List<Card> tempList = new List<Card>();
            tempList = IsContainTwoAndBesides(_cardList,out two);
            if (tempList == null)    // 第一对
                return false;
            else
                PatternCards.AddRange(tempList);

            tempList = IsContainTwoAndBesides(_cardList, out two,two);
            if (tempList != null)   // // 第二对
            {
                PatternCards.AddRange(tempList);
                Cards.AddRange(PatternCards);
                return true;
            }

            return false;
        }

        // 一对
        public bool HaveOnePair()
        {
            if (_cardList.Count < 2) // 条件不满足
                return false;
            Cards = new List<Card>();
            PatternCards = new List<Card>();

            int two = -1;
            List<Card> tempList = new List<Card>();
            tempList = IsContainTwoAndBesides(_cardList, out two);
            if ( tempList != null)  // 找到一对
            {
                PatternCards.AddRange(tempList);
                Cards.AddRange(tempList);
                return true;
            }
            return false;
        }

        // 寻找高牌
        public void FindHigh()
        {
            Cards = new List<Card>();
            PatternCards = new List<Card>();
            for (int i = 0; i < _cardList.Count; i  )
            {
                if (_cardList[i].Point == 0) // 如果是1点
                {
                    PatternCards.Add(_cardList[i]);
                    Cards.AddRange(PatternCards);
                    return;
                }
            }
            PatternCards.Add(_cardList[_cardList.Count - 1]);
            Cards.AddRange(PatternCards);
        }

        // 除开besides连续的两个数
        List<Card> IsContainTwoAndBesides(List<Card> cardList, out int two, int besides = -1)
        {
            List<Card> tempList = new List<Card>();
            bool isEqual = false;
            for (int i = 0; i < cardList.Count - 2; i  )   // -3: 整体后俩位不需要再比
            {
                isEqual = false;
                for (int j = i   1; j < i   3; j  )   //  3 : 四位与后三位进行比较
                {
                    if (cardList[i].Point == cardList[j].Point)
                    {
                        isEqual = true;
                        break;
                    }
                }
                if (isEqual && (cardList[i].Point != besides || besides == -1))   // 除开besides
                {
                    two = cardList[i].Point;
                    tempList.Add(cardList[i]);
                    for (int j = i   1; j < i   3; j  )   //  3 : 四位与后三位进行比较
                    {
                        if(cardList[j].Point == cardList[i].Point)
                            tempList.Add(cardList[j]);
                    }
                    return tempList;
                }
                    
            }
            two = -1;
            return null;
        }

        // 是否包含三条
        List<Card> IsContainThree(List<Card> cardList,out int three)
        {
            List<Card> tempList = new List<Card>();
            bool isEqual = true;
            for (int i = 0; i < cardList.Count - 2; i  )   // -3: 整体后俩位不需要再比
            {
                isEqual = true;
                for (int j = i   1; j < i   3; j  )   //  3 : 四位与后三位进行比较
                {
                    if (cardList[i].Point != cardList[j].Point)
                    {
                        isEqual = false;
                        break;
                    }
                }

                if (isEqual)
                {
                    three = cardList[i].Point;
                    tempList.Add(cardList[i]);
                    for (int j = i   1; j < i   3; j  )   //  3 : 四位与后三位进行比较
                    {
                        tempList.Add(cardList[j]);
                    }
                    return tempList;
                }
            }
            three = -1;
            return null;
        }

        // Point是否是连续
        bool IsContinuous(List<Card> cardList)
        {
            List<Card> tempList = new List<Card>();

            for (int i = 0; i < cardList.Count; i  )
            {
                if(cardList[i].Point == 0) // 存在A
                {
                    Card card = new Card();
                    card.Point = 13;   // 只是将A的点数改到13,用于判断是否连续
                    tempList.Add(card);
                }
                else
                {
                    tempList.Add(cardList[i]);
                }
            }
            tempList.Sort((a, b) =>
            {
                if (a.Point > b.Point)
                {
                    return 1;
                }
                else if (a.Point < b.Point)
                {
                    return -1;
                }
                return 0;
            });
            if(tempList.Count>0)
                if (tempList[cardList.Count - 1].Point - tempList[0].Point  == cardList.Count - 1)   // 因A两种身份  这里是第一种
                    return true;
            return cardList[cardList.Count - 1].Point - cardList[0].Point == cardList.Count - 1 ? true : false;    // 因A两种身份  这里是第二种
        }

        // 是否有五个连续的数
        List<Card> IsFiveContinuousInSeven(List<Card> cardList)
        {
            List<Card> tempList = new List<Card>();
            for (int i = 0; i < cardList.Count - 4; i  )
            {
                tempList.Clear();
                for (int j = i; j < i   5; j  )
                {
                    tempList.Add(cardList[j]);
                }

                if (IsContinuous(tempList))  // 有连续相同的数
                    return tempList;
            }
            return null;
        }

        // 去除相同点数的牌,用于判断顺子
        List<Card> KillTheSame(List<Card> cardList)
        {
            List<Card> tempList = new List<Card>();

            List<int> existList = new List<int>();
            for (int i = 0; i < cardList.Count; i  )
            {
                if(existList.Contains(cardList[i].Point))
                {
                    continue;
                }
                else
                {
                    tempList.Add(cardList[i]);
                    existList.Add(cardList[i].Point);
                }
            }
            return tempList;
        }

        public void SortByIDOrPoint(int type)
        {
            if(type == 1) // 根据Point升序
            {
                _cardList = _cardList.OrderBy(a => a.Point).ToList();
            }
            else if(type == 2)   // 根据ID升序
            {
                _cardList = _cardList.OrderBy(a => a.ID).ToList();
            }
            else if(type == 3)    // 根据Point降序
            {
                _cardList = _cardList.OrderByDescending(a => a.Point).ToList();
            }
            else if(type == 4)   // 根据ID降序
            {
                _cardList = _cardList.OrderByDescending(a => a.ID).ToList();
            }
        }
    }
}