思路:
-
List集合存储 54个编号
-
Map <key,value> key 对应的是编号 , value 是 牌的花色(红方梅黑)+ 具体的一张牌 ,比如 黑桃2
-
用2个数组 存放 花色+ 13张牌
-
将牌的信息 存入到 list和map
-
发牌了… (需要打乱顺序)
-
看一下 自己是什么牌 (看牌)
代码如下
public static void main(String[] args) {
//存放编号
List<Integer> list = new ArrayList<>();
//保存牌信息
Map<Integer, String> map = new HashMap<>();
//4种花色
String[] colors = {"♥", "♠", "♦", "♣"};
//13张牌
String[] numbers = {"A","2", "3", "4", "5","6","7","8","9","10","J","Q", "K"};
int index = 0; //记录 54个牌 .从0-53
//存储 牌信息到上面2个集合
// 4*13 =52次
for (String num : numbers) {
for (String col : colors) {
map.put(index, col + num);
list.add(index);
index++;
}
}
//处理大小王
map.put(52, "小王");
map.put(53, "大王");
list.add(52);
list.add(53);
//发牌之前先打乱顺序(洗牌)
Collections.shuffle(list); //打乱0-53
//发牌准备人数和底牌 (3人 , 底牌3张)
List<Integer> p1 = new ArrayList<>(); //第1人
List<Integer> p2 = new ArrayList<>(); //第2人
List<Integer> p3 = new ArrayList<>(); //第3人
List<Integer> bottom = new ArrayList<>(); //底牌
//发牌开始了
for (int i = 0; i < list.size();i++) {
//先弄出3张作为底牌
if (i < 3) {
bottom.add(list.get(i));
}//剩余51了
else if (i % 3 == 0) {
p1.add(list.get(i));
} else if (i % 3 == 1) {
p2.add(list.get(i));
} else {
p3.add(list.get(i));
}
}
//将自己的牌 排个序
Collections.sort(p1);
Collections.sort(p2);
Collections.sort(p3);
Collections.sort(bottom);
//看牌 (有3个人名 . 每个人都可以看自己的集合数据)
// 需要3个数据 , 姓名+p1/p2/p3+map
show("老王", p1, map);
show("老张", p2, map);
show("老八", p3, map);
show("底牌", bottom, map);
}
private static void show(String name,List<Integer> list, Map<Integer,String> map) {
//搜先看到名字
System.out.print(name+": ");
for(Integer in:list){
String value = map.get(in);
System.out.print(value+" ");
}
System.out.println();
}
}