package com.jim.mj;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
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) {
//11112222333344
MJCard 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());//隐藏牌编号10
MJCard num2 = MJCards.getOneCard(indexTemp.getNum() + 2, indexTemp.getType());//隐藏牌编号10 11
if(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
RayJimHeaven
- 粉丝: 6
- 资源: 3
最新资源
- 此存储库适用于 Linkedin Learning 课程学习 Java.zip
- (源码)基于STM32和AD9850的无线电信标系统.zip
- (源码)基于Android的新闻推荐系统.zip
- 本资源库是关于“Java Collection Framework API”的参考资料,是 Java 开发社区的重要贡献,旨在提供有关 Java 语言学院 API 的实践示例和递归教育关系 .zip
- 插件: e2eFood.dll
- 打造最强的Java安全研究与安全开发面试题库,帮助师傅们找到满意的工作.zip
- (源码)基于Spark的实时用户行为分析系统.zip
- (源码)基于Spring Boot和Vue的个人博客后台管理系统.zip
- 将流行的 ruby faker gem 引入 Java.zip
- (源码)基于C#和ArcGIS Engine的房屋管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页