Java基础:斗地主发牌案例Map版(有序版)

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 
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值