Java基础:17张牌你能秒我?斗地主发牌案例Map版(有序版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
public class DouDiZhu2 {
    public static void main(String[] args) {
        // Map储存牌的索引和组装好的牌
        HashMap<Integer, String> poker = new HashMap<>();
        // List储存牌的索引
        ArrayList<Integer> pokerIndex = new ArrayList<>();
        // 定义两个集合储存花色、牌号
        List<String> colors = List.of("?", "?", "?", "?");
        List<String> nums = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        // 把大王小王先放进去
        int index = 0;
        poker.put(index, "大王");
        pokerIndex.add(index);
        index++;
        poker.put(index, "小王");
        pokerIndex.add(index);
        index++;

        // 把剩下的牌组装并存储
        for (String num : nums) {
            for (String color : colors) {
                poker.put(index, color + num);
                pokerIndex.add(index);
                index++;
            }
        }
        //System.out.println(poker);
        //System.out.println(pokerIndex);

        // 洗牌
        Collections.shuffle(pokerIndex);
        // 发牌
        ArrayList<Integer> player1 = new ArrayList<>();
        ArrayList<Integer> player2 = new ArrayList<>();
        ArrayList<Integer> player3 = new ArrayList<>();
        ArrayList<Integer> diPai = new ArrayList<>();
        for (Integer i : pokerIndex) {
            Integer integer = pokerIndex.get(i);
            if (i >= 51) {
                // 给底牌
                diPai.add(integer);
            } else if (i % 3 == 0) {
                player1.add(integer);
            } else if (i % 3 == 1) {
                player2.add(integer);
            } else if (i % 3 == 2) {
                player3.add(integer);
            }


        }
        // 排序
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        Collections.sort(diPai);

        // 看牌
        look("卢本伟", poker, player1);
        look("阿姨", poker, player2);
        look("我", poker, player3);
        look("底牌", poker, diPai);
    }

    /*
     * 定义一个看牌的方法
     * */
    public static void look(String name, HashMap<Integer, String> poker, ArrayList<Integer> list) {
        // 输出
        System.out.print(name + ": ");
        for (Integer key : list) {
            String value = poker.get(key);
            System.out.print(value + " ");

        }
        System.out.println();//打印一位玩家后换行
    }
}

/*
卢本伟: 大王 ?2 ?A ?A ?A ?K ?10 ?10 ?8 ?8 ?7 ?7 ?6 ?5 ?4 ?4 ?3
阿姨: ?2 ?A ?Q ?Q ?J ?J ?J ?J ?9 ?9 ?8 ?8 ?6 ?5 ?5 ?5 ?4
我: 小王 ?2 ?2 ?K ?K ?Q ?Q ?10 ?10 ?7 ?7 ?6 ?6 ?4 ?3 ?3 ?3
底牌: ?K ?9 ?9
*/