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); } } |
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!