18.29_集合框架(模拟斗地主洗牌和发牌并对牌进行排序的代码实现)

时间:2022-11-24 19:24:11
 1 package dou_di_zhu;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collection;
 5 import java.util.Collections;
 6 import java.util.HashMap;
 7 import java.util.TreeSet;
 8 
 9 /**
10  * 思路:
11  *         A:创建一个HashMap集合
12  *         B:创建一个ArrayList集合
13  *         C:创建花色数组和点数数组
14  *         D:从0开始往HashMap里面存储编号,并存储对应的牌
15  *             同时往ArrayList里面存储编号即可
16  *         E:洗牌(洗的是编号)
17  *         F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
18  *         G:看牌(遍历TreeSet集合,获取编号,到HashMap中找对应的牌)
19  * @author user
20  *
21  */
22 public class PokerDemo1 {
23     public static void main(String[] args) {
24         //创建一个HashMap集合
25         HashMap<Integer, String> hm = new HashMap<Integer,String>();
26         
27         //创建一个ArrayList集合
28         ArrayList<Integer> array = new ArrayList<Integer>();
29         
30         //创建花色数组和点数数组
31         //定义一个花色数组
32         String[] colors  = {"♠","♥","♣","♦"};
33         //定义一个点数数组
34         String[] numbers = {"3","4","5","6","7","8",
35                 "9","10","J","Q","K","A","2"};
36         
37         //从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可
38         int index = 0;
39         for(String number : numbers){
40             for(String color : colors){
41                 String poker = color.concat(number);
42                 hm.put(index, poker);
43                 array.add(index);
44                 index++;
45             }
46         }
47         hm.put(index, "小王");
48         array.add(index);
49         index++;
50         hm.put(index, "大王");
51         array.add(index);
52         
53         //洗牌(洗的是编号)
54         Collections.shuffle(array);
55         
56         //发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
57         TreeSet<Integer> fanBingBing = new TreeSet<Integer>();
58         TreeSet<Integer> sunHang = new TreeSet<Integer>();
59         TreeSet<Integer> linQingXia = new TreeSet<Integer>();
60         TreeSet<Integer> diPai = new TreeSet<Integer>();
61         
62         for (int x = 0; x < array.size(); x++) {
63             if (x >= array.size() - 3) {
64                 diPai.add(array.get(x));
65             }else if (x%3 == 0) {
66                 fanBingBing.add(array.get(x));
67             }else if (x%3 == 1) {
68                 sunHang.add(array.get(x));
69             }else if (x%3 == 2) {
70                 linQingXia.add(array.get(x));
71             }
72         }
73         
74         //看牌(遍历TreeSet集合,获取编号,到HashMap中找对应的牌)
75         lookPoker("范冰冰",fanBingBing,hm);
76         lookPoker("孙航",sunHang,hm);
77         lookPoker("林青霞",linQingXia,hm);
78         lookPoker("底牌",diPai,hm);
79     }
80     //看牌的功能实现
81     public static void lookPoker(String name,TreeSet<Integer> ts,
82             HashMap<Integer, String> hm){
83         System.out.println(name + "的牌是:");
84         for(Integer key : ts){
85             String value = hm.get(key);
86             System.out.print(value + " ");
87         }
88         System.out.println();
89     }
90 }