用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心Java

新的麻将和牌程序(递归)

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 17:25:59
昨天写的那个太幼稚了,今天写的使用了递归,我用九连宝灯(麻将的一种和法)进行测试,可以通过。比昨天的精简了一些,去掉了那个实际上没有用的类。没有考虑十三幺和七小对。

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

    //花色,判断这个花色是否被减完了(如果没有减完自然没有和)
    private int huase(int[] hua)
    {
        if (hua.length==7)
        {
            //判断字,自比较特殊,没有顺
            for (int i=0;i<hua.length ;i++ )
            {
                if (hua[i]==3hua[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<hua.length ;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]==3hua[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<hua.length ;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);
    }
}

如果有什么错误的话,请告诉我,谢谢!!!

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:17,281.25000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号