/**
* 模拟斗地主洗牌和发牌并对牌进行排序的代码实现
*
* 分析,
* 1,买一盒扑克,其实就是自己定义一个集合,将扑克存储进去
* 2,洗牌 ,就是 shuffle
* 3,发牌,
* 4,看牌,
*/
public static void main(String[] args) {
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 ++ ;
}
}
//System.out.println(index);//输出52 ,
//将小王和大王添加进来
hm.put(index, "小王");
list.add(index); //将52索引添加到集合中
index++;
hm.put(index, "大王");
list.add(index); //将53索引添加到集合中
// System.out.println(hm);
// System.out.println(list);
//洗牌
Collections.shuffle(list);
//发牌
TreeSet<Integer> xiaoming = new TreeSet<>();
TreeSet<Integer> xiaobai = new TreeSet<>();
TreeSet<Integer> me = 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) {
xiaoming.add(list.get(i));
}else if(i%3 == 1) {
xiaobai.add(list.get(i));
}else {
me.add(list.get(i));
}
}
//看牌
lookPoker(hm,xiaoming,"小明");
lookPoker(hm,xiaobai,"小白");
lookPoker(hm,me,"我");
lookPoker(hm,dipai,"底牌");
}
/*
* 看牌参数 返回值类型是void
* 2,参数列表hashMap
* */
public static void lookPoker(HashMap<Integer,String> hm,TreeSet<Integer> ts , String name) {
System.out.println(name + "的牌是:");
for(Integer i : ts) {//i代表双列集合中每一个键,
System.out.print(hm.get(i) + " ");//根据键 获取值
}
System.out.println();
}
}
|