package com.xiaolong.map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
//斗地主,买牌,洗牌,发牌,treeSet排序
public class Test4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
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;
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < color.length; j++) {
hm.put(index, color[j].concat(num[i]));
list.add(index);
index++; //51+1
}
}
// 添加大小王
hm.put(index, "小王"); //52
list.add(index);
index++;
hm.put(index, "大王"); //53
list.add(index);
// 洗牌
Collections.shuffle(list);
// 发牌,使用treeSet加收同时可以排序
ArrayList<Integer> dipai = new ArrayList<>();
TreeSet<Integer> dizhu = new TreeSet<>();
TreeSet<Integer> nongmin1 = new TreeSet<>();
TreeSet<Integer> nongmin2 = new TreeSet<>();
for (int i = 0; i < list.size();) {
if (i >= list.size()-3) {
dipai.add(list.get(i++));
} else {
nongmin1.add(list.get(i++));
dizhu.add(list.get(i++));
nongmin2.add(list.get(i++));
}
}
look(dizhu, hm, "地主");
look(nongmin1, hm, "农民1");
look(nongmin2, hm, "农民2");
System.out
.println("底牌是:" + hm.get(dipai.get(2)) + " " + hm.get(dipai.get(1)) + " " + hm.get(dipai.get(0)) + " ");
}
// 看牌的方法,根据键获取对应的值
public static void look(TreeSet<Integer> ts, HashMap<Integer, String> hm, String name) {
System.out.println(name + "的牌是:");
for (Integer index : ts) {
System.out.print(hm.get(index) + " ");
}
System.out.println();
}
} |
|