需要经过的过程:
1.准备牌
2.洗牌
3.发牌
4.看牌
示例代码:
public class Test6 {
public static void main(String[] args) {
Map<Integer,String> map=new HashMap<>();
//准备牌
String [] s1={"♥","♠","♣","♦"};
String [] s2={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
map.put(1,"大王");
map.put(2,"小王");
int num=3;
for (String s02 : s2) {
for (String s01 : s1) {
map.put(num++,s01+s02);
}
}
//洗牌
List<Integer> list=new ArrayList<>();
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
list.add(entry.getKey());
}
Collections.shuffle(list);
//发牌
TreeSet<Integer> player1=new TreeSet<>();
TreeSet<Integer> player2=new TreeSet<>();
TreeSet<Integer> player3=new TreeSet<>();
TreeSet<Integer> landlord=new TreeSet<>();
Collections.addAll(landlord,list.get(0),list.get(1),list.get(2));
for (int i = 3; i < list.size(); i++) {
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));
}
}
//看牌
System.out.print("第一个玩家的牌:");
for (Integer p1 :player1){
System.out.print(map.get(p1)+" ");
}
System.out.println();
System.out.print("第二个玩家的牌:");
for (Integer p2 : player2) {
System.out.print(map.get(p2)+" ");
}
System.out.println();
System.out.print("第三个玩家的牌:");
for (Integer p3 : player3) {
System.out.print(map.get(p3)+" ");
}
System.out.println();
System.out.print("地主牌:");
for (Integer l : landlord) {
System.out.print(map.get(l)+" ");
}
}
}