`
hueng512
  • 浏览: 27859 次
  • 性别: Icon_minigender_1
  • 来自: 打工子哥
社区版块
存档分类
最新评论

斗地主出牌算法

阅读更多
根据斗地主出牌规则.对玩家出的牌进行检验.判断是否符合出牌规则.

(关于斗地主的出牌规则网上有很多)

思路:将玩家的牌按升序排序.然后将牌进行拆分,分存在4个数组中.拆分规则如下:

假设有牌:333\444\555\789

则拆分后数组中的数据如下

arr[0]:345789

arr[1]:345

arr[2]:345

arr[3]:null

可以看出拆分规则是:如果遇到相同数字的牌则存到下一个数组的末尾.

拆分完后可以根据各数组的存储情况判定玩家出牌的类型,上面例子arr[3]为空.可以排除掉4带1(2).炸.的情况根据arr[2]为顺子且个数大于1,且arr[2]中存放的牌的张数乘以3刚好等于arr[0]的张数+arr[1]的张数.则可以判定是三带一的飞机.其他类型的牌也有相似的规律.以下是该算法的核心源代码.本算法用C#编写.

using System;
using System.Collections.Generic;
using System.Text;

namespace LordLibrary
{

     /*

      *以下程序版权由林奕霖所有,有兴趣的朋友可以用来交流和探讨.但请别用于商业用途.否则后果自负.

      *您可以自由拷贝修改此源代码,但必须保留此注释.

      */

    public class CheckType
    {

        private static int[][] DiffRow(int[] nums)
        {
            int[][] list = new int[4][];
            for (int i = 0; i < list.Length; i++)
            {
                list[i] = new int[20];
            }
            int[] rowIndex = new int[4];
            int columIndex = 0;

            for (int i = 0; i < nums.Length; i++)
            {
                if (i + 1 < nums.Length)
                {
                    if (nums[i] != 0)
                    {
                        list[columIndex][rowIndex[columIndex]] = nums[i];
                        rowIndex[columIndex]++;
                    }

                    if (nums[i] == nums[i + 1])
                    {
                        columIndex++;
                    }
                    else
                    {
                        columIndex = 0;
                    }
                }
                else if (nums[i] != 0)
                    list[columIndex][rowIndex[columIndex]] = nums[i];
            }
            return list;
        }


        private static int checkListCount(int[][] list, int rowIndex, int compStart, int rowMaxCount)
        {

/* LIST 代表单顺.

*DOUB 代表双顺.

*FEI0 代表三顺.

*FEI1 代表三带一的飞机

*FEI2 代表三带二的飞机

*FOR1 代表四带1

*FOR2 代表四带2

*ROCK 代表大小王

*/
            int listCount = 1;
            for (int i = compStart; i < rowMaxCount - 1; i++)
            {
                if (list[rowIndex][i] + 1 == list[rowIndex][i + 1])
                    listCount++;
                else
                    listCount = 1;
            }
            return listCount;
        }
        public static string getCardType(int[] nums)
        {
            int[][] list = DiffRow(nums);
            int[] counts = new int[4];
            for (int k = 0; k < 4; k++)
            { 
              counts[k] = Array.IndexOf(list[k], 0);
            }
            int MaxValue = 0;
            int listCount = 0;
            string type = string.Empty;
            //当第4行牌的数量为1的时候   
            #region
            if (counts[3] == 1)
            {
                int index = Array.IndexOf(list[2], list[3][0]);
                switch (counts[2])
                {
                    case 1:
                        MaxValue = list[3][0];
                        if (counts[0] == 1)
                        {
                            type = "BOMB:4:" + MaxValue;
                        }
                        else if (counts[0] + counts[1] == 4)
                        {
                            type = "FOR1:6:" + MaxValue;
                        }
                        else if (counts[0] == counts[1] && counts[0] == 3)
                        {
                            type = "FOR2:8:" + MaxValue;
                        }

                        break;
                    case 2:
                        if (list[2][0] + 1 == list[2][1] && counts[1] == counts[2] && counts[0] == 3)
                        {
                            MaxValue = list[2][counts[2] - 1];
                            type = "FEI1:" + counts[2] + ":" + MaxValue;
                        } break;
                    case 3:
                        if (checkListCount(list, 2, 0, counts[2]) == counts[2] && counts[0] + counts[1] + counts[3] == 3 * counts[2])
                        {
                            MaxValue = list[2][counts[2] - 1];
                            type = "FEI1:" + counts[2] + ":" + MaxValue;
                        }
                        else if (Array.IndexOf(list[2], list[3][0]) == 0 && counts[0] == counts[2])
                        {
                            if ((listCount = checkListCount(list, 2, 1, counts[2])) == counts[2] - 1)
                            {
                                MaxValue = list[2][counts[2] - 1];
                                type = "FEI2:" + listCount + ":" + MaxValue;
                            }

                        }
                        else if (Array.IndexOf(list[2], list[3][0]) == counts[2] - 1 && counts[0] == counts[2])
                        {
                            if ((listCount = checkListCount(list, 2, 0, counts[2] - 1)) == counts[2] - 1)
                            {
                                MaxValue = list[2][counts[2] - 2];
                                type = "FEI2:" + listCount + ":" + MaxValue;
                            }
                        }
                        break;
                    case 4:
                        if (index == 0 && counts[0] == counts[1] && counts[0] == 5)
                        {
                            if ((listCount = checkListCount(list, 2, 1, counts[2])) == counts[2] - 1)
                            {
                                MaxValue = list[2][counts[2] - 1];
                                type = "FEI2:" + listCount + ":" + MaxValue;
                            }
                        }
                        else if (index == counts[2] - 1 && counts[0] == counts[1] && counts[0] == 5)
                        {
                            if ((listCount = checkListCount(list, 2, 0, counts[2] - 1)) == counts[2] - 1)
                            {
                                MaxValue = list[2][counts[2] - 2];
                                type = "FEI2:" + listCount + ":" + MaxValue;
                            }
                        }
                        else if ((listCount = checkListCount(list, 2, 0, counts[2])) == counts[2] && counts[0] + counts[1] + counts[3] == 3 * counts[2])
                        {
                            MaxValue = list[2][counts[2] - 1];
                            type = "FEI1:" + listCount + ":" + MaxValue;
                        }
                        break;
                    case 5:
                        if (index == 0)
                        {
                            if ((listCount = checkListCount(list, 2, 0, counts[2])) == counts[2] && counts[0] + counts[1] + counts[3] == 3 * counts[2])
                            {
                                MaxValue = list[2][counts[2] - 1];
                                type = "FEI1:" + listCount + ":" + MaxValue;
                            }
                            else if (listCount == counts[2] - 1 && counts[0] == counts[1])
                            {
                                if (counts[0] + 1 == 2 * (counts[2] - 1))
                                {
                                    MaxValue = list[2][counts[2] - 1];
                                    type = "FEI2:" + listCount + ":" + MaxValue;
                                }
                                else if (2 * (counts[0] + 1) == 3 * (counts[2] - 1))
                                {
                                    MaxValue = list[2][counts[2] - 1];
                                    type = "FEI1:" + listCount + ":" + MaxValue;
                                }
                            }
                        }
                        else if (index == counts[2] - 1)
                        {
                            if ((listCount = checkListCount(list, 2, 0, counts[2])) == counts[2] && counts[0] + counts[1] + counts[3] == 3 * counts[2])
                            {
                                MaxValue = list[2][counts[2] - 1];
                                type = "FEI1:" + listCount + ":" + MaxValue;
                            }
                            else if ((listCount = checkListCount(list, 2, 0, counts[2] - 1)) == counts[2] - 1 && counts[0] == counts[1])
                            {
                                if (counts[0] + 1 == 2 * (counts[2] - 1))
                                {
                                    MaxValue = list[2][counts[2] - 2];
                                    type = "FEI2:" + listCount + ":" + MaxValue;
                                }
                                else if (2 * (counts[0] + 1) == 3 * (counts[2] - 1))
                                {
                                    MaxValue = list[2][counts[2] - 2];
                                    type = "FEI1:" + listCount + ":" + MaxValue;
                                }
                            }
                        }
                        else
                        {
                            if ((listCount = checkListCount(list, 2, 0, counts[2])) == counts[2] && counts[0] + counts[1] + counts[3] == 3 * counts[2])
                            {
                                MaxValue = list[2][counts[2] - 1];
                                type = "FEI1:" + listCount + ":" + MaxValue;
                            }
                        }
                        break;
                    case 6:
                        if ((listCount = checkListCount(list, 2, 0, counts[2])) == counts[2] && counts[0] + counts[1] + counts[3] == 3 * counts[2])
                        {
                            MaxValue = list[2][counts[2] - 1];
                            type = "FEI1:" + listCount + ":" + MaxValue;
                        }
                        else if (index == 0 && listCount == counts[2] - 1 && counts[0] + counts[1] + 2 == 3 * (counts[2] - 1))
                        {
                            MaxValue = list[2][counts[2] - 1];
                            type = "FEI1:" + listCount + ":" + MaxValue;
                        }
                        else if (index == counts[2] - 1 && (listCount = checkListCount(list, 2, 0, counts[2] - 1)) == counts[2] - 1 && counts[0] + counts[1] + 2 == 3 * (counts[2] - 1))
                        {
                            MaxValue = list[2][counts[2] - 2];
                            type = "FEI1:" + listCount + ":" + MaxValue;
                        }
                        break;
                }
            }
            #endregion
            //当第4行牌的数量为2的时候
            #region
            if (counts[3] == 2)
            {
                switch (counts[2])
                {

                    default:
                        if (counts[2] >= 2 && counts[2] < 6)
                        {
                            if ((listCount = checkListCount(list, 2, 0, counts[2])) == counts[2] && counts[0] + counts[1] + counts[3] == 3 * counts[2])
                            {
                                MaxValue = list[2][counts[2] - 1];
                                type = "FEI1:" + listCount + ":" + MaxValue;
                            }
                        }
                        break;
                    case 6:
                        int firstIndex = Array.IndexOf(list[2], list[3][0]);
                        int secIndex = Array.IndexOf(list[2], list[3][1]);

                        if (secIndex == 1)
                        {
                            if ((listCount = checkListCount(list, 2, 2, counts[2])) == counts[2] - 2 && counts[0] == counts[1] && counts[0] + 2 == 2 * (counts[2] - 2))
                            {
                                MaxValue = list[2][counts[2] - 1];
                                type = "FEI2:" + listCount + ":" + MaxValue;
                            }
                        }
                        else if (secIndex == counts[2] - 1)
                        {
                            if (firstIndex == 0)
                            {
                                if ((listCount = checkListCount(list, 2, 1, counts[2] - 1)) == counts[2] - 2 && counts[0] == counts[1] && counts[0] + 2 == 2 * (counts[2] - 2))
                                {
                                    MaxValue = list[2][counts[2] - 2];
                                    type = "FEI2:" + listCount + ":" + MaxValue;
                                }
                            }
                            else if (firstIndex == secIndex - 1)
                            {
                                if ((listCount = checkListCount(list, 2, 0, counts[2] - 2)) == counts[2] - 2 && counts[0] == counts[1] && counts[0] + 2 == 2 * (counts[2] - 2))
                                {
                                    MaxValue = list[2][counts[2] - 3];
                                    type = "FEI2:" + listCount + ":" + MaxValue;
                                }
                            }
                        }
                        if ((listCount = checkListCount(list, 2, 1, counts[2])) == counts[2] - 1 && 2 * counts[0] + 3 == 3 * (counts[2] - 1))
                        {
                            MaxValue = list[2][counts[2] - 1];
                            type = "FEI1:" + listCount + ":" + MaxValue;
                        }
                        else if ((listCount = checkListCount(list, 2, 0, counts[2] - 1)) == counts[2] - 1 && 2 * counts[0] + 3 == 3 * (counts[2] - 1))
                        {
                            MaxValue = list[2][counts[2] - 2];
                            type = "FEI1:" + listCount + ":" + MaxValue;
                        }


                        break;
                }
            }
            #endregion
            //当第4行牌的数量大于2的时候
            #region
            if (counts[3] > 2)
            {
                if ((listCount = checkListCount(list, 2, 0, counts[2])) == counts[2] && counts[0] + counts[1] + counts[3] == 3 * counts[2])
                {
                    MaxValue = list[2][counts[2] - 1];
                    type = "FEI1:" + listCount + ":" + MaxValue;
                }
            }
            #endregion
            //当第4行牌的数量为0,第三行牌的数量大于0
            #region
            if (counts[3] == 0 && counts[2] > 0)
            {

                if ((listCount = checkListCount(list, 2, 0, counts[2])) == counts[2])
                {
                    if (counts[0] == counts[2])
                    {
                        MaxValue = list[2][counts[2] - 1];
                        type = "FEI0:" + listCount + ":" + MaxValue;
                    }
                    else if (counts[0] + counts[1] == 3 * counts[2])
                    {

                        MaxValue = list[2][counts[2] - 1];
                        type = "FEI1:" + listCount + ":" + MaxValue;
                    }
                    else if (counts[0] + counts[1] == 4 * counts[2] && counts[0] == counts[1])
                    {
                        MaxValue = list[2][counts[2] - 1];
                        type = "FEI2:" + listCount + ":" + MaxValue;
                    }
                }
                if ((listCount = checkListCount(list, 2, 1, counts[2])) == counts[2] - 1 && counts[0] + counts[1] + 1 == 3 * (counts[2] - 1))
                {
                    MaxValue = list[2][counts[2] - 1];
                    type = "FEI1:" + listCount + ":" + MaxValue;
                }
                else if ((listCount = checkListCount(list, 2, 0, counts[2] - 1)) == counts[2] - 1 && counts[0] + counts[1] + 1 == 3 * (counts[2] - 1))
                {
                    MaxValue = list[2][counts[2] - 2];
                    type = "FEI1:" + listCount + ":" + MaxValue;
                }
            }
            #endregion
            //当第3行牌的数量为0,第二行牌的数量大于0
            #region
            if (counts[2] == 0 && counts[1] > 0)
            {
                if (counts[0] == 1)
                {
                    MaxValue = list[1][counts[1] - 1];
                    listCount = counts[1];
                    type = "DOUB:" + listCount + ":" + MaxValue;
                }
                else
                {
                    if (counts[1] > 2 && counts[0] == counts[1] && (listCount = checkListCount(list, 1, 0, counts[1])) == counts[1])
                    {
                        MaxValue = list[1][counts[1] - 1];
                        type = "DOUB:" + listCount + ":" + MaxValue;
                    }
                }
            }
            #endregion
            //当第2行牌的数量为0
            #region
            if (counts[1] == 0)
            {
                if (counts[0] == 1)
                {
                    MaxValue = list[0][counts[0] - 1];
                    listCount = counts[0];
                    type = "LIST:" + listCount + ":" + MaxValue;
                }
                else if (counts[0] == 2 && list[0][0] == 16 && list[0][1] == 17)
                {
                    type = "ROCK:2:17";
                }
                else if (counts[0] >= 5 && (listCount = checkListCount(list, 0, 0, counts[0])) == counts[0])
                {
                    MaxValue = list[0][counts[0] - 1];
                    listCount = counts[0];
                    type = "LIST:" + listCount + ":" + MaxValue;
                }
            }
            #endregion
            String[] cmd= type.Split(new char[]{':'});
            int big = Array.IndexOf(nums, 16);
            int small = Array.IndexOf(nums, 17);
            if (cmd.Length > 0 && cmd[0]!=string.Empty)
            {
                if ((cmd[0]=="LIST" || cmd[0]=="DOUB" || cmd[0]=="FEI0" || cmd[0]=="FEI1"|| cmd[0]=="FEI2")&& (int.Parse(cmd[1]))>1)
                {
                    type = int.Parse(cmd[2]) > 14 ? string.Empty : type;
                }
                else if (cmd[0] == "FOR1" || cmd[0] == "FOR2")
                {
                    type = (big >0 && small>0) ? string.Empty : type;
                }
            }
            return type;
        }
    }
}

分享到:
评论

相关推荐

    斗地主出牌算法(java版)

    【斗地主出牌算法(Java版)】是基于Java JDK1.6开发的一款核心算法,主要用于网页Flash版的斗地主游戏客户端。斗地主是一款广受欢迎的三人扑克游戏,其策略性和趣味性深受玩家喜爱。算法设计的目的是为了模拟玩家在...

    斗地主出牌算法(as版)

    本算法采用ActionScript3.0动态脚本语言编写,是网页FLASH版斗地主客户端的核心程序!

    斗地主出牌算法(cs版)

    本算法采用C#(C Sharp)语言编写,是网页FLASH版斗地主客户端的核心程序!

    斗地主 出牌大小判断算法 (无AI出牌功能)

    用遍历的思想实现的算法,只实现了判断出牌是否符合规则,没有实现电脑的AI智能出牌(试过用hash队列实现,但颇有点复杂,写不下去了),再有也没有做全面的测试,希望有兴趣的朋友们如果测出了其中的bug请务必通知...

    Lua脚本的斗地主的提示出牌算法。

    Lua脚本的斗地主的提示出牌算法。 包括: 查找某牌型的算法:查找所有的单牌,查找所有的对子,查找所有的三条,查找所有的单顺,双顺,飞机,飞机带单,飞机带双。 排序算法。 转成花色,转成点数, 比较算法等。

    斗地主ai机器人出牌叫分算法cpp源码

    自己开发的简单的斗地主陪练机器人出牌算法,c++代码,无需修改,直接使用。 使用方法: https://blog.csdn.net/hhpp526/article/details/103159283 解压密码:hhpp526@hotmail.com

    斗地主牌型相关算法

    lua实现斗地主中的牌型判断,牌的比较,出牌提示的算法

    斗地主出牌权限算法.rar

    《斗地主出牌权限算法解析》 斗地主,作为中国最受欢迎的扑克游戏之一,其玩法多样,策略丰富。其中,出牌权限算法是游戏逻辑的核心部分,它决定了玩家在何时何地可以进行何种操作,直接影响了游戏的公平性和趣味性...

    C# 斗地主发牌算法

    本篇将详细探讨如何使用C#语言实现斗地主的发牌算法,旨在为初学者提供指导,并期待经验丰富的开发者提出宝贵的建议。 首先,我们要理解斗地主的基本规则。斗地主是一款三人对战的游戏,需要用到一副完整的扑克牌...

    斗地主出牌演示代码!!

    在这个场景中,我们关注的是"斗地主出牌演示代码",这意味着我们将探讨如何用编程语言实现斗地主的游戏逻辑。 在编程中,斗地主出牌演示代码可能会涉及以下几个核心知识点: 1. **数据结构**:首先,我们需要一个...

    斗地主AI拆牌核心逻辑源码

    斗地主AI拆牌核心逻辑源码,TS版本,后续更新出牌逻辑,主要算法按手数然后权重对比每组牌型优劣,选出手数最少或者权重最大的牌型,可以调用testMain测试。

    斗地主(求爱版)源码

    斗地主出牌算法 根据斗地主出牌规则.对玩家出的牌进行检验.判断是否符合出牌规则. (关于斗地主的出牌规则网上有很多) 思路:将玩家的牌按升序排序.然后将牌进行拆分,分存在4个数组中.拆分规则如下: 假设有牌:333...

    斗地主残局算法python

    本项目"斗地主残局算法python"专注于实现一个Python程序,能够接受地主和农民的牌型输入,并自动计算出地主的最佳出牌策略。 首先,我们要理解斗地主的牌型。牌型包括单张、对子、三张、三带一、三带二、顺子、飞机...

    C#编写--斗地主源码.rar

    斗地主出牌算法 根据斗地主出牌规则.对玩家出的牌进行检验.判断是否符合出牌规则. (关于斗地主的出牌规则网上有很多) 思路:将玩家的牌按升序排序.然后将牌进行拆分,分存在4个数组中.拆分规则如下: 假设有牌:333...

    斗地主ai机器人算法分析

    在斗地主游戏中,AI机器人的设计需要涵盖手牌分析、叫地主决策、出牌策略以及出牌后的局势分析等多个关键环节。 首先,**手牌分析**是AI机器人基础功能之一。它需要能够理解和评估一副手牌的价值,这通常通过计算...

    C#斗地主算法(绝对精华)

    本文将详细介绍一个用C#编写的斗地主出牌检验算法,该算法能够根据斗地主的出牌规则来判断玩家所出的牌是否合法。 #### 二、出牌规则 在斗地主游戏中,有多种不同的出牌方式,包括但不限于单张、对子、顺子、连对、...

    斗地主AI 算法

    AI算法必须理解这些基本规则,包括牌型(如单张、对子、顺子、三带一等)、出牌顺序、抢地主、炸弹优先级等。 在C#中,可以创建一个类来表示牌,包含牌的值和类型,然后用数组或列表来存储玩家的牌。接着,设计一个...

    斗地主算法(洗牌,发牌,出牌)flash

    出牌算法则相对复杂,涉及到游戏规则的理解和判断。出牌的逻辑主要包括以下几点: 1. 牌型判断:根据斗地主的规则,牌型包括单张、对子、三张、三带一、顺子、飞机、炸弹等。程序需要能够识别玩家出的牌型是否合法...

    C# 斗地主算法核心代码

    应编写单元测试用例,覆盖各种可能的情况,包括合法出牌、非法出牌、游戏结束等,以验证算法的正确性和健壮性。 总之,C#斗地主算法核心代码设计涵盖了数据结构、算法设计、面向对象编程、事件驱动、网络编程等多个...

    AI编程实例源码

    在“VS2010”这个文件名中,我们可以推断出这些源码可能是使用Visual Studio 2010编译器开发的。Visual Studio是微软提供的一款强大的开发环境,支持多种语言,包括C++,并提供了调试、版本控制、单元测试等功能,是...

Global site tag (gtag.js) - Google Analytics