[Java] 案例 Pooker 完善

分析

准备牌
    定义一个Map集合用来存储 牌索引 扑克牌
    定义一个List集合用来存储 牌索引
    定义一个花色的数组  "♥-♠-♣-♦"
    定义一个牌数字的数字 "2-A-K-Q-J-10-9-8-7-6-5-4-3"
    循环拼接扑克牌  将牌索引 和拼接后的扑克牌放入到map集合中
    将牌索引入到list集合中
    单独添加大王和小王

洗牌
    Collections
         public static void shuffle(List list) 打乱顺序
发牌
    三个玩家 三个集合
    底牌一个集合
    遍历List集合发 判断索引  发牌索引
    >=51  底牌
    %3== 0 玩家1
    %3== 1 玩家2
    %3== 2 玩家3
排序
看牌
   遍历玩家集合 获取到每个牌的索引 到map集合中找到对应的扑克牌 显示

代码

    public static void main(String[] args) {
        // 定义一个Map集合用来存储 牌索引 扑克牌
        Map<Integer, String> map = new HashMap<>();
        // 定义一个List集合用来存储 牌索引
        List<Integer> list = new ArrayList<>();
        // 定义一个花色的数组  "♥-♠-♣-♦"
        String[] colors = "♥-♠-♣-♦".split("-");
        //定义一个牌数字的数组 "2-A-K-Q-J-10-9-8-7-6-5-4-3"
        String[] nums = "2-A-K-Q-J-10-9-8-7-6-5-4-3".split("-");
        //循环拼接
        int index = 2;
        for (String num : nums) {
            for (String color : colors) {
                String thisPooker = color + num;
                //将牌索引 和拼接后的扑克牌放入到map集合中
                map.put(index,thisPooker);
                index++;
            }
        }
        //单独添加大王和小王
        map.put(0,"大☺");
        map.put(1,"小☻");

        //将牌的索引 添加到List集合中
        for (int i = 0; i < 54; i++) {
            list.add(i);
        }

        //洗牌
        Collections.shuffle(list);

        //发牌
        List<Integer> player1 = new ArrayList<>();
        List<Integer> player2 = new ArrayList<>();
        List<Integer> player3 = new ArrayList<>();
        List<Integer> diPai = new ArrayList<>();

        //遍历List集合发 判断索引  发牌索引
        for (int i = 0; i < list.size(); i++) {
            Integer pookerIndex = list.get(i);
            if(i >= 51){
                diPai.add(pookerIndex);
            }else if(i % 3 == 0){
                player1.add(pookerIndex);
            }else if(i % 3 == 1){
                player2.add(pookerIndex);
            }else {
                player3.add(pookerIndex);
            }
        }

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


        show("张三",player1,map);
        show("李四",player2,map);
        show("王五",player3,map);
        show("底牌",diPai,map);

    }
    public static void show(String name,List<Integer> player, Map<Integer, String> map ){
        StringBuilder sb = new StringBuilder();
        sb.append(name+":[");
        for (int i = 0; i < player.size(); i++) {
            Integer key = player.get(i);
            String pooker = map.get(key);
            sb.append(pooker);
            if(i == player.size()-1){
                sb.append("]");
            }else {
                sb.append(", ");
            }
        }

        System.out.println(sb.toString());

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值