关于棋牌游戏的福建十三水的一个工具类(牌型分析、大小对比、自动摆牌、获取所有牌型组合)
写的比较简陋,仅仅只是实现了而已,谈不上美观,大神们不要喷我
直接上图(个人研究使用,不允许用于不正当途径)
1、自动摆牌效果图
2、获取葫芦所有牌型组合效果图
3、判断是否包含顺子效果图
4、代码
/**
* 自动摆牌
* @param arr
* @return
*/
public static ThirteenWater automatic(List<Poker> arr){
ThirteenWater thirteenWater = new ThirteenWater();
List<Poker> copylist = copyPokerList(arr);
setPier(copylist,thirteenWater,1);
setPier(copylist,thirteenWater,2);
setPier(copylist,thirteenWater,3);
if(copylist.size() != 0){
List<Poker> tail = thirteenWater.getTail();
List<Poker> among = thirteenWater.getAmong();
List<Poker> head = thirteenWater.getHead();
if(tail.size() < 5){
for (int i = 0; i < copylist.size(); i++) {
if(tail.size() == 5){
break;
}
tail.add(copylist.get(i));
copylist.remove(copylist.get(i));
i--;
}
}
if(among.size() < 5){
for (int i = copylist.size()-1; i > -1; i--) {
if(among.size() == 5){
break;
}
among.add(copylist.get(i));
copylist.remove(copylist.get(i));
}
}
if(head.size() < 3){
for (int i = copylist.size()-1; i > -1; i--) {
if(head.size() == 3){
break;
}
head.add(copylist.get(i));
copylist.remove(copylist.get(i));
}
}
}
return thirteenWater;
}
/***
*
* @param arr
* @param thirteenWater
* @param addType 1尾 2中 3头
*/
private static void setPier(List<Poker> arr,ThirteenWater thirteenWater,int addType){
List<List<Poker>> pokers = new ArrayList<List<Poker>>();
int type = 0;
int index = 0;
//同花顺
if(pokers == null || pokers.size() < 1){
pokers = getStraightFlush(arr);
type = 1;
}
//铁支
if(pokers == null || pokers.size() < 1){
pokers = getBranch(arr);
type = 2;
}
//葫芦
if(pokers == null || pokers.size() < 1){
pokers = getGourd(arr);
type = 3;
}
//同花
if(pokers == null || pokers.size() < 1){
pokers = getSameColor(arr);
type = 4;
}
//顺子
if(pokers == null || pokers.size() < 1){
pokers = getStraight(arr);
type = 5;
}
//三条
if(pokers == null || pokers.size() < 1){
pokers = getThree(arr);
type = 6;
}
//两对
if(pokers == null || pokers.size() < 1){
pokers = getTwoPairs(arr);
type = 7;
}
//对子
if(pokers == null || pokers.size() < 1){
pokers = getPairs(arr);
type = 8;
}
if(pokers.size() > 1){
switch (type) {
case 1://同花顺
for (int i = 0; i < pokers.size(); i++) {
if(ThirteenWaterSizeComparison.containStraight(pokers.get(i),pokers.get(index))){
index = i;
}
}
break;
case 2://铁支
for (int i = 0; i < pokers.size(); i++) {
if(ThirteenWaterSizeComparison.containIronBranch(pokers.get(i),pokers.get(index))){
index = i;
}
}
break;
case 3://葫芦
for (int i = 0; i < pokers.size(); i++) {
if(ThirteenWaterSizeComparison.containGourd(pokers.get(i),pokers.get(index))){
index = i;
}
}
break;
case 4://同花
for (int i = 0; i < pokers.size(); i++) {
if(ThirteenWaterSizeComparison.containSameColor(pokers.get(i),pokers.get(index))){
index = i;
}
}
break;
case 5://顺子
for (int i = 0; i < pokers.size(); i++) {
if(ThirteenWaterSizeComparison.containStraight(pokers.get(i),pokers.get(index))){
index = i;
}
}
break;
case 6://三条
for (int i = 0; i < pokers.size(); i++) {
if(ThirteenWaterSizeComparison.containThree(pokers.get(i),pokers.get(index))){
index = i;
}
}
break;
case 7://两对
for (int i = 0; i < pokers.size(); i++) {
if(ThirteenWaterSizeComparison.containTwoPairs(pokers.get(i),pokers.get(index))){
index = i;
}
}
break;
case 8://对子
for (int i = 0; i < pokers.size(); i++) {
if(ThirteenWaterSizeComparison.containPair(pokers.get(i),pokers.get(index))){
index = i;
}
}
break;
}
}
if(pokers != null && pokers.size() > 0){
for (int j = 0; j < pokers.get(index).size(); j++) {
Poker pokerB = pokers.get(index).get(j);
for (int i = 0; i < arr.size(); i++) {
Poker pokerA = arr.get(i);
if(pokerA.getDeck() == pokerB.getDeck() && pokerA.getDecor() == pokerB.getDecor()){
arr.remove(pokerA);
i--;
}
}
}
}
if(pokers != null && pokers.size() > 0){
if(addType == 1){
thirteenWater.setTail(pokers.get(index));
}
if(addType == 2){
thirteenWater.setAmong(pokers.get(index));
}
if(addType == 3){
thirteenWater.setHead(pokers.get(index));
}
}else{
if(addType == 1){
thirteenWater.setTail(new ArrayList<Poker>());
}
if(addType == 2){
thirteenWater.setAmong(new ArrayList<Poker>());
}
if(addType == 3){
thirteenWater.setHead(new ArrayList<Poker>());
}
}
}
/**
* 获取所有对子组合
* @param arr
* @return
*/
public static List<List<Poker>> getPairs(List<Poker> arr){
List<List<Poker>> result = new ArrayList<List<Poker>>();
for (int i = 0; i < arr.size(); i++) {
int a = arr.get(i).getDeck();
for (int j = 0; j < arr.size(); j++) {
int b = arr.get(j).getDeck();;
if(i == j){
continue;
}
if(a == b){
List<Poker> aa = new ArrayList<Poker>();
aa.add(arr.get(i));
aa.add(arr.get(j));
result.add(aa);
break;
}
}
}
return result;
}
/**
* 获取所有两对组合
* @param arr
* @return
*/
public static List<List<Poker>> getTwoPairs(List<Poker> arr){
List<List<Poker>> result = new ArrayList<List<Poker>>();
for (int i = 0; i < arr.size(); i++) {
int a = arr.get(i).getDeck();
for (int j = 0; j < arr.size(); j++) {
int b = arr.get(j).getDeck();
if(i == j){
continue;
}
if(a == b){
for (int k = 0; k < arr.size(); k++) {
int c = arr.get(k).getDeck();
if(j == k || k == i){
continue;
}
if(b != c && a != c){
for (int l = 0; l < arr.size(); l++) {
int d = arr.get(l).getDeck();
if(k == l || l == j || l==i){
continue;
}
if(c == d){
List<Poker> list = new ArrayList<Poker>();
list.add(arr.get(i));
list.add(arr.get(j));
list.add(arr.get(k));
list.add(arr.get(l));
boolean contain = false;
for (int m = 0; m < result.size(); m++) {
if(result.get(m).get(1).getDeck()+result.get(m).get(2).getDeck() == b+c){
contain = true;
}
}
if(!contain){
result.add(list);
}
}
}
}
}
}
}
}
// for (int i = 0; i < result.size(); i++) {
// System.out.println(result.get(i));
// }
return result;
}
/**
* 获取所有三条组合
* @param arr
* @return
*/
public static List<List<Poker>> getThree(List<Poker> arr){
List<List<Poker>> result = new ArrayList<List<Poker>>();
Map<Integer, List<Poker>> notRepeatingMap = new HashMap<>();
for (int i = 0; i < arr.size(); i++) {
int a = arr.get(i).getDeck();
for (int j = 0; j < arr.size(); j++) {
int b = arr.get(j).getDeck();
if(i == j){
continue;
}
if(a == b){
for (int h = 0; h < arr.size(); h++) {
int c = arr.get(h).getDeck();
if(j == h || h == i){
continue;
}
if(b == c){
List<Poker> list = new ArrayList<Poker>();
list.add(arr.get(i));
list.add(arr.get(j));
list.add(arr.get(h));
notRepeatingMap.put(a, list);
}
}
}
}
}
for(Entry<Integer, List<Poker>> entry:notRepeatingMap.entrySet()){
result.add(entry.getValue());
}
return result;
}
/**
* 获取所有的顺子组合
* @param arr
* @return
*/
public static List<List<Poker>> getStraight(List<Poker> arr){
List<List<Poker>> result = new ArrayList<List<Poker>>();
Map<Integer,Poker> notRepeatingMap = new HashMap<Integer, Poker>();
List<Poker> list = new ArrayList<Poker>();
List<Poker> copylist = copyPokerList(arr);
//A==13,可以组成12345 & 10JQKA,方便计算添加一个A=0
for (int j = 0; j < arr.size(); j++) {
if(arr.get(j).getDeck() == 13){
Poker poker = new Poker();
poker.setDeck(0);
poker.setDecor(arr.get(j).getDecor());
copylist.add(poker);
}
}
//排序
sort(copylist);
for (int i = 0; i < copylist.size(); i++) {
int poker = copylist.get(i).getDeck();
int upPoker = -999;
if(notRepeatingMap.size() > 0){
upPoker = (int)notRepeatingMap.keySet().toArray()[notRepeatingMap.keySet().toArray().length-1];
}
if(Math.abs(poker-upPoker) == 1 || notRepeatingMap.size() < 1 || Math.abs(poker-upPoker) == 0){
notRepeatingMap.put(poker, copylist.get(i));
}else{
if(notRepeatingMap.size() > 0){
if(Math.abs(upPoker-poker) != 1 && notRepeatingMap.size() < 5){
notRepeatingMap.clear();
notRepeatingMap.put(poker, copylist.get(i));
}
if(Math.abs(upPoker-poker) != 1 && notRepeatingMap.size() > 5){
for(Entry<Integer, Poker> entry:notRepeatingMap.entrySet()){
list.add(entry.getValue());
}
notRepeatingMap.clear();
notRepeatingMap.put(poker, copylist.get(i));
}
}
}
}
for(Entry<Integer, Poker> entry:notRepeatingMap.entrySet()){
list.add(entry.getValue());
}
notRepeatingMap.clear();
sort(list);
for (int i = 0; i < list.size(); i++) {
List<Poker> aa = new ArrayList<Poker>();
for (int j = 0; j < 5; j++) {
if(i+j == list.size()){
break;
}
aa.add(list.get(i+j));
}
if(containStraight(aa)){
result.add(aa);
}
}
for (int i = 0; i < result.size(); i++) {
for (int j = 0; j < result.get(i).size(); j++) {
if(result.get(i).get(j).getDeck() == 0){
result.get(i).get(j).setDeck(13);
}
}
}
return result;
}
/**
* 获取所有的同花组合
* @param arr
* @return
*/
public static List<List<Poker>> getSameColor(List<Poker> arr){
List<List<Poker>> result = new ArrayList<List<Poker>>();
List<Poker> black = new ArrayList<>();
List<Poker> red = new ArrayList<>();
List<Poker> flower = new ArrayList<>();
List<Poker> square = new ArrayList<>();
for (int i = 1; i < 5; i++) {
for (int j = 0; j < arr.size(); j++) {
if(i==1 && i == arr.get(j).getDecor()){
black.add(arr.get(j));
}
if(i==2 && i == arr.get(j).getDecor()){
red.add(arr.get(j));
}
if(i==3 && i == arr.get(j).getDecor()){
flower.add(arr.get(j));
}
if(i==4 && i == arr.get(j).getDecor()){
square.add(arr.get(j));
}
}
}
if(black.size() > 5){
for (int i = 0; i < black.size(); i++) {
List<Poker> aa = new ArrayList<Poker>();
for (int j = 0; j < 5; j++) {
if(i+j == black.size()){
break;
}
aa.add(black.get(i+j));
}
if(aa.size() >= 5){
result.add(aa);
}
}
}else if(black.size() == 5){
result.add(black);
}
if(red.size() > 5){
for (int i = 0; i < red.size(); i++) {
List<Poker> aa = new ArrayList<Poker>();
for (int j = 0; j < 5; j++) {
if(i+j == red.size()){
break;
}
aa.add(red.get(i+j));
}
if(aa.size() >= 5){
result.add(aa);
} }
}else if(red.size() == 5){
result.add(red);
}
if(flower.size() > 5){
for (int i = 0; i < flower.size(); i++) {
List<Poker> aa = new ArrayList<Poker>();
for (int j = 0; j < 5; j++) {
if(i+j == flower.size()){
break;
}
aa.add(flower.get(i+j));
}
if(aa.size() >= 5){
result.add(aa);
} }
}else if(flower.size() == 5){
result.add(flower);
}
if(square.size() > 5){
for (int i = 0; i < square.size(); i++) {
List<Poker> aa = new ArrayList<Poker>();
for (int j = 0; j < 5; j++) {
if(i+j == square.size()){
break;
}
aa.add(square.get(i+j));
}
if(aa.size() >= 5){
result.add(aa);
} }
}else if(square.size() == 5){
result.add(square);
}
// for (int i = 0; i < result.size(); i++) {
// System.out.println(result.get(i));
//
// }
return result;
}
/**
* 获取所有的葫芦组合
* @param arr
* @return
*/
public static List<List<Poker>> getGourd(List<Poker> arr){
List<List<Poker>> result = new ArrayList<List<Poker>>();
List<List<Poker>> pair = getPairs(arr);
List<List<Poker>> three = getThree(arr);
if(pair.size() > 0 && three.size() > 0){
if(pair.size()+three.size() > 2){
for (int i = 0; i < pair.size(); i++) {
for (int j = 0; j < three.size(); j++) {
if(three.get(j).get(0).getDeck() != pair.get(i).get(0).getDeck()){
boolean b = false;
for (int j2 = 0; j2 < result.size(); j2++) {
if((three.get(j).get(2).getDeck() + pair.get(i).get(0).getDeck()) == result.get(j2).get(2).getDeck()+result.get(j2).get(3).getDeck()){
b = true;
break;
}
}
if(!b){
List<Poker> list = new ArrayList<Poker>();
list.add(three.get(j).get(0));
list.add(three.get(j).get(1));
list.add(three.get(j).get(2));
list.add(pair.get(i).get(0));
list.add(pair.get(i).get(1));
result.add(list);
}
}
}
}
}
}
return result;
}
/**
* 获取所有的铁支组合
* @param arr
* @return
*/
public static List<List<Poker>> getBranch(List<Poker> arr){
List<List<Poker>> result = new ArrayList<List<Poker>>();
Map<Integer, List<Poker>> map = new HashMap<Integer, List<Poker>>();
for (int i = 0; i < arr.size(); i++) {
int a = arr.get(i).getDeck();
for (int j = 0; j < arr.size(); j++) {
int b = arr.get(j).getDeck();
if(i == j){
continue;
}
if(a == b){
for (int h = 0; h < arr.size(); h++) {
int c = arr.get(h).getDeck();
if(j == h || h == i){
continue;
}
if(b == c){
for (int u = 0; u < arr.size(); u++) {
int d = arr.get(u).getDeck();
if(j == u || u == i || u == h){
continue;
}
if(d == c){
List<Poker> list = new ArrayList<Poker>();
list.add(arr.get(i));
list.add(arr.get(j));
list.add(arr.get(h));
list.add(arr.get(u));
map.put(a, list);
break;
}
}
}
}
}
}
}
for(Entry<Integer, List<Poker>> entry:map.entrySet()){
result.add(entry.getValue());
}
return result;
}
/**
* 获取所有同花顺组合
* @param arr
* @return
*/
public static List<List<Poker>> getStraightFlush(List<Poker> arr){
List<List<Poker>> result = new ArrayList<List<Poker>>();
List<Poker> temporaryRecord = new ArrayList<Poker>();
List<Poker> list = new ArrayList<Poker>();
List<Poker> copylist = copyPokerList(arr);
List<Poker> black = new ArrayList<Poker>();
List<Poker> red = new ArrayList<Poker>();
List<Poker> plum = new ArrayList<Poker>();
List<Poker> cube = new ArrayList<Poker>();
for (int j = 0; j < arr.size(); j++) {
if(arr.get(j).getDeck() == 13){
Poker poker = new Poker();
poker.setDeck(0);
poker.setDecor(arr.get(j).getDecor());
copylist.add(poker);
}
}
sort(copylist);
for (int i = 0; i < copylist.size(); i++) {
int a = copylist.get(i).getDeck();
int h = -999;
if(list.size() > 0){
h = list.get(list.size()-1).getDeck();
}
if(Math.abs(a-h) == 1 || list.size() < 1 || Math.abs(a-h) == 0){
list.add(copylist.get(i));
}else{
if(list.size() > 0){
if(Math.abs(h-a) != 1 && list.size() < 5){
list.clear();
list.add(copylist.get(i));
}
if(Math.abs(h-a) != 1 && list.size() > 5){
for (Poker poker : list) {
temporaryRecord.add(poker);
}
list.clear();
list.add(copylist.get(i));
}
}
}
}
for (Poker poker : temporaryRecord) {
list.add(poker);
}
sort(list);
for (int i = 1; i < 5; i++) {
for (int k = 0; k < list.size(); k++) {
if(i == 1 && i == list.get(k).getDecor()){
black.add(list.get(k));
}
if(i == 2 && i == list.get(k).getDecor()){
red.add(list.get(k));
}
if(i == 3 && i == list.get(k).getDecor()){
plum.add(list.get(k));
}
if(i == 4 && i == list.get(k).getDecor()){
cube.add(list.get(k));
}
}
}
for (int i = 0; i < black.size(); i++) {
List<Poker> uu = new ArrayList<Poker>();
for (int j = 0; j < 5; j++) {
if(i+j == black.size()){
break;
}
uu.add(black.get(i+j));
}
if(containStraight(uu)){
result.add(uu);
}
}
for (int i = 0; i < red.size(); i++) {
List<Poker> uu = new ArrayList<Poker>();
for (int j = 0; j < 5; j++) {
if(i+j == red.size()){
break;
}
uu.add(red.get(i+j));
}
if(containStraight(uu)){
result.add(uu);
}
}
for (int i = 0; i < plum.size(); i++) {
List<Poker> uu = new ArrayList<Poker>();
for (int j = 0; j < 5; j++) {
if(i+j == plum.size()){
break;
}
uu.add(plum.get(i+j));
}
if(containStraight(uu)){
result.add(uu);
}
}
for (int i = 0; i < cube.size(); i++) {
List<Poker> uu = new ArrayList<Poker>();
for (int j = 0; j < 5; j++) {
if(i+j == cube.size()){
break;
}
uu.add(cube.get(i+j));
}
if(containStraight(uu)){
result.add(uu);
}
}
for (int i = 0; i < result.size(); i++) {
for (int j = 0; j < result.get(i).size(); j++) {
if(result.get(i).get(j).getDeck() == 0){
result.get(i).get(j).setDeck(13);
}
}
}
return result;
}