1.
模拟斗地主洗牌和发牌并对牌进行排序
案例分析:
1.创建Map集合存储牌,并且大的键对应大的牌,小的键对应小的牌
2.洗牌,洗的是索引(map集合中每张牌对应的键)
3.发牌,发的索引,并且岁这个索引进行排序
4.看牌,根据发到的键去map集合中找出对应的牌
public class ChinesePoker {
public static <V, K> void main(String[] args) {
//模拟斗地主洗牌和发牌并对牌进行排序
//四花色:黑桃 红桃 梅花 方块 1—10-JQK ,大王小王
//创建集合ArrayList、HashMap
//创建花色、点数数组
String[] colors = {"黑桃","红桃","梅花","方块"};
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
ArrayList<Integer> al = new ArrayList<Integer>();
//创建HashMap给每一张牌编号,且把编号赋给ArrayList
HashMap<Integer, String> hm = new HashMap<Integer, String>();
int i = 0 ;
for (String number: numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(i,poker);
al.add(i);
i++;
}
}
hm.put(i, "小王");
al.add(i);
i++;
hm.put(i, "大王");
al.add(i);
i++;
//洗牌ArrayList乱序
Collections.shuffle(al);
//TreeSet接
TreeSet<Integer> A = new TreeSet<>();
TreeSet<Integer> B = new TreeSet<>();
TreeSet<Integer> C = new TreeSet<>();
TreeSet<Integer> D = new TreeSet<>();
for (int j = 0; j < al.size(); j++) {
if (j >= al.size() - 3) {
D.add(al.get(j));
} else if (j % 3 == 0) {
A.add(al.get(j));
} else if (j % 3 == 1) {
B.add(al.get(j));
} else if (j % 3 == 2) {
C.add(al.get(j));
}
}
lookPoker("A",A,hm);
lookPoker("B", B, hm);
lookPoker("C", C, hm);
lookPoker("底牌", D, hm);
}
private static void lookPoker(String string, TreeSet<Integer> a,
HashMap<Integer, String> hm) {
// TODO Auto-generated method stub
System.out.print(string+":");
for (Integer integer : a) {
System.out.print(hm.get(integer)+" ");
}
System.out.println();
}
}