最近在写跑胡子这个项目的时候,遇到了吃的情况,自己想了一种算法,我的小字是0-9代表一到十,大字是10-19代表壹到拾。
吃的情况 大约有五种 比如小7,有可能是789,678,567,77大7,大7大7小7,2710。大概就是这六种情况。我的思路就是能放在左边的1-8。意思就是123,234 345……8910。这几种情况。如果是9或者10就不能左边。同理 能放中间的就是2-9,能放右边的就是3-10。具体代码
int qumoPai = -1;
//这里取模判断区间
if(targetPai>9){
qumoPai=targetPai%9-1;
}else{
qumoPai=targetPai%9;
}
//这里的map是自己手上的牌
//是否需要跟牌
boolean isgen = false;
ArrayList chipais = new ArrayList<>();
Integer integer = map.get(targetPai);
if (integer==1){
isgen = true;
}
//能放在左边的就是1-8
if (qumoPai>=0&&qumoPai<8){
Integer right1 = map.get(targetPai+1);
Integer right2 = map.get(targetPai+2);
if (right1!=null&&right2!=null&&right1<3&&right2<3){
if (right1>0&&right2>0){
String chipai =targetPai+1+","+(targetPai+2);
chipais.add(chipai);
}
}
}
//能放在中间的就是2-9
if (qumoPai>=1&&qumoPai<9){
Integer left1 = map.get(targetPai-1);
Integer right1 = map.get(targetPai+1);
if (left1!=null&&right1!=null&&left1<3&&right1<3){
if (left1>0&&right1>0){
String chipai =(targetPai-1)+","+(targetPai+1);
chipais.add(chipai);
//System.out.println((targetPai-1)+","+targetPai+","+(targetPai+1));
}
}
}
//能放在最右边的就是3-10
if (qumoPai>=2&&qumoPai<10){
Integer left1 = map.get(targetPai-1);
Integer left2 = map.get(targetPai-2);
if (left1!=null&&left2!=null){
if (left1>0&&left2>0){
String chipai =(targetPai-2)+","+(targetPai-1);
chipais.add(chipai);
// System.out.println((targetPai-2)+","+(targetPai-1)+","+targetPai);
}
}
}
这样就判断了三种情况。然后二七十
单独抽出来,写成一个方法
public String chi_erqishi(int targetPai,Map maps){
if (targetPai==1){
Integer qi = maps.get(6);
Integer shi = maps.get(9);
boolean isnullqidayuling = isnullqidayuling(qi, shi);
if (!isnullqidayuling){
return "6,9";
}
}
if (targetPai==6){
Integer er = maps.get(1);
Integer shi = maps.get(9);
boolean isnullqidayuling = isnullqidayuling(er, shi);
if (!isnullqidayuling){
return "1,9";
}
}
if (targetPai==9){
Integer er = maps.get(1);
Integer qi = maps.get(6);
boolean isnullqidayuling = isnullqidayuling(er, qi);
if (!isnullqidayuling){
return "1,6";
}
}
if (targetPai==11){
Integer taiqi = maps.get(16);
Integer taishi = maps.get(19);
boolean isnullqidayuling = isnullqidayuling(taiqi, taishi);
if (!isnullqidayuling){
return "16,19";
}
}
if (targetPai==16){
Integer taiqi = maps.get(11);
Integer taishi = maps.get(19);
boolean isnullqidayuling = isnullqidayuling(taiqi, taishi);
if (!isnullqidayuling){
return "11,19";
}
}
if (targetPai==19){
Integer taiqi = maps.get(16);
Integer taishi = maps.get(11);
boolean isnullqidayuling = isnullqidayuling(taiqi, taishi);
if (!isnullqidayuling){
return "11,16";
}
}
return null;
}
然后就是两小一大,也抽也来写成一个方法,我觉得这样增加代码,减少算法复杂度也可以。
public String chi_yidaliangxiao(int targetPai,Map maps){
if (targetPai>10){
Integer xiaopai = maps.get(targetPai-10);
boolean isnullqidayuling = isnullqidayuling(xiaopai);
if (!isnullqidayuling&&xiaopai==2){
return (targetPai-10)+","+(targetPai-10);
}
}
if (targetPai<10){
Integer xiaopai = maps.get(targetPai);
Integer dapai = maps.get(targetPai+10);
boolean isnullqidayuling = isnullqidayuling(xiaopai , dapai);
if (!isnullqidayuling&&dapai<3&&xiaopai==1){
return targetPai+","+(targetPai+10);
}
}
return null;
}
然后两大一小
/*两大一小*/
public String chi_lingdayixiao(int targetPai,Map maps){
if (targetPai<10){
Integer pai = maps.get(10+targetPai);
boolean isnullqidayuling = isnullqidayuling(pai);
if (!isnullqidayuling&&pai==2){
return (targetPai+10)+","+(targetPai+10);
}
}
if (targetPai>10){
Integer xiaopai = maps.get(targetPai-10);
Integer dapai = maps.get(targetPai);
boolean isnullqidayuling = isnullqidayuling(xiaopai , dapai);
if (!isnullqidayuling&&xiaopai<2&&dapai==1){
return targetPai+","+(targetPai-10);
}
}
return null;
}
这样几种情况就算完成。
写的不好之处,还忘大佬指点。
欢迎大家进行技术讨论qq群:780731516