最近在研究麻将的算法,自己手写了一份记录一下以后查阅 只实现了听牌,翻数计算还没有做处理,后期在写
麻将万1-9
条11-19
筒21-29
核心:枚举出对子,然后找出刻子,找出顺子
直接上代码:
public static void main(String[] args) {
List list = new ArrayList();
list.add(4);
list.add(4);
list.add(4);
list.add(14);
List integers = new HuPai().checkTing(list);
System.err.println(list);
System.err.println(“可以胡的牌:”+integers);
}
public List checkTing(List list){
List hu = new ArrayList();
for(int i = 1;i<30;i++){
ArrayList pai = new ArrayList(list);
pai.add(i);
List integers = removeDuizi(pai);
if(integers.size()<1){
continue;
}
for(int j = 0;j
//移除对子
ArrayList pai2 = new ArrayList(pai);
int index =0;
Iterator it = pai2.iterator();
while(it.hasNext()){
Integer next = it.next();
if (next == integers.get(j)){
it.remove();
index++;
}
if (index>1){
break;
}
}
// System.out.println(“对子被移除”+pai2);
removeKezi(pai2);
// System.out.println(“移除刻字:”+pai2);
Collections.sort(pai2);
List removeShunzi = removeShunzi(pai2);
// System.out.println(“结果”+pai2);
if(removeShunzi.size()==0){
hu.add(i);
break;
}
}
}
return hu;
}
//枚举出所有的对子
public List removeDuizi(List pai){
ArrayList integers = new ArrayList();
for(int i = 0;i< pai.size();i++){
for(int j=i+1;j
if(pai.get(i)==pai.get(j)){
boolean falg = false;
for(int k = 0;k
if (pai.get(i) ==integers.get(k) ){
falg=true;
break;
}
}
if(!falg){
integers.add(pai.get(i));
}
}
}
}
return integers;
}
public List removeKezi(List pai){
Map tmpMap = new HashMap();
Iterator it = pai.iterator();
while (it.hasNext()){
Integer next = it.next();
if(tmpMap.get(next) ==null){
tmpMap.put(next, 1);
}else{
tmpMap.put(next,tmpMap.get(next)+1 );
}
}
for(Map.Entry ent : tmpMap.entrySet()){
if(ent.getValue()>2){
int index = 0;
Iterator it2 = pai.iterator();
while (it2.hasNext()){
Integer next = it2.next();
if(ent.getKey()==next){
it2.remove();
index++;
}
if(index>2){
break;
}
}
}
}
return pai;
}
public List removeShunzi(List pai){
for(int i = 0;i
for(int j = i+1;j
for(int k = j+1; k< pai.size() ; k++){
if(pai.get(i)+1==pai.get(j) && pai.get(j)+1 ==pai.get(k)){
pai.remove(i);
pai.remove(j-1);
pai.remove(k-2);
return removeShunzi(pai);
}
}
}
}
return pai;
}