当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

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

昨天写的那个太幼稚了,今天写的使用了递归,我用九连宝灯(麻将的一种和法)进行测试,可以通过。比昨天的精简了一些,去掉了那个实际上没有用的类。没有考虑十三幺和七小对。

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]==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<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]==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<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);
    }
}

如果有什么错误的话,请告诉我,谢谢!!!
相关内容
赞助商链接