栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java小游戏之扑克牌

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java小游戏之扑克牌

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

组装前的准备工作

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

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

        //花色和数字
        List colors = List.of("♠","♥","♣","♦");
        List 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 player01 = new ArrayList<>();
        List player02 = new ArrayList<>();
        List player03 = new ArrayList<>();
        List 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 poker,List 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 poker = new HashMap<>();

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

        //花色和数字
        List colors = List.of("♠","♥","♣","♦");
        List 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 player01 = new ArrayList<>();
        List player02 = new ArrayList<>();
        List player03 = new ArrayList<>();
        List 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 poker,List pokerIndex){

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

        for (Integer key : pokerIndex) {

            String value = poker.get(key);

            System.out.print(value+" ");
        }
        System.out.println();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/287205.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号