JAVA基础——集合高级练习,模拟斗地主发牌洗牌并排序

模拟斗地主洗牌和发牌并排序

一、模拟斗地主洗牌和发牌分析
1.创建一个集合对象,将扑克牌存进去
2.洗牌
3.发牌
4.看牌

String[]num = {
    
    "A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		String[]color= {
    
    "红桃","黑桃","方片","梅花"};
		ArrayList<String> poker =new ArrayList<String>();
		
		//拼接花色和数字
		for (String s1 : color) {
    
    
			for (String s2 : num) {
    
    
				poker.add(s1.concat(s2));//	连接两个字符串
			}
		}
		poker.add("小王");
		poker.add("大王");
		Collections.shuffle(poker);
		//2.洗牌
		//System.out.println(poker);
		//3.发牌
		ArrayList<String> player1=new ArrayList<String>();
		ArrayList<String> player2=new ArrayList<String>();
		ArrayList<String> player3=new ArrayList<String>();
		ArrayList<String> playcard = new ArrayList<String>();
		for (int i = 0; i < poker.size(); i++) {
    
    
			//先存三张底牌
			if (i>=poker.size() -3) {
    
    
				playcard.add(poker.get(i));		//将三张底牌存储在底牌的集合中
			}else if (i%3 ==0) {
    
    
				player1.add(poker.get(i));
			}else if (i%3 ==1) {
    
    
				player2.add(poker.get(i));
			}else {
    
    
				player3.add(poker.get(i));
			}
		
		}
		//4.看牌
		System.out.println(player1);
		System.out.println(player2);
		System.out.println(player3);
		System.out.println(playcard);
	}

二、升级:模拟斗地主洗牌和发牌分析
分析:

  1. 我们先将所有的牌放入HashMap集合中,左边存储索引0~53,值为梅花3,红桃3,方片3,黑桃3…依次往后存储直到大王。
  2. 将牌的索引0~53放到ArrayList中,洗索引,根据索引获取值,值就相当于被洗乱了
  3. 将每个人的排放到TreeSet集合中,并对每个人的TreeSet集合进行排序
 public static void main(String[] args) {
    
    
		String[]num = {
    
    "3","4","5","6","7","8","9","10","J","Q","K","A","2"};
		String[]color= {
    
    "红桃","黑桃","方片","梅花"};
		HashMap<Integer, String> hm =new HashMap<Integer, String>();
		List<Integer> list =new ArrayList<Integer>();
		int index=0;
		//拼接扑克牌并
		for (String s1 : num) {
    
    
			for (String  s2 : color) {
    
    
				hm.put(index, s1.concat(s2));
				list.add(index);
				index++;
			}
		}
		//将小王添加到双列集合中
		hm.put(index, "小王");
		list.add(index);
		index++;
		list.add(index);
		hm.put(index,"大王");
		
		//洗牌
		Collections.shuffle(list);
		//发牌
		TreeSet<Integer>  player1 =new TreeSet<Integer>();
		TreeSet<Integer>  player2 =new TreeSet<Integer>();
		TreeSet<Integer>  player3 =new TreeSet<Integer>();
		TreeSet<Integer>  playerCard =new TreeSet<Integer>();
		
		for (int i = 0; i < list.size(); i++) {
    
    
			if (i>=list.size() -3) {
    
    
				playerCard.add(list.get(i)); 	//三张底牌存储在底牌集合中
			}else if (i%3==0) {
    
    
				player1.add(list.get(i));
			}else if (i%3==1) {
    
    
				player2.add(list.get(i));			
			}else {
    
    
				player3.add(list.get(i));
			}
		}
		
		//看牌方法
		watchPoker(hm, player1, "玩家一");
		watchPoker(hm, player2, "玩家二");
		watchPoker(hm, player3, "玩家三");
		watchPoker(hm, playerCard, "底牌");
	}
	//返回值类型void
	//参数列表HashMap,TreeSet.String name
	public static void  watchPoker(HashMap<Integer, String> hm,TreeSet<Integer> ts,String name) {
    
    
		System.out.print(name+"的牌是:");
		for (Integer i : ts) {
    
    
			System.out.print(hm.get(i)+ " ");
		}
		System.out.println();
	}

猜你喜欢

转载自blog.csdn.net/Mr_GYF/article/details/109061340
今日推荐