unity开发 斗地主算法—提示AI(提示出牌)

牌型的定义在http://blog.csdn.net/csdn_cjt/article/details/78593140 第一章 这是第四章


下面是代码


 #region 提示出牌 
    public static Dictionary> FindPromptCards(List myCards, 
        List lastCards, DDZ_POKER_TYPE lastCardType) {
        Dictionary> PromptCards = new Dictionary>();
        Hashtable tempMyCardHash = SortCardUseHash1(myCards);

        // 上一首牌的个数
        int prevSize = lastCards.Count;
        int mySize = myCards.Count;
        
        // 我先出牌,上家没有牌
        if (prevSize == 0 && mySize != 0)
        {
            //把所有牌权重存入返回
            Debug.Log("上家没有牌");
            List myCardsHashKey = new List();
            foreach (int key in tempMyCardHash.Keys)
            {
                myCardsHashKey.Add(key);
            }
            myCardsHashKey.Sort();
            for (int i = 0; i < myCardsHashKey.Count; i++) {
                List tempIntList = new List();
                tempIntList.Add(myCardsHashKey[i]);
                PromptCards.Add(i, tempIntList);
                
            }
        }

        // 集中判断是否王炸,免得多次判断王炸
        if (lastCardType == DDZ_POKER_TYPE.KingBomb)
        {
            Debug.Log("上家王炸,肯定不能出。");
            
        }
        int prevGrade = 0;
        if (prevSize > 0) {
            prevGrade = lastCards[0];
            Debug.Log("prevGrade" + prevGrade);
        }

       

        // 比较2家的牌,主要有2种情况,1.我出和上家一种类型的牌,即对子管对子;
        // 2.我出炸弹,此时,和上家的牌的类型可能不同
        // 王炸的情况已经排除

        // 上家出单
        if (lastCardType == DDZ_POKER_TYPE.Single)
        {
            int tempCount = 0;
            List myCardsHashKey = new List();
            foreach (int key in tempMyCardHash.Keys)
            {
                myCardsHashKey.Add(key);
            }
            myCardsHashKey.Sort();
            for (int i = 0; i < myCardsHashKey.Count; i++)
            {
                if (myCardsHashKey[i] > prevGrade){
                    List tempIntList = new List();
                    tempIntList.Add(myCardsHashKey[i]);
                    PromptCards.Add(tempCount, tempIntList);
                    tempCount++;
                }
               

            }
           

        }
        // 上家出对子
        else if (lastCardType == DDZ_POKER_TYPE.Twin)
        {
            int tempCount = 0;
            List myCardsHashKey = new List();
            foreach (int key in tempMyCardHash.Keys)
            {
                myCardsHashKey.Add(key);
            }
            myCardsHashKey.Sort();
            for (int i = 0; i < myCardsHashKey.Count; i++)
            {
                if (myCardsHashKey[i] > prevGrade&& (int)tempMyCardHash[myCardsHashKey[i]]>=2)
                {
                    List tempIntList = new List();
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(myCardsHashKey[i]);
                    PromptCards.Add(tempCount, tempIntList);
                    tempCount++;
                }


            }

        }
        // 上家出3不带
        else if (lastCardType == DDZ_POKER_TYPE.Triple)
        {
            int tempCount = 0;
            List myCardsHashKey = new List();
            foreach (int key in tempMyCardHash.Keys)
            {
                myCardsHashKey.Add(key);
            }
            myCardsHashKey.Sort();
            for (int i = 0; i < myCardsHashKey.Count; i++)
            {
                if (myCardsHashKey[i] > prevGrade && (int)tempMyCardHash[myCardsHashKey[i]] >= 3)
                {
                    List tempIntList = new List();
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(myCardsHashKey[i]);
                    PromptCards.Add(tempCount, tempIntList);
                    tempCount++;
                }


            }

        }
        // 上家出3带1
        else if (lastCardType == DDZ_POKER_TYPE.TripleWithSingle)
        {
            // 3带1 3不带 比较只多了一个判断条件
            if (mySize < 4)
            {
                
            }
            int grade3=0;
            foreach (int key in tempMyCardHash.Keys)
            {
                if (int.Parse(tempMyCardHash[key].ToString()) == 1) {
                    grade3 = key;
                    break;
                }
            }
            int tempCount = 0;
            List myCardsHashKey = new List();
            foreach (int key in tempMyCardHash.Keys)
            {
                myCardsHashKey.Add(key);
            }
            myCardsHashKey.Sort();
            for (int i = 0; i < myCardsHashKey.Count; i++)
            {
                if (myCardsHashKey[i] > prevGrade && (int)tempMyCardHash[myCardsHashKey[i]] >= 3)
                {
                    List tempIntList = new List();
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(grade3);
                    PromptCards.Add(tempCount, tempIntList);
                    tempCount++;
                }


            }

        }
        // 上家出3带2
        else if (lastCardType == DDZ_POKER_TYPE.TripleWithTwin)
        {
            // 3带1 3不带 比较只多了一个判断条件
            if (mySize < 5)
            {
                
            }
            int grade3 = 0;
            int grade4 = 0;
            foreach (int key in tempMyCardHash.Keys)
            {
                if (int.Parse(tempMyCardHash[key].ToString()) == 2)
                {
                    grade3 = key;
                    grade4 = key;
                    break;
                }
            }
            int tempCount = 0;
            List myCardsHashKey = new List();
            foreach (int key in tempMyCardHash.Keys)
            {
                myCardsHashKey.Add(key);
            }
            myCardsHashKey.Sort();
            for (int i = 0; i < myCardsHashKey.Count; i++)
            {
                if (myCardsHashKey[i] > prevGrade && (int)tempMyCardHash[myCardsHashKey[i]] >= 3)
                {
                    List tempIntList = new List();
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(grade3);
                    tempIntList.Add(grade4);
                    PromptCards.Add(tempCount, tempIntList);
                    tempCount++;
                }


            }

        }
        // 上家出炸弹
        else if (lastCardType == DDZ_POKER_TYPE.FourBomb)
        {
            int tempCount = 0;
            // 4张牌可以大过上家的牌
            for (int i = mySize - 1; i >= 3; i--)
            {
                int grade0 = myCards[i];
                int grade1 = myCards[i - 1];
                int grade2 = myCards[i - 2];
                int grade3 = myCards[i - 3];
                
                if (grade0 == grade1 && grade0 == grade2 && grade0 == grade3)
                {
                    if (grade0 > prevGrade)
                    {
                        // 把四张牌存进去
                        List tempIntList = new List();
                        tempIntList.Add(grade0);
                        tempIntList.Add(grade1);
                        tempIntList.Add(grade2);
                        tempIntList.Add(grade3);
                        
                        PromptCards.Add(tempCount, tempIntList);
                        tempCount++;
                    }
                }
            }

        }
        // 上家出4带2 
        else if (lastCardType == DDZ_POKER_TYPE.FourWithSingle)
        {
            // 4张牌可以大过上家的牌
            for (int i = mySize - 1; i >= 3; i--)
            {
                int grade0 = myCards[i];
                int grade1 = myCards[i - 1];
                int grade2 = myCards[i - 2];
                int grade3 = myCards[i - 3];

                if (grade0 == grade1 && grade0 == grade2 && grade0 == grade3)
                {
                    // 只要有炸弹,则返回true
                    
                }
            }
        }
        // 上家出4带2 对子
        else if (lastCardType == DDZ_POKER_TYPE.FourWithSingle)
        {
            // 4张牌可以大过上家的牌
            for (int i = mySize - 1; i >= 3; i--)
            {
                int grade0 = myCards[i];
                int grade1 = myCards[i - 1];
                int grade2 = myCards[i - 2];
                int grade3 = myCards[i - 3];

                if (grade0 == grade1 && grade0 == grade2 && grade0 == grade3)
                {
                    // 只要有炸弹,则返回true

                }
            }
        }
        // 上家出顺子
        else if (lastCardType == DDZ_POKER_TYPE.StraightSingle)
        {
            if (mySize < prevSize)
            {
                
            }
            else
            {
                List tempMyCards = new List();
                tempMyCards = myCards;
                Hashtable myCardsHash = SortCardUseHash(tempMyCards);
                if (myCardsHash.Count < prevSize)
                {
                    Debug.Log("hash的总数小于顺子的count 肯定fales");
                    
                }
                List myCardsHashKey = new List();
                foreach (int key in myCardsHash.Keys)
                {
                    myCardsHashKey.Add(key);
                }
                myCardsHashKey.Sort();
                int tempCount = 0;
                for (int i = myCardsHashKey.Count - 1; i >= prevSize - 1; i--)
                {
                    List cards = new List();
                    for (int j = 0; j < prevSize; j++)
                    {
                        cards.Add(myCardsHashKey[myCardsHashKey.Count - 1 - i + j]);
                    }
                    DDZ_POKER_TYPE myCardType = DDZ_POKER_TYPE.DdzPass;
                    bool isRule = DDZCardRule.PopEnable(cards, out myCardType);
                    
                    if (myCardType == DDZ_POKER_TYPE.StraightSingle)
                    {
                        int myGrade2 = cards[cards.Count - 1];// 最大的牌在最后
                        int prevGrade2 = lastCards[prevSize - 1];// 最大的牌在最后

                        if (myGrade2 > prevGrade2)
                        {
                            //存进去PromptCards
                            PromptCards.Add(tempCount, cards);
                            tempCount++;
                        }
                    }
                }
                
            }

        }
        // 上家出连对
        else if (lastCardType == DDZ_POKER_TYPE.StraightTwin)
        {
            if (mySize < prevSize)
            {
                
            }
            else
            {
                List tempMyCards = new List();
                tempMyCards = myCards;
                Hashtable myCardsHash = SortCardUseHash(tempMyCards);
                if (myCardsHash.Count < prevSize)
                {
                    Debug.Log("hash的总数小于顺子的count 肯定fales");
                   
                }
                List myCardsHashKey = new List();
                foreach (int key in myCardsHash.Keys)
                {
                    myCardsHashKey.Add(key);
                }
                myCardsHashKey.Sort();
                int tempCount = 0;
                for (int i = myCardsHashKey.Count - 1; i >= prevSize - 1; i--)
                {
                    
                    List cards = new List();
                    for (int j = 0; j < prevSize; j++)
                    {
                        cards.Add(myCardsHashKey[myCardsHashKey.Count - 1 - i + j]);
                    }
                    DDZ_POKER_TYPE myCardType = DDZ_POKER_TYPE.DdzPass;
                    bool isRule = DDZCardRule.PopEnable(cards, out myCardType);
                    if (myCardType == DDZ_POKER_TYPE.StraightSingle)
                    {
                        int myGrade2 = cards[cards.Count - 1];// 最大的牌在最后
                        int prevGrade2 = lastCards[prevSize - 1];// 最大的牌在最后

                        if (myGrade2 > prevGrade2)
                        {
                            for (int ii = 0; ii < cards.Count; ii++)
                            {
                                if ((int)myCardsHash[cards[ii]] < 2)
                                {
                                    Debug.Log("是顺子但不是双顺");
                                    return PromptCards;
                                }
                                else
                                {
                                    for (int iii = 0; iii < cards.Count; iii++) {
                                        cards.Add(cards[iii]);
                                    }
                                    //存进去PromptCards
                                    PromptCards.Add(tempCount, cards);
                                    tempCount++;
                                }
                            }
                        }
                    }
                }
            }

        }
        //上家出飞机
        else if (lastCardType == DDZ_POKER_TYPE.PlanePure)
        {
            if (mySize < prevSize)
            {
                
            }
            else
            {
                int tempCount = 0;
                for (int i = 0; i <= mySize - prevSize; i++)
                {
                    
                    List cards = new List();
                    for (int j = 0; j < prevSize; j++)
                    {
                        cards.Add(myCards[i+ j]);
                    }

                    DDZ_POKER_TYPE myCardType = DDZ_POKER_TYPE.DdzPass;
                    bool isRule = DDZCardRule.PopEnable(cards, out myCardType);
                    if (myCardType == DDZ_POKER_TYPE.PlanePure)
                    {
                        int myGrade4 = cards[4];//
                        int prevGrade4 = lastCards[4];//

                        if (myGrade4 > prevGrade4)
                        {
                            //存进去PromptCards
                            PromptCards.Add(tempCount, cards);
                            tempCount++;
                        }
                    }
                }
            }
        }
        //上家出飞机带单
        else if (lastCardType == DDZ_POKER_TYPE.PlaneWithSingle)
        {
            if (mySize < prevSize)
            {
                
            }
            else
            {
                int tempCount = 0;
                for (int i = 0; i <= mySize - prevSize; i++)
                {
                    
                    List cards = new List();
                    for (int j = 0; j < prevSize- prevSize/4; j++)
                    {
                        cards.Add(myCards[i +j]);
                    }

                    DDZ_POKER_TYPE myCardType = DDZ_POKER_TYPE.DdzPass;
                    bool isRule = DDZCardRule.PopEnable(cards, out myCardType);
                    if (myCardType == DDZ_POKER_TYPE.PlanePure)
                    {
                        int myGrade4 = cards[4];//
                        int prevGrade4 = lastCards[4];//

                        if (myGrade4 > prevGrade4)
                        {
                            int ii = 0;
                            //存进去PromptCards 然后再找一个最小的两个单
                            foreach (int key in tempMyCardHash.Keys)
                            {
                                if (int.Parse(tempMyCardHash[key].ToString()) == 1)
                                {
                                    cards.Add(key);
                                    ii++;
                                    if (ii == prevSize/4) {
                                        break;
                                    }
                                }
                            }
                            PromptCards.Add(tempCount, cards);
                            tempCount++;
                        }
                    }
                }
            }
        }

        //上家出飞机带双
        else if (lastCardType == DDZ_POKER_TYPE.PlaneWithTwin)
        {
            if (mySize < prevSize)
            {
                
            }
            else
            {
                int tempCount = 0;
                for (int i = 0; i <= mySize - prevSize; i++)
                {
                    
                    List cards = new List();
                    for (int j = 0; j < prevSize- prevSize/5; j++)
                    {
                        cards.Add(myCards[i + j]);
                    }

                    DDZ_POKER_TYPE myCardType = DDZ_POKER_TYPE.DdzPass;
                    bool isRule = DDZCardRule.PopEnable(cards, out myCardType);
                    if (myCardType == DDZ_POKER_TYPE.PlanePure)
                    {
                        int myGrade4 = cards[4];//
                        int prevGrade4 = lastCards[4];//

                        if (myGrade4 > prevGrade4)
                        {
                            List tempTwoList = new List();
                            for (int ii = 0; ii < cards.Count; ii++)
                            {
                                int tempInt = 0;
                                for (int j = 0; j < cards.Count; j++)
                                {

                                    if (cards[ii] == cards[j])
                                    {
                                        tempInt++;
                                    }

                                }
                                if (tempInt == 2)
                                {
                                    tempTwoList.Add(cards[ii]);
                                }

                            }
                            if (tempTwoList.Count / 2 < prevSize / 5)
                            {

                                
                            }
                            else
                            {
                                //存进去
                                int iii = 0;
                                //存进去PromptCards 然后再找一个最小的两个单
                                foreach (int key in tempMyCardHash.Keys)
                                {
                                    if (int.Parse(tempMyCardHash[key].ToString()) == 2)
                                    {
                                        cards.Add(key);
                                        cards.Add(key);
                                        iii++;
                                        if (iii == prevSize /5)
                                        {
                                            break;
                                        }
                                    }
                                }
                                PromptCards.Add(tempCount, cards);
                                tempCount++;
                            }
                        }
                    }
                }
            }
        }


       

        // 集中判断对方不是炸弹,我出炸弹的情况
        if (lastCardType != DDZ_POKER_TYPE.FourBomb)
        {
            
            List myCardsHashKey = new List();
            foreach (int key in tempMyCardHash.Keys)
            {
                myCardsHashKey.Add(key);
            }
            myCardsHashKey.Sort();
            for (int i = 0; i < myCardsHashKey.Count; i++)
            {
                if ( (int)tempMyCardHash[myCardsHashKey[i]] == 4)
                {
                    List tempIntList = new List();
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(myCardsHashKey[i]);
                    tempIntList.Add(myCardsHashKey[i]);
                    Debug.Log("PromptCards.Count"+PromptCards.Count);
                    PromptCards.Add(PromptCards.Count, tempIntList);
                    
                }


            }

        }
        if (mySize >= 2)
        {
            List myCardsHashKey = new List();
            foreach (int key in tempMyCardHash.Keys)
            {
                myCardsHashKey.Add(key);
            }
            if (myCardsHashKey.Contains(53) && myCardsHashKey.Contains(54)) {
                List tempIntList = new List();
                tempIntList.Add(53);
                tempIntList.Add(54);
                PromptCards.Add(PromptCards.Count, tempIntList);
            }
        }

        return PromptCards;
    }
    #endregion

//牌编号: 从1~54, 按黑红梅方的顺序,1是BLACK3, 2是BLACK4, ..., 12是BLACK1, 13是BLACK2, 14是RED4, ..., 53是小毛,54大毛



你可能感兴趣的:(unity3d)