首页 > 代码库 > 集合框架08

集合框架08

一、斗地主

 7 public class Demo01 {
 8     /*
 9      * 斗地主:
10      * 1.组合牌
11      * 2.洗牌
12      * 3.发牌
13      * 4.看牌
14      */
15     public static void main(String[] args) {
16         //1.组合牌
17         //创建Map集合,键是编号,值是牌
18         HashMap<Integer,String> pooker = new HashMap<Integer,String>();
19         //创建List集合存储编号
20         ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
21         //定义13个点数
22         String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
23         String[] colors = {"?","?","?","?"};
24         //定义整数变量,作为键出现
25         int index = 2;
26         
27         //遍历数组,花色+点数的组合,存储到Map集合
28         for(String number:numbers){
29             for(String color:colors){
30                 pooker.put(index, color+number);
31                 pookerNumber.add(index);
32                 index++;
33             }
34         }
35         //存储大小王
36         pooker.put(0, "大王");
37         pookerNumber.add(0);
38         pooker.put(1, "小王");
39         pookerNumber.add(1);
40         
41         //洗牌,将牌的编号打乱
42         Collections.shuffle(pookerNumber);
43         
44         //发牌,将牌的编号,发给玩家集合,底牌集合
45         ArrayList<Integer> player1 = new ArrayList<Integer>();
46         ArrayList<Integer> player2 = new ArrayList<Integer>();
47         ArrayList<Integer> player3 = new ArrayList<Integer>();
48         ArrayList<Integer> bottom = new ArrayList<Integer>();
49         //发牌采用的是集合索引%3
50         for(int i=0;i<pookerNumber.size();i++){
51             //先将底牌做好
52             if(i<3){
53                 bottom.add(pookerNumber.get(i));
54             }else if(i%3==0){
55                 player1.add(pookerNumber.get(i));
56             }else if(i%3==1){
57                 player2.add(pookerNumber.get(i));
58             }else if(i%3==2){
59                 player3.add(pookerNumber.get(i));
60             }
61         }
62         //排序
63         Collections.sort(player1);
64         Collections.sort(player2);
65         Collections.sort(player3);
66         //看牌,将玩家手中的编号到Map集合中查找,根据键找值
67         look("玩家一",player1,pooker);
68         look("玩家二",player2,pooker);
69         look("玩家三",player3,pooker);
70         look("底牌",bottom,pooker);
71         
72     }
73     public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker){
74         System.out.print(name+":");
75         for(Integer key:player){
76             String value =http://www.mamicode.com/ pooker.get(key);
77             System.out.print(value+"   ");
78     }
79         System.out.println();
80     }
81 }

 

集合框架08