分享一个麻将胡牌算法,支持多癞子,自己对麻将胡牌的理解写的一套快速识别胡牌逻辑,核心逻辑500行代码,仅对同条万进行处理,字花牌不包含在内,易理解,1M次随机胡牌牌型大概3秒左右。原创分享,我的算法也许可以给你带来一种思路,也许你有更好的,仅供学习参考。如果有什么逻辑错误,欢迎指出。源码:https://download.csdn.net/download/jimheaven/12147500

基础类

public class MJCard {public static final int Tong = 0;public static final int Tiao = 1;public static final int Wan = 2;public static final int Feng = 3;public static final int Jian = 4;public static final int Hua = 5;public static final int Laizi = 6;@Getterprivate int type = 0;@Getterprivate int num = 0;public MJCard(int type,int num) {this.type = type;this.num = num;}public int getNumValue() {return type * 10 + num;}public String toString() {String info = "";if(type == Tiao) {info +="条"+num;}else if(type == Tong) {info += "同"+num;}else if(type == Wan) {info += "万"+num;}else if(type == Feng) {if(num == 1) {info = "东";}else if(num == 2) {info = "南";}else if(num == 3) {info = "西";}else if(num == 4) {info = "北";}}else if(type == Jian) {if(num == 1) {info = "红中";}else if(num == 2) {info = "发财";}else if(num == 3) {info = "白板";}}else if(type == Hua) {if(num == 1) {info = "春";}else if(num == 2) {info = "夏";}else if(num == 3) {info = "秋";}else if(num == 4) {info = "冬";}else if(num == 5) {info = "梅";}else if(num == 6) {info = "兰";}else if(num == 7) {info = "竹";}else if(num == 8) {info = "菊";}}else if(type == Laizi) {if(num == 1) {info = "系统癞子";}else if(num == 2) {info = "特殊癞子";}}return info;}
}
public class MJCards {private static final MJCard tong1 = new MJCard(MJCard.Tong, 1); private static final MJCard tong2 = new MJCard(MJCard.Tong, 2); private static final MJCard tong3 = new MJCard(MJCard.Tong, 3); private static final MJCard tong4 = new MJCard(MJCard.Tong, 4); private static final MJCard tong5 = new MJCard(MJCard.Tong, 5); private static final MJCard tong6 = new MJCard(MJCard.Tong, 6); private static final MJCard tong7 = new MJCard(MJCard.Tong, 7); private static final MJCard tong8 = new MJCard(MJCard.Tong, 8); private static final MJCard tong9 = new MJCard(MJCard.Tong, 9); private static final MJCard tiao1 = new MJCard(MJCard.Tiao, 1); private static final MJCard tiao2 = new MJCard(MJCard.Tiao, 2); private static final MJCard tiao3 = new MJCard(MJCard.Tiao, 3); private static final MJCard tiao4 = new MJCard(MJCard.Tiao, 4); private static final MJCard tiao5 = new MJCard(MJCard.Tiao, 5); private static final MJCard tiao6 = new MJCard(MJCard.Tiao, 6); private static final MJCard tiao7 = new MJCard(MJCard.Tiao, 7); private static final MJCard tiao8 = new MJCard(MJCard.Tiao, 8); private static final MJCard tiao9 = new MJCard(MJCard.Tiao, 9); private static final MJCard wan1 = new MJCard(MJCard.Wan, 1); private static final MJCard wan2 = new MJCard(MJCard.Wan, 2); private static final MJCard wan3 = new MJCard(MJCard.Wan, 3); private static final MJCard wan4 = new MJCard(MJCard.Wan, 4); private static final MJCard wan5 = new MJCard(MJCard.Wan, 5); private static final MJCard wan6 = new MJCard(MJCard.Wan, 6); private static final MJCard wan7 = new MJCard(MJCard.Wan, 7); private static final MJCard wan8 = new MJCard(MJCard.Wan, 8); private static final MJCard wan9 = new MJCard(MJCard.Wan, 9); private static final MJCard fengdong = new MJCard(MJCard.Feng, 1); private static final MJCard fengxi = new MJCard(MJCard.Feng, 2); private static final MJCard fengnan = new MJCard(MJCard.Feng, 3); private static final MJCard fengbei = new MJCard(MJCard.Feng, 4); private static final MJCard zhong = new MJCard(MJCard.Jian, 1); private static final MJCard fa = new MJCard(MJCard.Jian, 2); private static final MJCard bai = new MJCard(MJCard.Jian, 3); private static final MJCard chun = new MJCard(MJCard.Hua, 1); private static final MJCard xia = new MJCard(MJCard.Hua, 2); private static final MJCard qiu = new MJCard(MJCard.Hua, 3); private static final MJCard dong = new MJCard(MJCard.Hua, 4); private static final MJCard mei = new MJCard(MJCard.Hua, 5); private static final MJCard lan = new MJCard(MJCard.Hua, 6); private static final MJCard zu = new MJCard(MJCard.Hua, 7); private static final MJCard ju = new MJCard(MJCard.Hua, 8); private static final MJCard laiziSys = new MJCard(MJCard.Laizi, 1);private static final MJCard laizi = new MJCard(MJCard.Laizi, 2);/** 获取所有牌型数组 */public static MJCard[] getCardArr() {MJCard[] card = { tong1, tong2, tong3, tong4, tong5, tong6, tong7,tong8, tong9, tiao1, tiao2, tiao3, tiao4, tiao5, tiao6,tiao7, tiao8, tiao9,wan1, wan2, wan3, wan4, wan5,wan6, wan7, wan8, wan9, fengdong, fengnan, fengxi,fengbei, zhong, fa, bai, chun, xia,qiu, dong, mei, lan, zu, ju };return card;}/*** 获取指定牌型* * @param types*            牌类集合, 0=筒,1=条子,2=万,3=风,4=将,5=花* @return*/public static ArrayList<MJCard> getCardArr(Set<Integer> types) {ArrayList<MJCard> cardList = new ArrayList<MJCard>();// 放入筒子if (types.contains(MJCard.Tong)) {cardList.add(tong1);cardList.add(tong2);cardList.add(tong3);cardList.add(tong4);cardList.add(tong5);cardList.add(tong6);cardList.add(tong7);cardList.add(tong8);cardList.add(tong9);}// 放入条子if (types.contains(MJCard.Tiao)) {cardList.add(tiao1);cardList.add(tiao2);cardList.add(tiao3);cardList.add(tiao4);cardList.add(tiao5);cardList.add(tiao6);cardList.add(tiao7);cardList.add(tiao8);cardList.add(tiao9);}// 放入万if (types.contains(MJCard.Wan)) {cardList.add(wan1);cardList.add(wan2);cardList.add(wan3);cardList.add(wan4);cardList.add(wan5);cardList.add(wan6);cardList.add(wan7);cardList.add(wan8);cardList.add(wan9);}// 放入风if (types.contains(MJCard.Feng)) {cardList.add(fengdong);cardList.add(fengnan);cardList.add(fengxi);cardList.add(fengbei);}// 放入将if (types.contains(MJCard.Jian)) {cardList.add(zhong);cardList.add(fa);cardList.add(bai);}// 放入花if (types.contains(MJCard.Hua)) {cardList.add(chun);cardList.add(xia);cardList.add(qiu);cardList.add(dong);cardList.add(mei);cardList.add(lan);cardList.add(zu);cardList.add(ju);}return cardList;}public static MJCard getOneCard(int num,int type) {return getOneCard(type * 10 + num);}public static MJCard getOneCard(int number) {int num = number % 10;int type= number / 10;if(type == MJCard.Tong) {if(num == 1) {return tong1;}else if(num == 2) {return tong2;}else if(num == 3) {return tong3;}else if(num == 4) {return tong4;}else if(num == 5) {return tong5;}else if(num == 6) {return tong6;}else if(num == 7) {return tong7;}else if(num == 8) {return tong8;}else if(num == 9) {return tong9;}}else if(type == MJCard.Tiao) {if(num == 1) {return tiao1;}else if(num == 2) {return tiao2;}else if(num == 3) {return tiao3;}else if(num == 4) {return tiao4;}else if(num == 5) {return tiao5;}else if(num == 6) {return tiao6;}else if(num == 7) {return tiao7;}else if(num == 8) {return tiao8;}else if(num == 9) {return tiao9;}}else if(type == MJCard.Wan) {if(num == 1) {return wan1;}else if(num == 2) {return wan2;}else if(num == 3) {return wan3;}else if(num == 4) {return wan4;}else if(num == 5) {return wan5;}else if(num == 6) {return wan6;}else if(num == 7) {return wan7;}else if(num == 8) {return wan8;}else if(num == 9) {return wan9;}}else if(type == MJCard.Feng) {if(num == 1) {return fengdong;}else if(num == 2) {return fengnan;}else if(num == 3) {return fengxi;}else if(num == 4) {return fengbei;}}else if(type == MJCard.Jian) {if(num == 1) {return zhong;}else if(num == 2) {return fa;}else if(num == 3) {return bai;}}else if(type == MJCard.Hua) {if(num == 1) {return chun;}else if(num == 2) {return xia;}else if(num == 3) {return qiu;}else if(num == 4) {return dong;}else if(num == 5) {return mei;}else if(num == 6) {return lan;}else if(num == 7) {return zu;}else if(num == 8) {return ju;}}else if(type == MJCard.Laizi) {if(num == 1) {             return laiziSys;}else if(num == 2){return laizi;}}return null;}
}

核心算法

public class MJUtil {public static boolean checkHu(LinkedList<MJCard> cards, MJCard otherCard, List<MJCard> laizi,List<List<MJCard>> huCardGroup) {// 查将LinkedList<MJCard> temp = new LinkedList<MJCard>(cards);if (otherCard != null) {temp.add(otherCard);}soreCard(temp);List<MJCard> laiziList = new ArrayList<MJCard>();Iterator<MJCard> iterator = temp.iterator();while (iterator.hasNext()) {MJCard next = iterator.next();if (laizi.contains(next)) {iterator.remove();laiziList.add(next);}}if (temp.size() < 2) {return true;}if (temp.size() == 2) {MJCard c1 = temp.get(0);MJCard c2 = temp.get(1);if (c1 == c2) {return true;} else {if (laiziList.size() > 0) {if(laiziList.size() == 1 && c1.getType() == c2.getType()) {if (Math.abs(c1.getNum() - c2.getNum()) < 3) {return true;}}else if(laiziList.size() >= 3){return true;}else {return false;}}}}// 检查七对if(temp.size() + laiziList.size() == 14) {           boolean qidui = true;MJCard index = null;List<MJCard> laiziListTemp = new ArrayList<MJCard>(laiziList);int count = 0;for (int i = 0; i < temp.size(); i++) {MJCard mjCard = temp.get(i);if (index == null) {index = mjCard;count = 1;} else {if (index == mjCard) {count += 1;} else {if (count % 2 != 0) {if(laiziListTemp.size() > 0) {laiziListTemp.remove(0);}else {                             qidui = false;break;}} else {index = mjCard;count = 1;}}}}if (qidui && count % 2 != 1) {return true;}}//全杠不可胡if(temp.size() == 14) {//11112222333344MJCard tmepFind = null;int tempCount = 0;List<MJCard> gangC = new ArrayList<MJCard>();for(MJCard c : temp) {if(tmepFind == null) {tmepFind = c;tempCount = 1;}else {if(tmepFind == c) {tempCount += 1;if(tempCount == 4) {gangC.add(c);}}else if(tempCount == 4) {tmepFind = c;tempCount = 1;}else {break;                   }}}if(gangC.size() == 3) {if(gangC.get(0).getType() != gangC.get(1).getType() || gangC.get(0).getType() != gangC.get(2).getType()) {                    return false;}if(gangC.get(0).getNum() + 1 != gangC.get(1).getNum() || gangC.get(1).getNum() + 1 != gangC.get(2).getNum()) {return false;}}}Set<MJCard> jiangList = new HashSet<MJCard>(temp);//幺九放最后面,牌型幺九有特殊需求LinkedList<MJCard> jiangListList = new LinkedList<MJCard>();for(MJCard c : jiangList) {if(c.getNum() % 10 == 1 || c.getNum() % 10 == 9) {jiangListList.addLast(c);}else {jiangListList.addFirst(c);}}for (MJCard c : jiangListList) {huCardGroup.clear();List<MJCard> tempLaizi = new ArrayList<MJCard>(laiziList);LinkedList<MJCard> tempCheck = new LinkedList<MJCard>(temp);tempCheck.remove(c);if (!tempCheck.contains(c)) {continue;}tempCheck.remove(c);List<MJCard> j = new ArrayList<MJCard>();j.add(c);j.add(c);huCardGroup.add(j);LinkedList<MJCard> tong = new LinkedList<MJCard>();LinkedList<MJCard> tiao = new LinkedList<MJCard>();LinkedList<MJCard> wan = new LinkedList<MJCard>();for (MJCard c1 : tempCheck) {if (c1.getType() == MJCard.Tong) {tong.add(c1);} else if (c1.getType() == MJCard.Tiao) {tiao.add(c1);} else if (c1.getType() == MJCard.Wan) {wan.add(c1);}}if (tong.size() != 0) {boolean checkCardList = checkCardListNew(tong, tempLaizi,huCardGroup);if (!checkCardList) {continue;}}if (tiao.size() != 0) {boolean checkCardList = checkCardListNew(tiao, tempLaizi,huCardGroup);if (!checkCardList) {continue;}}if (wan.size() != 0) {boolean checkCardList = checkCardListNew(wan, tempLaizi,huCardGroup);if (!checkCardList) {continue;}}if(tempLaizi.size() % 3 != 0) {continue;}return true;}//癞子去单if (laiziList.size() > 0) {for (MJCard mjCard : jiangListList) {huCardGroup.clear();int count = 0;for(MJCard c : temp) {if(c == mjCard) {count += 1;}}if(count > 1) {continue;}List<MJCard> tempLaizi = new ArrayList<MJCard>(laiziList);LinkedList<MJCard> tempCheck = new LinkedList<MJCard>(temp);tempCheck.remove(mjCard);MJCard remove = tempLaizi.remove(0);List<MJCard> j = new ArrayList<MJCard>();j.add(mjCard);j.add(remove);huCardGroup.add(j);LinkedList<MJCard> tong = new LinkedList<MJCard>();LinkedList<MJCard> tiao = new LinkedList<MJCard>();LinkedList<MJCard> wan = new LinkedList<MJCard>();for (MJCard c1 : tempCheck) {if (c1.getType() == MJCard.Tong) {tong.add(c1);} else if (c1.getType() == MJCard.Tiao) {tiao.add(c1);} else if (c1.getType() == MJCard.Wan) {wan.add(c1);}}if (tong.size() != 0) {int laiziCount = tempLaizi.size();int tongCount = tong.size();boolean checkCardList = checkCardListNew(tong, tempLaizi,huCardGroup);if (!checkCardList || (tongCount + laiziCount - tempLaizi.size()) % 3 != 0) {continue;}}if (tiao.size() != 0) {int laiziCount = tempLaizi.size();int tiaoCount = tiao.size();boolean checkCardList = checkCardListNew(tiao, tempLaizi,huCardGroup);if (!checkCardList || (tiaoCount + laiziCount - tempLaizi.size()) % 3 != 0) {continue;}}if (wan.size() != 0) {int laiziCount = tempLaizi.size();int wanCount = wan.size();boolean checkCardList = checkCardListNew(wan, tempLaizi,huCardGroup);if (!checkCardList || (wanCount + laiziCount - tempLaizi.size()) % 3 != 0) {continue;}}if(tempLaizi.size() % 3 != 0) {continue;}return true;}}return false;}/*** 公式AAA*z+ABC*y+AA<br>* 单双不过,并且在外部已经去将牌<br>* 思路<br>* 1:AAAA是必拆组合,并且后两张必须存在* * @param list* @param laiziList* @param mjCardCount* @return*/private static boolean checkCardListNew(LinkedList<MJCard> list, List<MJCard> laiziList,List<List<MJCard>> huCardGroup) {LinkedList<MJCard> left = new LinkedList<MJCard>(list);LinkedList<MJCard> laiziListleft = new LinkedList<MJCard>(laiziList);int times = 20;do {MJCard indexTemp = null;int countTemp = 0;for (int i = 0; i < left.size(); i++) {if(indexTemp == null) {indexTemp = left.get(i);countTemp += 1;}else {if(indexTemp == left.get(i)) {countTemp += 1;}else {if(countTemp == 3) {indexTemp = left.get(i);countTemp = 1;continue;}break;}}}MJCard num1 = MJCards.getOneCard(indexTemp.getNum() + 1, indexTemp.getType());//隐藏牌编号10MJCard num2 = MJCards.getOneCard(indexTemp.getNum() + 2, indexTemp.getType());//隐藏牌编号10 11if(countTemp == 1) {//只能是第一张List<MJCard> temp = new ArrayList<MJCard>();temp.add(indexTemp);left.remove(indexTemp);if(left.contains(num1)) {left.remove(num1);temp.add(num1);}else {if(laiziListleft.size() > 0) {MJCard remove = laiziListleft.remove(0);temp.add(remove);}else {return false;}}if(left.contains(num2)) {left.remove(num2);temp.add(num2);}else {if(laiziListleft.size() > 0) {MJCard remove = laiziListleft.remove(0);temp.add(remove);}else {return false;}}huCardGroup.add(temp);}else if(countTemp == 2) {//只能是前两张List<MJCard> temp1 = new ArrayList<MJCard>();List<MJCard> temp2 = new ArrayList<MJCard>();temp1.add(indexTemp);temp2.add(indexTemp);left.remove(indexTemp);left.remove(indexTemp);if(left.contains(num1) || left.contains(num2)) {//考虑一下AA赖boolean isok = false;LinkedList<MJCard> leftTemp = new LinkedList<MJCard>(left);LinkedList<MJCard> laiziListleftTemp = new LinkedList<MJCard>(laiziListleft);if(laiziListleftTemp.size() > 0) {laiziListleftTemp.remove(0);List<List<MJCard>> huCardGroupTemp = new ArrayList<List<MJCard>>();isok = checkCardListNew(leftTemp, laiziListleftTemp,huCardGroupTemp);if(isok) { MJCard remove = laiziListleft.remove(0);temp1.add(indexTemp);temp1.add(remove);temp2.clear();}}if(!isok) {                     if(left.contains(num1)) {left.remove(num1);temp1.add(num1);if(left.contains(num1)) {left.remove(num1);temp2.add(num1);}else {if(laiziListleft.size() > 0) {MJCard remove = laiziListleft.remove(0);temp2.add(remove);}else {return false;}}}else {if(laiziListleft.size() > 1) {MJCard remove = laiziListleft.remove(0);laiziListleft.remove(0);temp1.add(remove);temp2.add(remove);}else {return false;}}if(left.contains(num2)) {left.remove(num2);temp1.add(num2);if(left.contains(num2)) {left.remove(num2);temp2.add(num2);}else {if(laiziListleft.size() > 0) {MJCard remove = laiziListleft.remove(0);temp1.add(remove);}else {return false;}}}else {if(laiziListleft.size() > 1) {MJCard remove = laiziListleft.remove(0);laiziListleft.remove(0);temp1.add(remove);temp2.add(remove);}else {return false;}}}huCardGroup.add(temp1);if(!temp2.isEmpty()) {                      huCardGroup.add(temp2);}}else {if(laiziListleft.size() > 0) {MJCard remove = laiziListleft.remove(0);temp1.add(indexTemp);temp1.add(remove);temp2.clear();huCardGroup.add(temp1);}else {return false;}}}else if(countTemp == 4) {List<MJCard> temp = new ArrayList<MJCard>();temp.add(indexTemp);left.remove(indexTemp);if(left.contains(num1)) {left.remove(num1);temp.add(num1);}else {if(laiziListleft.size() > 0) {MJCard remove = laiziListleft.remove(0);temp.add(remove);}else {return false;}}if(left.contains(num2)) {left.remove(num2);temp.add(num2);}else {if(laiziListleft.size() > 0) {MJCard remove = laiziListleft.remove(0);temp.add(remove);}else {return false;}}huCardGroup.add(temp);}else {//将三张集合if(!left.isEmpty()) {for(int j=0;j<left.size() / 3 ;j++) {List<MJCard> temp = new ArrayList<MJCard>();temp.add(left.get(j));temp.add(left.get(j+1));temp.add(left.get(j+2));huCardGroup.add(temp);}}//结束laiziList.clear();laiziList.addAll(laiziListleft);return true;}times -= 1;if(left.isEmpty()) {laiziList.clear();laiziList.addAll(laiziListleft);return true;}}while(true && times > 0);if(times <= 0) {return false;}return true;}private static void soreCard(LinkedList<MJCard> cards) {Collections.sort(cards,new Comparator<MJCard>() {@Overridepublic int compare(MJCard o1, MJCard o2) {return o1.getNumValue() - o2.getNumValue();}});}
}

测试函数

public static void whileCheckHu(int count,int laiziCount) {System.out.println("开始"+System.currentTimeMillis());File file = new File("d://nohu.txt");try (FileOutputStream fos = new FileOutputStream(file);OutputStreamWriter osw = new OutputStreamWriter(fos);BufferedWriter bw = new BufferedWriter(osw);){Random r = new Random();List<MJCard> laizi = new LinkedList<MJCard>();MJCard laizi1 = MJCards.getOneCard(1, MJCard.Laizi);laizi.add(laizi1);for(int i=0;i<count;i++) {Map<Integer, Integer> cardsCount = new HashMap<Integer, Integer>();List<Integer> allCards = new ArrayList<Integer>();int jiang = r.nextInt(9) + 1;allCards.add(jiang);allCards.add(jiang);cardsCount.put(jiang, 2);int zu1 = r.nextInt(5);int zu2 = 4 - zu1;while(zu1 > 0) {int get1 = r.nextInt(9) + 1;if(r.nextBoolean()) {//砍if(get1 == 8) {if(cardsCount.containsKey(get1)) {if(cardsCount.get(get1) + 1 > 4) {continue;}}else {cardsCount.put(get1, 0);}if(cardsCount.containsKey(get1 - 1)) {if(cardsCount.get(get1 - 1) + 1 > 4) {continue;}}else {cardsCount.put(get1 - 1, 0);}if(cardsCount.containsKey(get1 + 1)) {if(cardsCount.get(get1 + 1) + 1 > 4) {continue;}}else {cardsCount.put(get1 + 1, 0);}allCards.add(get1 - 1);allCards.add(get1);allCards.add(get1 + 1);cardsCount.put(get1, cardsCount.get(get1) + 1);cardsCount.put(get1 - 1, cardsCount.get(get1 - 1) + 1);cardsCount.put(get1 + 1, cardsCount.get(get1 + 1) + 1);zu1 -= 1;}else if(get1 == 9) {if(cardsCount.containsKey(get1)) {if(cardsCount.get(get1) + 1 > 4) {continue;}}else {cardsCount.put(get1, 0);}if(cardsCount.containsKey(get1 - 1)) {if(cardsCount.get(get1 - 1) + 1 > 4) {continue;}}else {cardsCount.put(get1 - 1, 0);}if(cardsCount.containsKey(get1 - 2)) {if(cardsCount.get(get1 - 2) + 1 > 4) {continue;}}else {cardsCount.put(get1 - 2, 0);}allCards.add(get1 - 1);allCards.add(get1);allCards.add(get1 - 2);cardsCount.put(get1, cardsCount.get(get1) + 1);cardsCount.put(get1 - 1, cardsCount.get(get1 - 1) + 1);cardsCount.put(get1 - 2, cardsCount.get(get1 - 2) + 1);zu1 -= 1;}else {if(cardsCount.containsKey(get1)) {if(cardsCount.get(get1) + 1 > 4) {continue;}}else {cardsCount.put(get1, 0);}if(cardsCount.containsKey(get1 + 1)) {if(cardsCount.get(get1 + 1) + 1 > 4) {continue;}}else {cardsCount.put(get1 + 1, 0);}if(cardsCount.containsKey(get1 + 2)) {if(cardsCount.get(get1 + 2) + 1 > 4) {continue;}}else {cardsCount.put(get1 + 2, 0);}allCards.add(get1);allCards.add(get1 + 1);allCards.add(get1 + 2);cardsCount.put(get1, cardsCount.get(get1) + 1);cardsCount.put(get1 + 1, cardsCount.get(get1 + 1) + 1);cardsCount.put(get1 + 2, cardsCount.get(get1 + 2) + 1);zu1 -= 1;}}else {//碰if(cardsCount.containsKey(get1)) {if(cardsCount.get(get1) + 3 > 4) {continue;}}else {cardsCount.put(get1, 0);}allCards.add(get1);allCards.add(get1);allCards.add(get1);cardsCount.put(get1, cardsCount.get(get1) + 3);zu1 -= 1;}}while(zu2 > 0) {int get1 = r.nextInt(9) + 11;if(r.nextBoolean()) {if(get1 == 18) {if(cardsCount.containsKey(get1)) {if(cardsCount.get(get1) + 1 > 4) {continue;}}else {cardsCount.put(get1, 0);}if(cardsCount.containsKey(get1 - 1)) {if(cardsCount.get(get1 - 1) + 1 > 4) {continue;}}else {cardsCount.put(get1 - 1, 0);}if(cardsCount.containsKey(get1 + 1)) {if(cardsCount.get(get1 + 1) + 1 > 4) {continue;}}else {cardsCount.put(get1 + 1, 0);}allCards.add(get1 - 1);allCards.add(get1);allCards.add(get1 + 1);cardsCount.put(get1, cardsCount.get(get1) + 1);cardsCount.put(get1 - 1, cardsCount.get(get1 - 1) + 1);cardsCount.put(get1 + 1, cardsCount.get(get1 + 1) + 1);zu2 -= 1;}else if(get1 == 19) {if(cardsCount.containsKey(get1)) {if(cardsCount.get(get1) + 1 > 4) {continue;}}else {cardsCount.put(get1, 0);}if(cardsCount.containsKey(get1 - 1)) {if(cardsCount.get(get1 - 1) + 1 > 4) {continue;}}else {cardsCount.put(get1 - 1, 0);}if(cardsCount.containsKey(get1 - 2)) {if(cardsCount.get(get1 - 2) + 1 > 4) {continue;}}else {cardsCount.put(get1 - 2, 0);}allCards.add(get1 - 1);allCards.add(get1);allCards.add(get1 - 2);cardsCount.put(get1, cardsCount.get(get1) + 1);cardsCount.put(get1 - 1, cardsCount.get(get1 - 1) + 1);cardsCount.put(get1 - 2, cardsCount.get(get1 - 2) + 1);zu2 -= 1;}else {if(cardsCount.containsKey(get1)) {if(cardsCount.get(get1) + 1 > 4) {continue;}}else {cardsCount.put(get1, 0);}if(cardsCount.containsKey(get1 + 1)) {if(cardsCount.get(get1 + 1) + 1 > 4) {continue;}}else {cardsCount.put(get1 + 1, 0);}if(cardsCount.containsKey(get1 + 2)) {if(cardsCount.get(get1 + 2) + 1 > 4) {continue;}}else {cardsCount.put(get1 + 2, 0);}allCards.add(get1);allCards.add(get1 + 1);allCards.add(get1 + 2);cardsCount.put(get1, cardsCount.get(get1) + 1);cardsCount.put(get1 + 1, cardsCount.get(get1 + 1) + 1);cardsCount.put(get1 + 2, cardsCount.get(get1 + 2) + 1);zu2 -= 1;}}else {if(cardsCount.containsKey(get1)) {if(cardsCount.get(get1) + 3 > 4) {continue;}}else {cardsCount.put(get1, 0);}allCards.add(get1);allCards.add(get1);allCards.add(get1);cardsCount.put(get1, cardsCount.get(get1) + 3);zu2 -= 1;}}Collections.sort(allCards);LinkedList<MJCard> cards = new LinkedList<MJCard>();for(int c : allCards) {cards.add(MJCards.getOneCard(c % 10, c/ 10));}for(int j=0;j<laiziCount;j++) {cards.remove(r.nextInt(cards.size()));cards.add(laizi1);}soreCard(cards);List<List<MJCard>> huCardGroup = new ArrayList<>();boolean checkHu = MJUtil.checkHu(cards, null, laizi,huCardGroup);if(checkHu) {
//                  System.out.println(allCards+" "+checkAnGang.getType());}else {bw.write(cards.toString()+"\n");
//                  System.out.println("no"+"  "+allCards);}}} catch (Exception e) {e.printStackTrace();}System.out.println("结束"+System.currentTimeMillis());}private static void soreCard(LinkedList<MJCard> cards) {Collections.sort(cards,new Comparator<MJCard>() {@Overridepublic int compare(MJCard o1, MJCard o2) {return o1.getNumValue() - o2.getNumValue();}});}

麻将 胡牌 算法(任意癞子)相关推荐

  1. 麻将胡牌算法带癞子 python实现

    姐姐:你去帮我和闺蜜打麻将? 学霸哥哥:可是我不会打麻将呀! 姐姐:你不是学霸吗?我教你一个麻将公式,我闺蜜可是单身哟! 学霸哥哥:什么公式? 姐姐:麻将胡牌公式: AAA*M+ABC*N+BB,WM ...

  2. 可带癞子的通用麻将胡牌算法

    本文原创文章,转载注明出处,博客地址 https://segmentfault.com/u/to... 第一时间看后续精彩文章.觉得好的话,顺手分享到朋友圈吧,感谢支持. 笔者前段时间做过一款地方麻将 ...

  3. 麻将胡牌算法——C#

    这里只介绍普通的麻将胡牌算法,也就是7个对子或者 1个对子+3*N; N = 三个顺子或者三个一样的 ,其中字牌(东南西北中发白)不能算顺子. 首先对于每张牌 它有牌的的数字 1-9,牌的种类 (万条 ...

  4. 麻将胡牌算法 极速(速度接近理论极限)

    此麻将胡牌算法优点: 1.可处理多赖子牌(万能牌) 2.算法速度极快:1ms可大约计算1W+副手牌是否可胡(带赖子.0.08us左右),不带赖子的牌型更快.(最新版的算法速度感觉已很接近理论极限值) ...

  5. 麻将胡牌算法(遍历+剪枝)

    麻将胡牌算法(遍历+剪枝) 简介 麻将胡牌算法及代码 1. 方法引入 2. 类型定义 2.1 牌定义 2.2 牌特征定义 3. 计算胡牌 3.1 检测十三幺牌型 3.2 检测七小对牌型 3.3 检测普 ...

  6. 麻将胡牌算法,带癞子

    貌似去年去面试一家公司,问了麻将的算法.虽然之前做过广东麻将,但是胡牌算法在服务端,就没有在意. 现在在网上搜了一些算法试了试 = =! 麻将普通的胡牌就是刻子+顺子+将.癞子可以充当任意一张牌. 参 ...

  7. 麻将胡牌算法-癞子牌特别多(一)

    麻将内最核心的算法,每一次出牌,摸牌都需要使用到.(仅个人想法,想要更快速度,建用把算出来的结果存储下来,通过查表法来使用) 1.计算前准备 /*癞子数量*/protected byte laiNum ...

  8. 包含癞子的麻将胡牌算法

    记录一下麻将的通用胡牌算法实现,只要满足M x ABC + N x DDD + EE 即可胡牌. 在这里先分析一下最简单的胡牌思路:先找出所有可能的将牌,若除去两张将牌之外的所有牌都能成扑,则可胡牌. ...

  9. 麻将胡牌算法lua 不支持癞子

    --[[ file:game\lualib\mahjongHelper.lua desc:麻将辅助 + 胡牌 + 听牌 算法 auto:Carol Luo ]] local ipairs = ipai ...

最新文章

  1. 一次违反常规的Java大厂面试经历,你还看不懂吗?
  2. resultmap的写法_mybatis的mapper.xml中resultMap标签的使用详解
  3. ielts speaking questions
  4. 天津大学计算机科学学院夏令营,天津大学计算机科学与技术学院(专业学位)计算机技术保研夏令营...
  5. AI到底有多吃香?推荐一个斯坦福、CMU、清北学生都在上的AI课
  6. JavaScript学习总结(三)——逻辑And运算符详解
  7. Codeforces Round #715 (Div. 2) C. The Sports Festival 区间dp
  8. Anchor-free 目标检测之 ExtremeNet
  9. WebFlux响应式编程基础之 2 函数式编程 工具jclasslib bytecode viewer
  10. 原来AI也可以如此简单!教你从0到1开发开源知识问答机器人
  11. jQuery学习笔记(二)
  12. [PyTorch] jit.script 与 jit.trace
  13. vue 高德轨迹自定义两点之间的颜色_vue 还是 react?这是一个问题
  14. IOS 打包后安装崩溃,debug正常运行
  15. de4dot脱壳方法
  16. 喜欢Photoshop的来看看啊
  17. 02 编辑素材和Tilemap
  18. java处理 mysql中json类型
  19. OFD格式文件怎么转PDF格式?分享一个轻松转换小妙招
  20. 【编程原则】预则立,不预则废

热门文章

  1. MySQL之连接服务器
  2. 使用Axure RP 8制作两种导航菜单-悬浮菜单
  3. 开源WebGIS架构
  4. 一流在线课程申报表公共计算机,重庆大学SPOC教学平台
  5. 【安全知识分享】承包商入厂安全培训课件(106页).pptx(附下载)
  6. 绝缘栅型n沟道场管_技术小科普—MOS管场效应管(MOSFET)详解
  7. Storm集群安装及wordcount案例
  8. 从入门到精通,CAD板式定制家具设计全套大放送~
  9. oracle 硬解析过高,过度硬解析导致share pool latch争用
  10. 魅蓝s6 android系统版本,魅族魅蓝S6 Android 7.0 ROM刷机包 3GB RAM 全网通 官方固件