集合框架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 = pooker.get(key);
77             System.out.print(value+"   ");
78     }
79         System.out.println();
80     }
81 }

 

转载于:https://www.cnblogs.com/Nelsoner/p/6699623.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值