扑克案例

 1 package day1;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collections;
 5 import java.util.HashMap;
 6 
 7 public class pookerDemo {
 8 
 9     public static void main(String[] args) {
10         //创建Map集合   
11         HashMap<Integer, String> pooker = new HashMap<Integer, String>();
12         //创建list集合 
13         ArrayList<Integer> pookerNumber = new ArrayList<>();
14         //定义13个点数的数组
15         String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
16         //定义四个花色
17         String[] colors = {"♠","♥","♦","♣"};
18         int index = 2;
19         //遍历数组 花色+点数的组合,存储到Map集合
20         for(String number:numbers) {
21             for(String color:colors) {
22                 pooker.put(index, number+color);
23                 pookerNumber.add(index);
24                 index++;
25             }
26         }
27         //存储大王和小王
28         pooker.put(0, "大王");
29         pookerNumber.add(0);
30         pooker.put(1, "小王");
31         pookerNumber.add(1);
32         //洗牌 编号打乱
33         Collections.shuffle(pookerNumber);
34 //        System.out.println(pooker);
35 //        System.out.println(pookerNumber);
36         
37         //发牌功能 玩家 底牌
38         ArrayList<Integer> player1 = new ArrayList<Integer>();
39         ArrayList<Integer> player2 = new ArrayList<Integer>();
40         ArrayList<Integer> player3 = new ArrayList<Integer>();
41         ArrayList<Integer> dipai = new ArrayList<Integer>();  
42         //发牌
43         for(int i=0;i<pookerNumber.size();i++) {
44             //底牌
45             if(i<3) {
46                 dipai.add(pookerNumber.get(i));
47                 //对索引进行判断
48             }else if(i%3==0) {
49                 //索引上的编号 发给玩家1
50                 player1.add(pookerNumber.get(i));
51             }
52             if(i%3==1) {
53                 //索引上的编号 发给玩家2
54                 player2.add(pookerNumber.get(i));
55             }else if(i%3==2){
56                 //索引上的编号 发给玩家3
57                 player3.add(pookerNumber.get(i));
58                 
59             }
60         }
61         //对玩家手中的牌进行排序
62         Collections.sort(player1);
63         Collections.sort(player2);
64         Collections.sort(player3);
65         
66         //看牌 将玩家手中的编号,到Map几个中查找 ,根据键查找
67         Look("玩家1",player1,pooker);
68         Look("玩家2",player2,pooker);
69         Look("玩家3",player3,pooker);
70         Look("底牌",dipai,pooker);
71     }
72     /**
73      * 
74      * @param name
75      * @param playe
76      * @param pooker
77      */
78     public static void Look(String name,ArrayList<Integer> playe,HashMap<Integer, String> pooker) {
79         System.out.println(name+" ");
80         //通过ArrayList集合获取元素,作为键到Map集合中查找
81         for(Integer key:playe) {
82             String value = pooker.get(key);
83             System.out.print(value+" ");
84         }
85         System.out.println();
86     }
87 
88 }

猜你喜欢

转载自www.cnblogs.com/987m/p/12240556.html