集合实例演示(斗地主发牌)

本文介绍了一款使用Java编程语言实现的斗地主游戏模拟器。通过创建一副54张牌的扑克牌组,包括大小王,并进行洗牌、发牌等操作,模拟了斗地主游戏的基本流程。游戏涉及三个玩家和一副底牌,详细展示了如何利用数据结构如TreeMap和ArrayList来管理和分配牌组。

标题

public class 斗地主 {
    public static void main(String[] args) {
        //模拟发牌 洗牌 看牌
        //1.得有一副牌
        //创建牌盒子
        TreeMap<Integer, ArrayList<String>> poxBox2 = new TreeMap<>();
        //TreeMap会自动去重

        HashMap<Integer, String> map = new HashMap<>();


        ArrayList<String> poxBox = new ArrayList<>();

        //我们生成54张牌,放进牌盒子
        String[] colors = {"♥", "♣", "♠", "♦"};
        String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        int index = 0;
        for (String num : nums) {
            for (String color : colors) {
                poxBox.add(index++, num + color);
            }
        }

        //手动添加大小王
        poxBox.add("☀");
        poxBox.add("?");

        //为牌加上编号
        ArrayList<Integer> cards = new ArrayList<>();

        for (int j = 0; j <= 53; j++) {
            cards.add(j);
        }


        //洗牌

        Collections.shuffle(cards);
        //Collections.shuffle(poxBox);
        // Collections.shuffle(poxBox);
        //Collections.shuffle(poxBox);

        //发牌


        //三个人 加一副底牌
        ArrayList<Integer> 高进牌号 = new ArrayList<>();
        ArrayList<Integer> 刀仔牌号 = new ArrayList<>();
        ArrayList<Integer> 星仔牌号 = new ArrayList<>();
        ArrayList<Integer> 底牌牌号 = new ArrayList<>();

        //最懒的发牌
        //  高进 = (ArrayList<String>) poxBox.subList(0, 13);

        //一人一张发
        //高进 0 3 6 %3 ==0

        //刀子 1 4 7  %3==1
        //星仔 2 5 8  %3==2

        for (int i = 0; i < cards.size(); i++) {
            if (i >= cards.size() - 3) {
                底牌牌号.add(cards.get(i));

            } else if (i % 3 == 0) {
                高进牌号.add(cards.get(i));


            } else if (i % 3 == 1) {
                刀仔牌号.add(cards.get(i));

            } else {
                星仔牌号.add(cards.get(i));

            }
        }


        Collections.sort(高进牌号);
        Collections.sort(刀仔牌号);
        Collections.sort(星仔牌号);
        //牌号对应牌
        ArrayList<String> 高进 = new ArrayList<String>();
        ArrayList<String> 刀仔 = new ArrayList<String>();
        ArrayList<String> 星仔 = new ArrayList<String>();
        ArrayList<String> 底牌 = new ArrayList<String>();

        for (Integer key : 高进牌号) {
            高进.add(poxBox.get(key));
        }
        for (Integer key : 刀仔牌号) {
            刀仔.add(poxBox.get(key));
        }
        for (Integer key : 星仔牌号) {
            星仔.add(poxBox.get(key));
        }
        for (Integer key : 底牌牌号) {
            底牌.add(poxBox.get(key));
        }


        lookPoker("高进", 高进);
        lookPoker("刀仔", 刀仔);
        lookPoker("星仔", 星仔);
        lookPoker("底牌", 底牌);
    }


//看牌
    private static void lookPoker(String name, ArrayList<String> list) {
        System.out.println(name);
        for (String s : list) {
            System.out.print(s+"  ");
        }
        System.out.println();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值