斗地主

 1 public class CardDemo {
 2     private String size;
 3     private String color;
 4     private int index;
 5 
 6     public CardDemo(String size, String color, int index) {
 7         this.size = size;
 8         this.color = color;
 9         this.index = index;
10     }
11 
12     public int getIndex() {
13         return index;
14     }
15 
16     public void setIndex(int index) {
17         this.index = index;
18     }
19 
20     public CardDemo() {
21     }
22 
23     public String getSize() {
24         return size;
25     }
26 
27     public void setSize(String size) {
28         this.size = size;
29     }
30 
31     public String getColor() {
32         return color;
33     }
34 
35     public void setColor(String color) {
36         this.color = color;
37     }
38 
39     @Override
40     public String toString() {
41         return size + color;
42     }
43 }
 1 public class CardGamesDemo {
 2 
 3     //  牌面 {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
 4     //  花色 {"♠","♥","♣","♦"};
 5     public static List<CardDemo> allCards = new ArrayList<>();
 6 
 7     static {
 8         //  定义牌面
 9         String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
10         //  定义花色
11         String[] colors = {"♠","♥","♣","♦"};
12         //  定义大小
13         int index = 0;
14         for (String size : sizes) {
15             index ++;
16             for (String color : colors) {
17                 CardDemo c = new CardDemo(size, color, index);
18                 allCards.add(c);
19             }
20         }
21 
22         //  大小王添加到集合中
23         CardDemo small = new CardDemo("", "🐍", ++index);
24         CardDemo big = new CardDemo("", "🐉", ++index);
25         Collections.addAll(allCards, big, small);
26         System.out.println("新牌" + allCards);
27     }
28 
29     public static void main(String[] args) {
30         Collections.shuffle(allCards);
31         System.out.println("洗牌后:" + allCards);
32 
33         //  定义三个玩家(每个玩家都是一个集合)
34         List<CardDemo> gName1 = new ArrayList<>();
35         List<CardDemo> gName2 = new ArrayList<>();
36         List<CardDemo> gName3 = new ArrayList<>();
37 
38         //  发牌(从集合中将 51 张牌分发给三位玩家,留三张为底牌)
39         for (int i = 0; i < allCards.size() - 3; i++) {
40             //  先拿到当前排对象
41             CardDemo c = allCards.get(i);
42             if (i % 3 == 0){
43                 gName1.add(c);
44             }else if (i % 3 == 1){
45                 gName2.add(c);
46             } else if (i % 3 == 2) {
47                 gName3.add(c);
48             }else {
49                 System.out.println("少牌,从新来!!!");
50             }
51         }
52         //  拿到最后三张底牌(把最后三张底牌截取一个子集合)
53         List<CardDemo> last = allCards.subList(allCards.size() - 3, allCards.size());
54 
55         //  给玩家的牌排序
56         sortCards(gName1);
57         sortCards(gName2);
58         sortCards(gName3);
59         //  输出玩家的拍
60         System.out.println("玩家1" + gName1);
61         System.out.println("玩家2" + gName2);
62         System.out.println("玩家3" + gName3);
63         System.out.println("底牌:" + last);
64     }
65 
66     //  给牌排序
67     private static void sortCards(List<CardDemo> cards) {
68         Collections.sort(cards, new Comparator<CardDemo>() {
69             @Override
70             public int compare(CardDemo o1, CardDemo o2) {
71                 //  制定规则
72                 return o2.getIndex() - o1.getIndex();
73             }
74         });
75     }
76 }

 

posted @ 2024-01-24 19:46  小※兽  阅读(26)  评论(0)    收藏  举报