牛牛算法




--------------------扑克对象定义说明------------------------------


包路径 business.global.poker


Poker.java PokerDefine.java:
扑克编码说明


方块A,2,3---K:  
对应编码顺序 101,102,103-113
梅花A,2,3---K:	
对应编码顺序 201,202,203-213
红桃A,2,3---K:	
对应编码顺序 301,302,303-313




黑桃A,2,3---K:	
对应编码顺序 401,402,403-413
小鬼:对应编码 500大鬼:对应编码 600


----------------------------------------------------------------------
牛牛规则:
	
游戏使用54张牌,最多可6人同时游戏,每手牌发5张牌。
	
将自己手上的5张牌进行3+2的模式组合,特殊牌型无需组牌。
	
鬼牌可以代替其他任意牌


有牛:
	
如果其中3张牌数值相加是10的倍数,则剩余2张牌相加之和取个位数即为牛X。
例如:4/9/8/7/K,(4+7+9=20)(K+8=18),此牌型为牛八;


无牛:
	5张牌中任意3张之和都不能为10的倍数,则判定为无牛;牛牛
	如果在有牛的情况下,剩余2张牌相加之和也是10的倍数即为牛牛牌型,例如:4/9/7/10/Q,10+Q=20,则为牛牛;
	


特殊牛
	特殊牌型:五小牛>四炸>五花牛
	
五小牛:所有单张<5,点数总和<=10
	
四炸:有4张相同牌
	
五花牛:5张单牌均为JQK




题目牛牛算法实现:




包路径 core.game.poker_nn


NNCardUtil.java 
函数 onStart 中实现5张手牌判断牛几




需要实现4个函数


//
判断是否是五小牛
s1_FiveLittleNiu


//
判断是否是四炸
s2_BoomNiu


//
判断是否是五花牛
s3_FlowerNiu


//
public class Test {
	public static void main(String[] args) throws Exception {
		//int[] handCardIDList = {101, 201, 303, 403, 401};
		//int[] handCardIDList = {101, 201, 401, 303, 403};
		//int[] handCardIDList = {103, 600, 302, 403, 500};
		//int[] handCardIDList = {101, 201, 301, 401, 213};
		//int[] handCardIDList = {401, 301, 201, 101, 213};
		//int[] handCardIDList = {311, 111, 112, 113, 213};
		//int[] handCardIDList = {111, 311, 112, 113, 213};
		//int[] handCardIDList = {111, 311, 112, 500, 213};
		//int[] handCardIDList = {111, 311, 112, 213, 500};
		//int[] handCardIDList = {101, 208, 301, 403, 406};
		//int[] handCardIDList = {301, 403, 406, 101, 208};
		//int[] handCardIDList = {105, 208, 310, 403, 500};
		//int[] handCardIDList = {403, 105, 500, 208, 310};
		//int[] handCardIDList = {105, 208, 310, 403, 406};
		//int[] handCardIDList = {403, 105, 406, 208, 310};
		//int[] handCardIDList = {403, 310, 600, 105, 500};
		//int[] handCardIDList = {105, 600, 310, 403, 500};
		int[] handCardIDList = {110, 211, 310, 410, 500};
		System.out.println(onStart(handCardIDList));
	}
	
	public static String onStart(int[] handCardIDList) {
		boolean is_fiveLittleNiuNum = true;
		boolean is_FlowerNiuNum = true;
		int[] numList = new int[5];
		for(int i = 0; i < 5; i++) {
			int num = handCardIDList[i]%100;
			numList[i] = num;
			if(num>5){
				is_fiveLittleNiuNum = false;
			}
			if(num < 11 && num != 0){
				is_FlowerNiuNum = false;
			}
		}
		if(is_fiveLittleNiuNum) {
			if(s1_FiveLittleNiu(numList)) {
				return "五小牛";
			};
		}else if(s2_BoomNiu(numList)) {
			return "四炸";
		}else if (is_FlowerNiuNum){
			return "五花牛";
		};
		String result = treat(numList);
		return result;
	}

	//在不是五小牛、五花牛、四炸的基础上判断牛数
	private static String treat(int[] numList) {
		int[] treatNumList = new int[5];
		int less_than10_num = 0;
		int totalNum = 0;
		int kingNum = 0;//鬼牌数量
		String result = null;
		for (int i : numList){
			if(i < 10){
				if(i==0){
					kingNum +=1;
				}else{
					treatNumList[less_than10_num] = i;
					totalNum += i;
					less_than10_num += 1;
				}
			}
		}
		if(kingNum==2) {
			return "牛牛";
		}
		switch (less_than10_num) {
			case 0:
				result = "牛牛";
				break;
			case 1:
				if(kingNum == 1){
					result = "牛牛";
				}else {
					result = "牛" + treatNumList[0];
				}
				break;
			case 2:
				int case2_niu_niuNum = (treatNumList[0]+treatNumList[1])%10;
				if (case2_niu_niuNum == 0 || kingNum == 1) {
					result = "牛牛";
				}else {
					result = "牛" + case2_niu_niuNum;
				}
				break;
			case 3:
				int case3_niu_num = 0;
				if(kingNum == 1) {
					int case3_teratnum = 0;
					for(int i = 0; i < less_than10_num; i++){
						case3_teratnum = treatNumList[i]%10;
						if(case3_teratnum > case3_niu_num)
							case3_niu_num = case3_teratnum;
					}
				}else{
					for(int i = 0; i < less_than10_num-1; i++){
						for(int j = i+1; j < less_than10_num; j++){
							if((treatNumList[i] + treatNumList[j])%10 == 0){
								case3_niu_num = totalNum-treatNumList[i] - treatNumList[j];
							}
						}
					}
				}
				if (case3_niu_num > 0) {
					result = "牛" + case3_niu_num;
				}else {
					result = "没牛";
				}
				break;
			case 4:
				int case4_niu_num = 0;
				if(kingNum == 1) {
					int case4_teratnum = 0;
					for(int i = 0; i < less_than10_num-1; i++){
						for(int j = i+1; j < less_than10_num; j++){
							case4_teratnum = (treatNumList[i] + treatNumList[j])%10;
							if(case4_teratnum > case4_niu_num)
								case4_niu_num = case4_teratnum;
						}
					}
				}else {
					for(int i = 0; i < less_than10_num-1; i++){
						for(int j = i+1; j < less_than10_num; j++){
							if((treatNumList[i] + treatNumList[j])%10 == 0){
								case4_niu_num = totalNum-treatNumList[i] - treatNumList[j];
							}
						}
					}
				}
				if (case4_niu_num > 0) {
					result = "牛" + case4_niu_num;
				}else {
					result = "没牛";
				}
				break;
			default:
				int case5_niu_num = 0;
				for(int i = 0; i < less_than10_num-1; i++){
					for(int j = i+1; j < less_than10_num; j++){
						if((totalNum-treatNumList[i] - treatNumList[j])%10 == 0){
							case5_niu_num = (treatNumList[i] + treatNumList[j])%10;
						}
					}
				}
				if (case5_niu_num > 0) {
					result = "牛" + case5_niu_num;
				}else {
					result = "没牛";
				}
				break;
		}
		return result;
	}

	//判断是否五小牛
	private static boolean s1_FiveLittleNiu(int[] fiveLittleNiuList) {
		int num = 0;
		for (int i : fiveLittleNiuList){
			num += i;
			if(num > 10)
				return false;
		}
		return true;
	}
	
	//判断是否四炸()
	private static boolean s2_BoomNiu(int[] boomNiuList) {
		
		int compareNum1 = boomNiuList[0];
		int compareNum2 = boomNiuList[1];
		if(compareNum1 == compareNum2 && compareNum1 == 0){//牌一牌二都为鬼牌
			for(int i = 2; i < boomNiuList.length-1; i++) {
				for(int j = i+1; j < boomNiuList.length; j++)
					if(boomNiuList[i] == boomNiuList[j]){
						return true;
					}
			}
		}
		if(compareNum1 == compareNum2) {//判断第一张与第二张是否相同,相同继续以第一张与后续牌比较
			int num = 0;
			for(int i = 2; i < boomNiuList.length && num<2; i++) {
				if(compareNum1 != boomNiuList[i] && boomNiuList[i] != 0 ){
					num++;
				}
			}
			if(num <= 1){
				return true;
			}
		}else if (compareNum1 == boomNiuList[2]){//第一张与第二张不相同,则判断第一张与第三张是否相同
			int num = 0;
			for(int i = 3; i < boomNiuList.length; i++) {
				if(compareNum1!=boomNiuList[i] && boomNiuList[i] != 0 ){
					num++;
				}
			}
			if(boomNiuList[2] == 0){
				num -= 1;
			}
			if(num < 1){
				return true;
			}
		}else if(compareNum2 == boomNiuList[2]){//第一张与第二张、第三张不相同,则判断第二张与第三张是否相同
			int num = 0;
			for(int i = 3; i < boomNiuList.length; i++) {
				if(compareNum2!=boomNiuList[i] && boomNiuList[i] != 0 ){
					num++;
				}
			}
			if(boomNiuList[1] == 0){
				num -= 1;
			}
			if(num <= 1){
				return true;
			}
		}else if(compareNum1 == 0 || compareNum2 == 0) {
			if (boomNiuList[2] == boomNiuList[3] && boomNiuList[3] == boomNiuList[4]) {
				return true;
			}
			
		}
		return false;
		
	}
}

你可能感兴趣的:(牛牛算法)