java小游戏之扑克牌

思路
1、组装一副牌
2、在遍历组装好的扑克牌前先把大小王存储起来
3、打乱扑克牌的顺序
4、遍历扑克牌
5、首先把三张底牌抽取出来存储
6、依次给三个玩家发牌
7、看牌

组装前的准备工作

        //存储牌和索引
        Map<Integer,String> poker = new HashMap<>();

        //存储纸牌索引
        List<Integer> pokerIndex = new ArrayList<>();

        //花色和数字
        List<String> colors = List.of("♠","♥","♣","♦");
        List<String> nums = List.of("A","2","3","4","5","6","7","8","9","10","J","Q","K");

存储大小王

        //索引
        int index = 0;

        //先把大王和小王存储
        poker.put(index,"大王");
        pokerIndex.add(index);
        index += 1;

        poker.put(index,"小王");
        pokerIndex.add(index);
        index += 1;

开始组装

        for (String num : nums) {
            for (String color : colors) {
                poker.put(index,num+color);
                pokerIndex.add(index);
                index += 1;
            }
        }

打乱顺序

Collections.shuffle(pokerIndex);

发牌给玩家(先存储三张底牌)

        List<Integer> player01 = new ArrayList<>();
        List<Integer> player02 = new ArrayList<>();
        List<Integer> player03 = new ArrayList<>();
        List<Integer> downPoker = new ArrayList<>();

        for (int i = 0; i < pokerIndex.size(); i++) {

            Integer integer = pokerIndex.get(i);

            if (i >= 51){
                downPoker.add(integer);
                
            }else if (i % 3 == 0){
                player01.add(integer);
                
            }else if (i % 3 == 1){
                player02.add(integer);
                
            }else if (i % 3 == 2){
                player03.add(integer);
            }
        }

看牌的方法

    public static void seePoker(String name,Map<Integer,String> poker,List<Integer> pokerIndex){

        System.out.print(name+":");

        for (Integer key : pokerIndex) {

            String value = poker.get(key);

            System.out.print(value+" ");
        }
        System.out.println();
    }

效果
请添加图片描述
源代码

public class Poker {

    public static void main(String[] args) {

        //存储牌和索引
        Map<Integer,String> poker = new HashMap<>();

        //存储纸牌索引
        List<Integer> pokerIndex = new ArrayList<>();

        //花色和数字
        List<String> colors = List.of("♠","♥","♣","♦");
        List<String> nums = List.of("A","2","3","4","5","6","7","8","9","10","J","Q","K");

        //索引
        int index = 0;

        //先把大王和小王存储
        poker.put(index,"大王");
        pokerIndex.add(index);
        index += 1;

        poker.put(index,"小王");
        pokerIndex.add(index);
        index += 1;

        for (String num : nums) {
            for (String color : colors) {
                poker.put(index,num+color);
                pokerIndex.add(index);
                index += 1;
            }
        }

        List<Integer> player01 = new ArrayList<>();
        List<Integer> player02 = new ArrayList<>();
        List<Integer> player03 = new ArrayList<>();
        List<Integer> downPoker = new ArrayList<>();

        Collections.shuffle(pokerIndex);

        for (int i = 0; i < pokerIndex.size(); i++) {

            Integer integer = pokerIndex.get(i);

            if (i >= 51){
                downPoker.add(integer);
            }else if (i % 3 == 0){
                player01.add(integer);
            }else if (i % 3 == 1){
                player02.add(integer);
            }else if (i % 3 == 2){
                player03.add(integer);
            }
        }

        Collections.sort(player01);
        Collections.sort(player02);
        Collections.sort(player03);
        Collections.sort(downPoker);

        seePoker("玩家A",poker,player01);
        seePoker("玩家B",poker,player02);
        seePoker("玩家C",poker,player03);
        seePoker("底 牌",poker,downPoker);

    }

    public static void seePoker(String name,Map<Integer,String> poker,List<Integer> pokerIndex){

        System.out.print(name+":");

        for (Integer key : pokerIndex) {

            String value = poker.get(key);

            System.out.print(value+" ");
        }
        System.out.println();
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值