麻将和牌程序(递归)

开发 后端
本文提供了一个Java编写的麻将和牌程序(递归)源代码。

以下是Java编写的麻将和牌程序:

public class MaJiang2
{
    private boolean jiang=false;//是否找到将
    public MaJiang2(){}

    //花色,判断这个花色是否被减完了(如果没有减完自然没有和)
    private int huase(int[] hua)
    {
        if (hua.length==7)
        {
            //判断字,自比较特殊,没有顺
            for (int i=0;i            {
                if (hua[i]==3||hua[i]==4)
                {
                    hua[i]=0;
                    huase(hua);
                }
                //如果字有两个,肯定是将
                if (hua[i]==2&&!jiang)
                {
                    hua[i]=0;
                    jiang=true;
                    huase(hua);
                }
            }
        }
        else
        {
            for (int i=0;i            {
                //如果没有将,先把将减出去
                if (!jiang&&hua[i]>=2)
                {
                    hua[i]=hua[i]-2;
                    jiang=true;
                    int fanhui=huase(hua);
                    //如果递归回来依旧没有减完,则把将加回去
                    if (fanhui!=0)
                    {
                        hua[i]=hua[i]+2;
                        jiang=false;
                    }
                }
                if (hua[i]!=0&&i<7&&hua[i+1]!=0&&hua[i+2]!=0)
                {
                    hua[i]--;
                    hua[i+1]--;
                    hua[i+2]--;
                    huase(hua);
                    int fanhui=huase(hua);
                    //如果递归回来依旧没有减完,减去的加回去
                    if (fanhui!=0)
                    {
                        hua[i]++;
                        hua[i+1]++;
                        hua[i+2]++;
                    }
                }
                if (hua[i]==3||hua[i]==4)
                {
                    int temp=hua[i];
                    hua[i]=0;
                    huase(hua);
                    int fanhui=huase(hua);
                    //如果递归回来依旧没有减完,减去的加回去
                    if (fanhui!=0)
                    {
                        hua[i]++;
                        hua[i]=temp;
                    }
                }
            }
        }
        int re=0;
        //最后判断减没减完
        for (int i=0;i        {
            re=re+hua[i];
        }
        return re;
    }


    public void Hu(int[] aWan,int[] aTiao,int[] aTong,int[] aZi)
    {
        //先从字开始,如果某一花色计算完之后依旧不为0,则肯定不和。如果所有的花色全部减完了,就和了,呵呵:-〉
        int jieguo=huase(aZi);
        if (jieguo!=0)
        {
            System.out.println("没和");
        }
        else if ((jieguo=huase(aWan))!=0)
        {
            System.out.println("没和");
        }
        else if ((jieguo=huase(aTiao))!=0)
        {
            System.out.println("没和");
        }
        else if ((jieguo=huase(aTong))!=0)
        {
            System.out.println("没和");
        }
        else
        {
            System.out.println("和了,真不容易,一脑门子汉");
        }
    }
    public static void main(String[] args)
    {
        MaJiang2 mj=new MaJiang2();
        int[] w={3,1,1,2,1,1,1,1,3};
        int[] ti={0,0,0,0,0,0,0,0,0};
        int[] to={0,0,0,0,0,0,0,0,0};
        int[] z={0,0,0,0,0,0,0};

        mj.Hu(w,ti,to,z);
    }
}

【编辑推荐】

  1. JavaOne 2009第三天:微软与Sun/Oracle携手并进
  2. 开发高可移植性J2ME的软件
  3. Java虚拟机(JVM)中的内存设置详解
  4. JavaFX和Java之间的互操作性
  5. JavaFX能否实现Sun的桌面端梦想
责任编辑:王观 来源: 百家编程
相关推荐

2019-03-13 09:51:56

AI人工智能打麻将

2020-10-14 08:32:08

算法递归面试

2020-04-03 09:05:43

麻将 AI Suphx神经网络

2018-12-24 10:04:32

区块链数据存储分布式记账

2020-05-27 07:38:36

尾递归优化递归函数

2021-06-02 07:57:48

内存管理

2019-09-16 08:32:59

递归算法编程

2020-07-10 08:15:19

递归算法函数

2022-03-31 08:15:59

递归代码非递归

2019-09-18 10:12:37

递归数据结构

2009-11-17 16:53:24

PHP递归算法

2010-04-02 15:04:14

Oracle递归查询

2023-10-24 09:30:49

Java内存

2013-11-11 10:03:29

递归路由

2018-03-05 08:52:57

2015-07-08 16:06:15

iOS递归锁

2020-01-17 06:02:46

技术开发AI

2021-09-15 07:40:50

二叉树数据结构算法

2023-07-11 13:07:53

中国AI麻将

2009-11-18 16:47:50

PHP递归算法
点赞
收藏

51CTO技术栈公众号