洗牌 发牌 以及玩家拿到牌之后整理牌的实现思路

题目:洗牌 发牌 以及玩家拿到牌之后整理牌的实现思路

实现思路:

方式1:用数组实现

import java.util.Arrays;
public class demo14 {
	public static void main(String[] args) {
		
		//所有的牌面花色
		char[] flags= {'♥','♠','♦','♣'};
		
		//所有的牌面数字
		String[] numbers= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		
		//步骤1:初始化所有的牌面(排列组合)
		int index = 0;
		String[] pokers = new String[52];
		
		for (char f : flags) {
			for (String n : numbers) {
			pokers[index++] = f + n;
			}
		}
		System.out.println("52张牌面完成初始化: " + Arrays.toString(pokers));
		
		//步骤2:洗牌10次
		for (int counter = 1; counter <= 10; counter++) {
			//乱序算法
			for (int k = pokers.length - 1; k > 0; k--) {
				int randIndex = (int) (Math.random() * k );
				String temp = pokers[k];
				pokers[k] = pokers [randIndex];
				pokers[randIndex] = temp;
			}
		}
		System.out.println("52张牌10次洗牌后:" + Arrays.toString(pokers));
		
		//步骤3:发牌
		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[] pl : players) {
			System.out.println("玩家:" + Arrays.toString(pl));
			
		}
		System.out.println();
		
		//步骤4:整理牌面(牌面排序)
		String[]array = { "A","J","K","Q"};
		
		//循环:每次拿出一个玩家的所有牌,整理排序
		for (String[] pl : players) {
			//冒泡排序
			for (int i = 0, n = pl.length; i < n - 1; i++) {
				for (int k = 0; k < n - 1 - i; k++) {
					//获取相邻元素的牌面值
					// ♥10 = 10 ♥A = A ♥K =k
					String s1 = pl[k].substring(1);
					String s2 = pl[k + 1].substring(1);
					
					//将截取出的牌面值,保存至临时数组
					String[]tempArray = { s1,s2 };
					
					//遍历临时数组,将A=1,J=11,Q=12,K=13
					for (int x = 0; x < tempArray . length; x++) {
						//检查(搜索)本次的牌面值,是否是"A,3,k,Q"中的任意一个
						int searchIndex = Arrays.binarySearch(array,tempArray[x]);
						
						if ( searchIndex >= 0) {
							switch (tempArray[x ]) {
							case "A":
								tempArray[x] = "1";
								break;
							case "J":
								tempArray[x] = "11";
								break;
							case "Q":
								tempArray[x] = "12";
								break;
							case "K" :
								tempArray[x]= "13";
								break;
							}
						}
					}
		
							//tempArray = { "A","J”};
							//tempArray = { "1","11”};
					//将字符串的牌面值转换成纯数字
					int n1 = Integer.parseInt( tempArray[0]);
					int n2 = Integer.parseInt(tempArray[1]);
							
							
					//根据比较结果,交换两张牌
					if (n1 > n2) {
					String temp = pl[k];
					pl[k] = pl[k + 1];
					pl[k + 1] = temp;
					}
				}
			}
		}
		System.out.println();
		System.out.println("玩家整牌后:");
		for (String[] pl : players) {
		System.out.println("玩家:" + Arrays.toString(pl));
		}
	
	}
}
							




运行结果:

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次洗牌后:[♥6, ♣2, ♦3, ♣3, ♠2, ♥A, ♠9, ♥8, ♠K, ♦2, ♠10, ♣10, ♠6, ♥Q, ♥5, ♥4, ♥7, ♠5, ♦7, ♠Q, ♣K, ♦A, ♠3, ♣A, ♦6, ♥2, ♠J, ♥10, ♦9, ♥J, ♣7, ♠A, ♣6, ♣5, ♦5, ♣J, ♥K, ♥9, ♦Q, ♣8, ♦4, ♦8, ♠7, ♦K, ♠4, ♠8, ♣4, ♣9, ♦10, ♣Q, ♥3, ♦J]
依次发牌后: 
玩家:[♥6, ♠2, ♠K, ♠6, ♥7, ♣K, ♦6, ♦9, ♣6, ♥K, ♦4, ♠4, ♦10]
玩家:[♣2, ♥A, ♦2, ♥Q, ♠5, ♦A, ♥2, ♥J, ♣5, ♥9, ♦8, ♠8, ♣Q]
玩家:[♦3, ♠9, ♠10, ♥5, ♦7, ♠3, ♠J, ♣7, ♦5, ♦Q, ♠7, ♣4, ♥3]
玩家:[♣3, ♥8, ♣10, ♥4, ♠Q, ♣A, ♥10, ♠A, ♣J, ♣8, ♦K, ♣9, ♦J]


玩家整牌后:
玩家:[♠2, ♦4, ♠4, ♥6, ♠6, ♦6, ♣6, ♥7, ♦9, ♦10, ♠K, ♣K, ♥K]
玩家:[♥A, ♦A, ♣2, ♦2, ♥2, ♠5, ♣5, ♦8, ♠8, ♥9, ♥J, ♥Q, ♣Q]
玩家:[♦3, ♠3, ♥3, ♣4, ♥5, ♦5, ♦7, ♣7, ♠7, ♠9, ♠10, ♠J, ♦Q]
玩家:[♣A, ♠A, ♣3, ♥4, ♥8, ♣8, ♣9, ♣10, ♥10, ♣J, ♦J, ♠Q, ♦K]

方式2:用集合实现

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

public class demo15 {
	public static void main(String[] args) {
		// 所有的牌面花色
		char[] flags = {'♥','♠','♦','♣'};
		// 所有的牌面数字
		String[] numbers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		// 步聚1: 初始化所有的牌面(排列组合)
		ArrayList pokers = new ArrayList();
		for(char f : flags) {
			for(String n : numbers) {
				pokers.add(f+n);
			}
		}
		System.out.println("52张牌面完成初始化:" + pokers);
		// 步骤2: 洗牌
		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) {
					// ♠10 = "10" ♠5 = "5"
					String s1 = o1.substring(1);
					String s2 = o2.substring(1);
					String[] tempArray = { s1, s2 };
					for (int x = 0; x < tempArray.length; x++) {
						switch (tempArray[x]) {
						case"A":
							tempArray[x] ="1";
							break;
						case "J":
							tempArray[x] ="11";
							break;
						case "Q":
							tempArray[x] ="12";
							break;
						case "K":
							tempArray[x] ="13";
							break;
						}
					}
					//转换牌面数值为整数
					int n1 = Integer.parseInt(tempArray[0]);
					int n2 = Integer.parseInt(tempArray[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, ♠5, ♦10, ♠6, ♦6, ♥5, ♥3, ♥7, ♠8, ♥J, ♠2, ♥4, ♥Q, ♥10, ♥8, ♦3, ♣K, ♠7, ♥9, ♠9, ♣2, ♥2, ♠3, ♣J, ♣5, ♠10, ♠K, ♣3, ♣6, ♣Q, ♣8, ♦8, ♦5, ♣A, ♦Q, ♦A, ♣9, ♠4, ♠J, ♥A, ♦4, ♠Q, ♣7, ♠A, ♦9, ♥6, ♣4, ♦7, ♦J, ♥K, ♦2, ♣10]
[♣2, ♦4, ♣5, ♦5, ♣6, ♦6, ♠8, ♣9, ♦9, ♦J, ♥Q, ♣K, ♦K]
[♣A, ♥2, ♠4, ♠5, ♥5, ♥6, ♠7, ♠10, ♥10, ♥J, ♠Q, ♣Q, ♥K]
[♠2, ♦2, ♠3, ♥3, ♣4, ♣7, ♣8, ♥8, ♥9, ♦10, ♠J, ♦Q, ♠K]
[♠A, ♥A, ♦A, ♣3, ♦3, ♥4, ♠6, ♥7, ♦7, ♦8, ♠9, ♣10, ♣J]

 

 

 

你可能感兴趣的:(java,算法,数据结构)