集合回顾训练之斗地主练习

 1 package Collect_test;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collections;
 5 import java.util.List;
 6 
 7 /**
 8  * FileName: GameDemo
 9  * Author:   lps
10  * Date:     2022/4/13 18:19
11  * Sign:刘品水 Q:1944900433
12  */
13 public class GameDemo {
14     //1.定义一个静态集合存储所有的牌
15     public static List<Card> allCards = new ArrayList<>();
16 
17     //2.做牌 定义静态代码块初始化数据
18     static {
19         //3.定义点数 个数确定 类型确定 使用数组
20         String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
21         //4.定义花色 个数确定 类型确定
22         String[] colors = {"♥", "♠", "♦", "♣"};
23         //5.组合点数和花色
24         int index=0;//记录牌的大小
25         for (String size : sizes) {
26             index++;
27             for (String color : colors) {
28                 //6.封装成一个牌对象
29                 Card c = new Card(size, color,index);
30                 //7.存入到集合容器中
31                 allCards.add(c);
32             }
33         }
34         //8.将大小王耶添加到集合中
35         Card c1 = new Card("", "小王",++index);
36         Card c2 = new Card("", "大王",++index);
37         //static <T> boolean addAll​(Collection<? super T> c, T... elements) 将所有指定的元素添加到指定的集合。
38         Collections.addAll(allCards, c1, c2);
39         System.out.println("新牌" + allCards);
40     }
41 
42     public static void main(String[] args) {
43         //9.洗牌
44         //static void shuffle(List<?> list) 使用默认的随机源随机排列指定的列表。
45         Collections.shuffle(allCards);
46         System.out.println("洗牌后" + allCards);
47         //10.发牌(定义三个玩家 每个玩家的牌是个集合容器)
48         List<Card> lps = new ArrayList<>();
49         List<Card> wyr = new ArrayList<>();
50         List<Card> zw = new ArrayList<>();
51 
52         //11.开始发牌 从集合发出51张牌给三个玩家 剩余三张做底牌
53         for (int i = 0; i < allCards.size()-3; i++) {
54             //先拿到当前牌对象
55             Card c=allCards.get(i);
56             if (i%3==0){
57                 lps.add(c);
58             }else if (i%3==1){
59                 wyr.add(c);
60             }else if(i%3==2){
61                 zw.add(c);
62             }
63         }
64         //12.拿到最后三张底牌
65         //List<E> subList(int fromIndex, int toIndex) 返回指定的 fromIndex (含)和 toIndex之间的列表部分的视图。
66         List<Card> lastThreeCards = allCards.subList(allCards.size()-3, allCards.size());
67 
68         //13.给玩家的牌排序(从大到小)
69         sortCard(lps);
70         sortCard(wyr);
71         sortCard(zw);
72 
73 
74         //14.输出玩家的牌
75         System.out.println("刘品水:"+lps);
76         System.out.println("王雅茹"+wyr);
77         System.out.println("张伟"+zw);
78         System.out.println("底牌:"+lastThreeCards);
79 
80     }
81 
82     /**
83      * 给牌排序
84      * @param cards
85      */
86 
87     private static void sortCard(List<Card> cards) {
88         //4♠, 8♦, J♣, 小王, 大王, A♣, 9♣, 6♠, 7♣, K♠, 8♥, K♣, 5♦, 6♦, 5♠, 6♣
89        /* Collections.sort(cards, new Comparator<Card>() {
90             @Override
91             public int compare(Card s1, Card s2) {
92                 return s2.getIndex()-s1.getIndex();
93             }
94         });*/
95         Collections.sort(cards, (s1, s2) -> s2.getIndex()-s1.getIndex());
96     }
97 
98 }
 1 package Collect_test;
 2 
 3 /**
 4  * FileName: Card
 5  * Author:   lps
 6  * Date:     2022/4/13 18:21
 7  * Sign:刘品水 Q:1944900433
 8  */
 9 public class Card {
10     private String size;
11     private String color;
12     private int index;//牌的真正大小
13 
14     @Override
15     public String toString() {
16         return size + color;
17     }
18 
19     public String getSize() {
20         return size;
21     }
22 
23     public void setSize(String size) {
24         this.size = size;
25     }
26 
27     public String getColor() {
28         return color;
29     }
30 
31     public void setColor(String color) {
32         this.color = color;
33     }
34 
35     public Card() {
36     }
37 
38     public int getIndex() {
39         return index;
40     }
41 
42     public void setIndex(int index) {
43         this.index = index;
44     }
45 
46     public Card(String size, String color, int index) {
47         this.size = size;
48         this.color = color;
49         this.index=index;
50     }
51 }

 

posted @ 2022-04-14 11:02  刘品水  阅读(57)  评论(0编辑  收藏  举报