java集合--模拟斗地主发牌洗牌

import java.util.*;

/**
* @Date: 2020/6/17 19:53
*/
public class Test04 {
public static void main(String[] args) {
// 请编写斗地主洗牌发牌程序
HashMap pk = new HashMap<>();//存整副牌
ArrayList num = new ArrayList<>();//牌面
ArrayList color = new ArrayList<>();//存花色
Collections.addAll(num, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
Collections.addAll(color, "♠", "♥", "♣", "♦");
int id = 0;//给牌大到小编号
pk.put(id++, "大");
pk.put(id++, "小");
//将牌面和花色拼接一起加入整副牌中
for (int i = 0; i < num.size(); i++) {
for (int j = 0; j < color.size(); j++) {
pk.put(id++, color.get(j) + num.get(i));
}
}
//将排序编号提取出来便于打乱顺序
ArrayList list = new ArrayList<>();
Set i = pk.keySet();
for (Integer integer : i) {
list.add(integer);
}
Collections.shuffle(list);//洗牌
TreeSet play1 = new TreeSet<>();//玩家一
TreeSet play2 = new TreeSet<>();//玩家二
TreeSet play3 = new TreeSet<>();//玩家三
TreeSet dipai = new TreeSet<>();//底牌
//发牌
for (int a = 0; a < list.size(); a++) {
if (a >= 51) {
dipai.add(list.get(a));
} else if (a % 3 == 0) {
play1.add(list.get(a));
} else if (a % 3 == 1) {
play2.add(list.get(a));
} else {
play3.add(list.get(a));
}
}
heq("玩家一:", play1, pk);
heq("玩家二:", play2, pk);
heq("玩家三:", play3, pk);
heq("底牌:", dipai, pk);
}
private static void heq(String p, TreeSet play, HashMap pk) {
System.out.print(p + "\t");
for (Integer index : play) {
String s = pk.get(index);
System.out.print(s + "\t");
}
System.out.println();
}
}

玩家一: ♥2 ♣2 ♣A ♥K ♦K ♥Q ♠J ♥J ♦J ♥10 ♠9 ♥8 ♦8 ♠7 ♣7 ♠5 ♣5
玩家二: ♠2 ♦2 ♠K ♠Q ♣J ♣10 ♦10 ♣9 ♦9 ♠6 ♦6 ♥5 ♠4 ♥4 ♦4 ♠3 ♣3
玩家三: 大 小 ♠A ♥A ♦A ♣K ♣Q ♦Q ♥9 ♠8 ♣8 ♦7 ♥6 ♣6 ♦5 ♣4 ♦3
底牌: ♠10 ♥7 ♥3

 

你可能感兴趣的:(java集合--模拟斗地主发牌洗牌)