JAVA basics-a collection of advanced exercises, simulation of the landlord deal shuffling and sorting

Simulate Doudizhu shuffle and deal cards and sort

1. Simulate the shuffle and deal analysis of the landlord
1. Create a collection object and deposit the playing cards
2. Shuffle
3. Deal
4. Check the cards

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);
	}

2. Upgrade: Simulate the shuffle and deal analysis of the landlord.
Analysis:

  1. We first put all the cards into the HashMap set, and store the index 0~53 on the left, the values ​​are clubs 3, hearts 3, squares 3, spades 3... and then store them until the king.
  2. Put the index 0~53 of the card in the ArrayList, shuffle the index, get the value according to the index, the value is equivalent to being shuffled
  3. Arrange everyone's into the TreeSet collection, and sort everyone's TreeSet collection
 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();
	}

Guess you like

Origin blog.csdn.net/Mr_GYF/article/details/109061340