import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* 模拟QQ斗地主发牌 54张
* @author lenovo
*
分析:1、创建一个HashMap集合
2、创建一个ArrayList集合
3、创建一个点数组和花数组
4、遍历数组 往HashMap里面存储 编号和值, 往AarryList里存储编号
5、洗牌、
6、发牌 ,用treeMap集合接收
7.看牌 ,遍历treeMap集合,得到各自的编号,再到HashMap集合集合中找到对应的值
*
*
*/
public class PorkDemo {
public static void main(String[] args) {
//创建一个HashMap集合
HashMap<Integer, String> hm = new HashMap<Integer, String>();
//创建一个AarryList集合
ArrayList<Integer> arr = new ArrayList<Integer>();
//创建一个点数组和花数组
String[] colors = {"♠", "♥", "♣", "♦"};
String[] numbers = {"3", "4", "5","6", "7", "8", "9", "10","J", "Q", "K","A", "2"};
//遍历数组 往HashMap里面存储 编号和值, 往AarryList里存储编号
int index =0;
for (String number : numbers) {
for (String color : colors) {
String str = color.concat(number);
hm.put(index, str);
arr.add(index);
index++;
}
}
hm.put(index, "小王");
arr.add(index);
index++;
hm.put(index, "大王");
arr.add(index);
//洗牌、
Collections.shuffle(arr);
//发牌 ,用treeMap集合接收 发牌发的是编号
TreeSet<Integer> aa = new TreeSet<Integer>();
TreeSet<Integer> bb = new TreeSet<Integer>();
TreeSet<Integer> cc = new TreeSet<Integer>();
TreeSet<Integer> dipai = new TreeSet<Integer>();
for(int i =0; i<arr.size();i++){
if(i >= arr.size()-3){
dipai.add(arr.get(i));
}else if(i % 3 == 0){
aa.add(arr.get(i));
}else if(i % 3 == 1){
bb.add(arr.get(i));
}else if(i % 3 == 2){
cc.add(arr.get(i));
}
}
//看牌
look ("aa",aa,hm);
look ("bb",aa,hm);
look ("cc",aa,hm);
look ("dipai",aa,hm);
}
private static void look(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
System.out.println(name+"的牌是:");
for (Integer key : ts) {
String value = hm.get( key);
System.out.print(value+" ");
}
System.out.println();
}
}