Java学习总结--037模拟斗地主洗牌、发牌、按顺序整理牌面

本文总结了使用Java进行斗地主游戏的洗牌、发牌操作,并详细介绍了如何按照顺序整理牌面的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟斗地主洗牌和发牌

public class MyTest {
    public static void main(String[] args) {
        //案例演示:
        //模拟斗地主洗牌和发牌,牌没有排序
        //得有一副牌
        ArrayList<String> pokerBox = new ArrayList<>();
        //生成54张牌放到牌盒里面
        String[] colors = {"♠", "♥", "♦", "♣"};
        String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        for (String color : colors) {
            for (String num : nums) {
                pokerBox.add(color.concat(num));
            }
        }
        //手动添加大小王
        pokerBox.add("★");
        pokerBox.add("☆");
        //洗牌
        Collections.shuffle(pokerBox);
        Collections.shuffle(pokerBox);
        Collections.shuffle(pokerBox);
        //发牌
        ArrayList<String> 高进 = new ArrayList<>();
        ArrayList<String> 刀仔 = new ArrayList<>();
        ArrayList<String> 星仔 = new ArrayList<>();
        ArrayList<String> 底牌 = new ArrayList<>();
        // 高进 = (ArrayList<String>) pokerBox.subList(0,17);
        // 高进 0  3 6 9
        //刀仔 1 4 7 10
        //  星仔 2 5 8 11
        for (int i = 0; i < pokerBox.size(); i++) {
            if (i >= pokerBox.size() - 3) {
                底牌.add(pokerBox.get(i));
            } else if (i % 3 == 0) {
                高进.add(pokerBox.get(i));
            } else if (i % 3 == 1) {
                刀仔.add(pokerBox.get(i));
            } else {
                星仔.add(pokerBox.get(i));
            }
        }
        //看牌
        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();
    }
}

添加洗牌发牌后,按顺序整理牌

public class MyTest {
    public static void main(String[] args) {
        //选用双列集合
        HashMap<Integer, String> pokerBox = new HashMap<>();
        //创建一个索引集合
        ArrayList<Integer> indexs = new ArrayList<>();
        int index = 0;
        String[] colors = {"♠", "♥", "♦", "♣"};
        String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        for (String num : nums) {
            for (String color : colors) {
                pokerBox.put(index, num.concat(color));
                indexs.add(index);
                index++;
            }
        }
        //手动添加大小王
        pokerBox.put(index, "★");//
        indexs.add(index);
        index++;
        pokerBox.put(index, "☆");
        indexs.add(index);
        //洗牌
        Collections.shuffle(indexs);
        //发牌
        TreeSet<Integer> 高进 = new TreeSet<Integer>();
        TreeSet<Integer> 刀仔 = new TreeSet<Integer>();
        TreeSet<Integer> 星仔 = new TreeSet<Integer>();
        TreeSet<Integer> 底牌 = new TreeSet<Integer>();
        for (int i = 0; i < indexs.size(); i++) {
            if (i >= indexs.size() - 3) {
                底牌.add(indexs.get(i));
            } else if (i % 3 == 0) {
                高进.add(indexs.get(i));
            } else if (i % 3 == 1) {
                刀仔.add(indexs.get(i));
            } else {
                星仔.add(indexs.get(i));
            }
        }
        //看牌
        lookPoker("高进", 高进, pokerBox);
        lookPoker("刀仔", 刀仔, pokerBox);
        lookPoker("星仔", 星仔, pokerBox);
        lookPoker("底牌", 底牌, pokerBox);
    }
    private static void lookPoker(String name, TreeSet<Integer> set, HashMap<Integer, String> pokerBox) {
        System.out.println(name);
        for (Integer key : set) {
            System.out.print(pokerBox.get(key) + "  ");
        }
        System.out.println();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值