/**
* 模仿洗牌发牌
* @author LENOVO
*
*/
public class Demo7 {
public static void main(String[] args) {
//定义Poker
String[] str = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String[] color = {"红桃","黑桃","梅花","方块"};
//一副新牌
HashMap<Integer, String> map = new HashMap<>();
//每张牌的索引存放
ArrayList<Integer> list = new ArrayList<>();
//Integer索引
int index = 0;
for (String s : str) {
for (String c : color) {
map.put(index,c.concat(s));
list.add(index);
index++;
}
}
map.put(index, "小王");
list.add(index);
index++;
map.put(index, "大王");
list.add(index); //添加索引到List中
//洗牌
Collections.shuffle(list);
//System.out.println(list);
//玩家
TreeSet<Integer> one = new TreeSet<>();
TreeSet<Integer> two = new TreeSet<>();
TreeSet<Integer> three = new TreeSet<>();
//底牌
TreeSet<Integer> end = new TreeSet<>();
//发牌
for (int i = 0; i < list.size(); i++) {
if(i >= list.size()-3) {
end.add(list.get(i));
}else if (i % 3 == 0) {
one.add(list.get(i));
}else if (i % 3 == 1) {
two.add(list.get(i));
}else {
three.add(list.get(i));
}
}
/*System.out.println(one);
System.out.println(two);
System.out.println(three);
System.out.println(end);*/
lookPoker(one, map, "李蒙蒙");
lookPoker(two, map, "崔宁");
lookPoker(three, map, "张三");
lookPoker(end, map, "底牌");
/*
//洗牌 打乱索引
Collections.shuffle(list);
System.out.println(list);
//底牌
ArrayList<String> end = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if(i >= list.size()-3) {
end.add(list.get(i));
}else if (i % 3 == 0) {
one.add(list.get(i));
}else if (i % 3 == 1) {
two.add(list.get(i));
}else {
three.add(list.get(i));
}
}
System.out.println(one);
System.out.println(two);
System.out.println(three);
System.out.println(end);*/
}
public static void lookPoker(TreeSet<Integer> set,HashMap<Integer, String> map,String name) {
System.out.print(name + ":");
for (Integer integer : set) {
System.out.print(map.get(integer) + " ");
}
System.out.println();
}
} |