public static void main(String[] args) {
/*
* 模拟斗地主洗牌和发牌,牌没有排序
* 自己创建一个集合对象 将扑克牌存储进去
* 洗牌 collections 里的方法
* 发牌
* 看牌 底牌单独看
*/
//自己创建一个集合对象 将扑克牌存储进去
String [] num = {"3","4","5","6","7","8","9"
,"10","J","Q","K","A","2"};
String [] color ={"红桃","黑桃","方块","梅花"};
//储存索引和牌
HashMap<Integer, String> hm =new HashMap<>();
//储存索引
ArrayList<Integer> list =new ArrayList<>();
int index =0;
//拼接扑克牌 并将索引和扑克牌存在HashMap中
for (String s1 : num) { //获取数字
for (String s2 : color) {//获取颜色
hm.put(index, s2.concat(s1));
list.add(index); //0到51添加到list集合中
index++;
}
}
//将小王添加到集合中
hm.put(index, "小王");
list.add(index);//52添加到list集合中
index++;
//将大王添加到集合中
hm.put(index, "大王");
list.add(index);//53添加到list集合中
//System.out.println(list);
//洗牌
Collections.shuffle(list);
//发牌
TreeSet<Integer>gaojing =new TreeSet<>();
TreeSet<Integer>longwu =new TreeSet<>();
TreeSet<Integer>mi =new TreeSet<>();
TreeSet<Integer>dipai =new TreeSet<>();
for (int i = 0; i < list.size(); i++) {
if (i>=list.size()-3) {
//存三张底牌给底牌
dipai.add(list.get(i));
}else if (i%3==0) {
gaojing.add(list.get(i));
}else if (i%3==1) {
longwu.add(list.get(i));
}else{
mi.add(list.get(i));
}
}
//看牌
lookpoker(hm, gaojing, "高进");
lookpoker(hm, mi, "我");
lookpoker(hm, longwu, "龙五");
lookpoker(hm, dipai, "底牌");
}
//看牌
//返回值类型 void
//参数列表 hashmap treeset Stringname
public static void lookpoker(HashMap<Integer, String> hm ,TreeSet<Integer> ts ,String name){
System.out.println(name +"的牌是:");
for (Integer i : ts) {
System.out.print(hm.get(i)+" ");
}
System.out.println();
}