public class Test03 {
/**
* 模拟斗地主
*
* 分析: 1 买衣服牌,就是自己创建一个集合对象
* 2 洗牌 Collections.shffle();
* 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;
// 拼接扑克, 并将索引存在hm中
for (String s1 : num) { // 获取数字
for (String s2 : color) { // 获取颜色
hm.put(index, s2.concat(s1));
list.add(index); //把索引添加扫单列集合中
index++; // 索引加1
}
}
//添加小王和大王到双列集合中
hm.put(index, "小王");
list.add(index);
index++;
hm.put(index, "大王");
list.add(index);
//洗牌
Collections.shuffle(list);
//发牌;
TreeSet<Integer> gaojin = new TreeSet<>(); //接收牌可以排序
TreeSet<Integer> longwu = 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)); //把list中的后三个放到底牌中
}else if ( i% 3 == 0) {
gaojin.add(list.get(i));
}else if(i % 3 == 1) {
longwu.add(list.get(i));
}else {
me.add(list.get(i));
}
}
//看牌
lookPoker(hm,gaojin,"高进");
System.out.println();
lookPoker(hm,longwu,"龙五");
System.out.println();
lookPoker(hm,me,"我");
}
/*
* 返回值: void
* 参数列表: HashMap , TreeSet , String name
*/
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) + " "); //获取值
}
}
}
|
|