Java集合综合练习1——扑克牌发牌

实现给三名玩家发牌操作

public static void main(String[] args) {
		String[] color = { "♥", "♠", "♣", "♦" };
		String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

		// 用于保存所有牌的LinkedHashMap 保持添加键值对的顺序
		// key:代表每个牌的数字
		// value:代表数字对应的牌面
		LinkedHashMap<Integer, String> pokerMap = new LinkedHashMap<Integer, String>();

		// 创建一个用来保存所有扑克牌值的数字集合
		ArrayList<Integer> pokerValueList = new ArrayList<Integer>();

		// 1. 准备扑克牌 初始化集合对象
		for (int i = 0; i < 52; i++) {
			// 添加扑克牌值
			pokerValueList.add(i);
			// 计算扑克牌面 计算花色
			int colorIndex = i % 4;
			// System.out.println(color[colorIndex]);

			// 计算扑克牌的值
			int pokerIndex = i / 4;
			//System.out.println(pokerIndex);
			
			//用字符串接受花色与数值
			String poker = color[colorIndex] + numbers[pokerIndex];
			//添加到扑克牌的Map
			pokerMap.put(i,poker);
		}

		System.out.println("准备扑克牌");
		System.out.println("扑克牌值:"+pokerValueList);
		System.out.println("扑克牌面:"+pokerMap);
	
		//2.洗牌
		System.out.println("洗牌");
		Collections.shuffle(pokerValueList);
		System.out.println(pokerValueList);
	
		//3.发牌
		//创建每个玩家的扑克牌值的集合  treeset 不允许重复 避免玩家拿到两张同一张 的牌
		// 可以自动排序
		TreeSet<Integer> play1 = new TreeSet<Integer>();
		TreeSet<Integer> play2 = new TreeSet<Integer>();
		TreeSet<Integer> play3 = new TreeSet<Integer>();
		TreeSet<Integer> root = new TreeSet<Integer>();
		
		//将集合最后四张牌放入底牌集合中
		List<Integer> lastpoker = pokerValueList.subList(pokerValueList.size()-4, pokerValueList.size());
		
		//添加到底牌集合中去
		root.addAll(lastpoker);
		
		//给玩家依次发牌
		for (int i = 0; i < pokerValueList.size()-4; i++) {
			//计算玩家序号
			int index = i % 3;
			
			//获取扑克牌值
			int pokerValue = pokerValueList.get(i);
			if (index == 0) {
				//给玩家一发牌
				play1.add(pokerValue);
			}else if (index == 1) {
				//给玩家二发牌
				
				play2.add(pokerValue);
				
			}else if(index == 2){
				//给玩家三发牌
				play3.add(pokerValue);
			}
			
		}
		
		System.out.println("玩家1:"+play1);
		System.out.println("玩家2:"+play2);
		System.out.println("玩家3:"+play3);
		System.out.println("底牌:"+root);
		
		//4.处理牌值 变成扑克牌对应的值
		StringBuilder play1sb = new StringBuilder();
		StringBuilder play2sb = new StringBuilder();
		StringBuilder play3sb = new StringBuilder();
		StringBuilder rootsb = new StringBuilder();
		
		//除去底牌剩余每人16张牌进行遍历转换
		for (int i = 0; i < 16; i++) {
			//treeset特有的pollfirst 每次取出栈顶元素
			//map集合getkey 方法
			
			play1sb.append(pokerMap.get(play1.pollFirst())+" " ) ;
			play2sb.append(pokerMap.get(play2.pollFirst())+" " ) ;
			play3sb.append(pokerMap.get(play3.pollFirst())+" " ) ;
			rootsb.append(pokerMap.get(root.pollFirst())+" " ) ;
			
			
		}
		System.out.println("玩家1:"+play1sb);
		System.out.println("玩家2:"+play2sb);
		System.out.println("玩家3:"+play3sb);
		System.out.println("底牌:"+rootsb.toString().replaceAll("null",""));

		
	}

运行结果如下

准备扑克牌
扑克牌值:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51]
扑克牌面:{0=♥A, 1=♠A, 2=♣A, 3=♦A, 4=2, 5=2, 6=2, 7=2, 8=3, 9=3, 10=3, 11=3, 12=4, 13=4, 14=4, 15=4, 16=5, 17=5, 18=5, 19=5, 20=6, 21=6, 22=6, 23=6, 24=7, 25=7, 26=7, 27=7, 28=8, 29=8, 30=8, 31=8, 32=9, 33=9, 34=9, 35=9, 36=10, 37=10, 38=10, 39=10, 40=♥J, 41=♠J, 42=♣J, 43=♦J, 44=♥Q, 45=♠Q, 46=♣Q, 47=♦Q, 48=♥K, 49=♠K, 50=♣K, 51=♦K}
洗牌
[13, 36, 47, 24, 26, 12, 49, 23, 25, 45, 42, 0, 51, 9, 29, 34, 30, 28, 40, 10, 37, 46, 21, 44, 6, 32, 5, 4, 41, 16, 50, 14, 33, 11, 43, 15, 1, 27, 18, 8, 17, 31, 19, 22, 2, 7, 20, 3, 35, 38, 48, 39]
玩家1[1, 4, 6, 7, 8, 11, 13, 19, 24, 34, 40, 45, 46, 49, 50, 51]
玩家2[9, 10, 14, 17, 20, 21, 22, 23, 26, 27, 30, 32, 36, 41, 42, 43]
玩家3[0, 2, 3, 5, 12, 15, 16, 18, 25, 28, 29, 31, 33, 37, 44, 47]
底牌:[35, 38, 39, 48]
玩家1:♠A ♥222334579 ♥J ♠Q ♣Q ♠K ♣K ♦K 
玩家2:♠33456666778910 ♠J ♣J ♦J 
玩家3:♥A ♣A ♦A ♠244557888910 ♥Q ♦Q 
底牌:♦91010 ♥K             

你可能感兴趣的:(Java集合综合练习1——扑克牌发牌)