packagedemo;importjava.util.ArrayList;importjava.util.Collections;importjava.util.HashMap;public classDouDiZhu {public static voidmain(String[] args) {//创建Map集合,键是编号,值是牌
HashMap pooker = new HashMap();//List集合存储编号//用List集合原因:可以调用排序方法
ArrayList pookerNumber = new ArrayList();//由于13个点数恒定,定义数组
String[] numbers = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};//花色恒定,定义数组
String[] colors = { "♠", "♥", "♣", "♦"};//定义整数变量,作为键,0和1留给大小王
int index = 2;//遍历数组,存入Map集合
for(String number : numbers) {for(String color : colors) {
pooker.put(index, color+number);
pookerNumber.add(index);
index++;
}
}//单独存储大小王
pooker.put(0, "大王");
pookerNumber.add(0);
pooker.put(1, "小王");
pookerNumber.add(1);//洗牌,将牌的编号打乱
Collections.shuffle(pookerNumber);//发牌//三个玩家和底牌
ArrayList player1 = new ArrayList();
ArrayList player2 = new ArrayList();
ArrayList player3 = new ArrayList();
ArrayList dipai = new ArrayList();//每张依次发到三个玩家
for (int i = 0; i < pookerNumber.size(); i++) {//先将底牌做好
if (i < 3) {
dipai.add(pookerNumber.get(i));
}//依次给每个玩家发牌
else if (i % 3 == 0) {
player1.add(pookerNumber.get(i));
}else if (i % 3 == 1) {
player2.add(pookerNumber.get(i));
}else if (i % 3 == 2) {
player3.add(pookerNumber.get(i));
}
}//对玩家手中的牌排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);//看牌,根据键找值
look("玩家1", player1, pooker);
look("玩家2", player2, pooker);
look("玩家3", player3, pooker);
look("底牌", dipai, pooker);
}public static void look(String name, ArrayList player, HashMappooker) {
System.out.print(name+ ":");for(Integer key : player) {
String value=pooker.get(key);
System.out.print(" " +value);
}
System.out.println();
}
}