洗牌发牌并整理(1.数组 2.集合)

问题描述:

对52张牌进行洗10次后,依次发牌给4个玩家,再对每个玩家的牌进行整理。

第一种方式:数组实现

public static void main(String[] args) {
		
		//所有牌面花色
		char[] colors = {'红','黑','梅','方'};
		//所有牌面数字
		String[] numbers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

		//步骤1:初始化所有牌面(排列组合)
		int index = 0;
		//牌数
		String[] pokers = new String[colors.length*numbers.length];
		
		for(int i =0;i < colors.length;i++) {
			for(int k = 0;k < numbers.length;k++) {
//				System.out.print(colors[i] + numbers[k]);
				pokers[index] = colors[i] + numbers[k];
				index++;
			}
//			for(char f : colors) {
//				for(String n :numbers) {
//					pokers[index ++] = f + n ;
//				}
//			}
			
		}
		System.out.println("52张牌面完成初始化:"+Arrays.toString(pokers));
		//步骤2:洗牌10次
		//洗牌次数
		for(int counter = 0;counter < 10;counter++) {
			for(int n = pokers.length-1;n > 0;n--) {
				int randIndex = (int)(Math.random()*n);
				String temp = pokers[randIndex];
				pokers[randIndex] = pokers[n];
				pokers[n] = temp;
			}
			counter++;
		}
		
		System.out.println("52张牌面完成10次洗牌后:"+Arrays.toString(pokers));
		//步骤3:发牌
		//(4个人每人13张牌)
		String[][] players = new String[4][13];
		for(int i = 0,n = 0;i < players[0].length;i++) {
			for(int k = 0;k < players.length;k++) {
				players[k][i] = pokers[n++];
			}
		}
		System.out.println("依次发牌后:");
		for(String[] p1 : players) {
			System.out.println("玩家"+Arrays.toString(p1));
		}
		System.out.println();
		//步骤4:整理牌(牌面排序)
		String[] array = {"A","J","K","Q"};//按照ACII码
		//循环:每次拿出一个玩家的所有牌,整理排序
		for(String[] pai : players) {
			//冒泡
			for(int i = 0, n = pai.length; i < n - 1; i++) {
				for(int k = 0; k < n - 1 - i; k++) {
					//获取相邻元素的牌面值
					String s1 = pai[k].substring(1);
					String s2 = pai[k + 1].substring(1);
					//将截取出的牌面值,保存至临时数组
					String[] temporary = {s1,s2};
					//遍历临时数组,将A=1,J=11,Q=12,K=13
					for(int x = 0; x < temporary.length; x++) {
						//检查本次的牌面值,是否为“A,J,Q,K”中的任意一个
						int searchIndex = Arrays.binarySearch(array, temporary[x]);
						if(searchIndex >= 0) {
							switch(temporary[x]) {
							case "A":
								temporary[x] = "1";
								break;
							case "J":
								temporary[x] = "11";
								break;
							case "Q":
								temporary[x] = "12";
								break;
							case "K":
								temporary[x] = "13";
								break;
							}
						}
					}
					
					//将字符串的牌面值转换成纯数字
					int n1 = Integer.parseInt(temporary[0]);
					int n2 = Integer.parseInt(temporary[1]);
					
					//根据比较结果,交换两张牌
					if(n1 > n2) {
						String ret = pai[k];
						pai[k] = pai[k+1];
						pai[k+1] = ret;
					}
				}
			}
		}
		System.out.println("玩家整牌后:");
		for(String[] pai : players) {
			System.out.println("玩家:" + Arrays.toString(pai));
		}
	}

第二种方式:集合实现

public static void main(String[] args) {
		//所有的牌面花色
				char[] flowers = {'♣','♥','♦','♠'};
				//所有的牌面数字
				String[] numbers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
				
				//步骤1:初始化所有的牌面(排列组合)
				ArrayList pokers = new ArrayList();
				for(char f : flowers) {
					for(String n : numbers) {
						pokers.add(f + n);
					}
				}
				System.out.println("52张牌面完成初始化:"+ pokers);
				
				//步骤2:洗牌10次
				for(int i = 0; i < 10; i++) {
					Collections.shuffle(pokers);
				}
				System.out.println("52张牌完成10次洗牌后:"+ pokers);
				
				//步骤3:发牌
				ArrayList> playerList = new ArrayList>();
				for(int i = 0; i < 4; i++) {
					//玩家x:
					playerList.add(new TreeSet(new Comparator() {
						@Override
						public int compare(String o1, String o2) {
							String s1 = o1.substring(1);
							String s2 = o2.substring(1);
							String[] temporary = {s1,s2};
							for(int x = 0; x < temporary.length; x++) {
								switch(temporary[x]) {
								case "A":
									temporary[x] = "1";
									break;
								case "J":
									temporary[x] = "11";
									break;
								case "Q":
									temporary[x] = "12";
									break;
								case "K":
									temporary[x] = "13";
									break;
								}
							}
							//将字符串的牌面值转换成纯数字
							int n1 = Integer.parseInt(temporary[0]);
							int n2 = Integer.parseInt(temporary[1]);
							
							//如果牌面数值相等,则比较花色+牌面数值
							if(n1 == n2) {
								return o1.compareTo(o2);
							}
							//比较牌面数值
							return n1 - n2;
						}
					}));
				}
				
				for(int i = 0; i < pokers.size(); i++) {
					//玩家依次发牌
					playerList.get(i%4).add(pokers.get(i));
				}
				
				//整牌
				for(TreeSet player : playerList) {
					System.out.println(player);
				}
	}

运行结果: 

52张牌面完成初始化:[♣A, ♣2, ♣3, ♣4, ♣5, ♣6, ♣7, ♣8, ♣9, ♣10, ♣J, ♣Q, ♣K, ♥A, ♥2, ♥3, ♥4, ♥5, ♥6, ♥7, ♥8, ♥9, ♥10, ♥J, ♥Q, ♥K, ♦A, ♦2, ♦3, ♦4, ♦5, ♦6, ♦7, ♦8, ♦9, ♦10, ♦J, ♦Q, ♦K, ♠A, ♠2, ♠3, ♠4, ♠5, ♠6, ♠7, ♠8, ♠9, ♠10, ♠J, ♠Q, ♠K]
52张牌完成10次洗牌后:[♥K, ♦Q, ♥5, ♥7, ♠2, ♦2, ♦8, ♣K, ♠J, ♠4, ♣9, ♦J, ♣8, ♠Q, ♥3, ♦3, ♠3, ♥4, ♠5, ♥A, ♣3, ♥9, ♣A, ♠6, ♣5, ♥J, ♥2, ♠K, ♠7, ♦4, ♦7, ♣10, ♣7, ♦5, ♣6, ♥10, ♠9, ♠8, ♠A, ♣2, ♦10, ♥8, ♣Q, ♦A, ♦K, ♥Q, ♣J, ♠10, ♦9, ♣4, ♦6, ♥6]
[♠2, ♠3, ♣3, ♣5, ♠7, ♣7, ♣8, ♠9, ♦9, ♦10, ♠J, ♥K, ♦K]
[♦2, ♠4, ♣4, ♥4, ♦4, ♦5, ♠8, ♥8, ♥9, ♥J, ♠Q, ♥Q, ♦Q]
[♠A, ♣A, ♥2, ♥3, ♠5, ♥5, ♣6, ♦6, ♦7, ♦8, ♣9, ♣J, ♣Q]
[♥A, ♦A, ♣2, ♦3, ♠6, ♥6, ♥7, ♠10, ♣10, ♥10, ♦J, ♠K, ♣K]

你可能感兴趣的:(java小白之旅,java,算法,数据结构,eclipse,开发语言)