Home > Java > javaTutorial > body text

How to implement mahjong and card programs in java

WBOY
Release: 2023-04-17 22:16:01
forward
1348 people have browsed it

The following is a mahjong and tiles program written in Java:

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);
}
}
Copy after login

The above is the detailed content of How to implement mahjong and card programs in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template