斗地主案例的需求分析和斗地主案例的代码实现

案例介绍
  按照斗地主的规则,完成洗牌发牌的动作、

  

 

 

具体规则:
1.组装54张扑克牌将

2.54张牌顺序打乱
3.三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌

4.查看三人各自手中的牌(按照牌的大小排序)、底牌

案例分析

 

 代码实现

复制代码
public class PokerTest {
        public static void main(String[] args) {
            //准备牌
            //存牌的索引和组装好的牌
            HashMap<Integer, String> poker = new HashMap<>();
            //存牌的索引
            ArrayList<Integer> pokerIndex = new ArrayList<>();
            //两个集合(JDK1.9的of)存储花色和牌号(这里用数组)
            String[] colors = {"♠", "♥", "♣", "♦"};
            String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
    /*        ArrayList<String> colors = new ArrayList<>();
            colors.add("♠");
            colors.add("♥");
            colors.add("♣");
            colors.add("♦");
            ArrayList<String> numbers = new ArrayList<>();
            numbers.add("2");
            numbers.add("A");
            numbers.add("K");
            numbers.add("Q");
            numbers.add("J");
            numbers.add("10");
            numbers.add("9");
            numbers.add("8");
            numbers.add("7");
            numbers.add("6");
            numbers.add("5");
            numbers.add("4");
            numbers.add("3");*/
    
            //把大王小王存储到集合中
            int index = 0;
            poker.put(index, "大王");
            pokerIndex.add(index);
            index++;
            poker.put(index, "小王");
            pokerIndex.add(index);
            index++;
    
            //循环嵌套来存储52张牌
            for (String number : numbers) {
                for (String color : colors) {
                    poker.put(index, number + color);
                    pokerIndex.add(index);
                    index++;
                }
            }
            //System.out.println(poker);
    
            //2洗牌,使用Collections中的shuffle方法打乱集合(pokerIndex)
            Collections.shuffle(pokerIndex);
    
            //3发牌:定义四个集合
            ArrayList<Integer> palyer01 = new ArrayList<>();
            ArrayList<Integer> palyer02 = new ArrayList<>();
            ArrayList<Integer> palyer03 = new ArrayList<>();
            ArrayList<Integer> diPai = new ArrayList<>();
    
            //遍历pokerIndex集合,获取每一个索引
            for (int i = 0; i < pokerIndex.size(); i++) {
                Integer in = pokerIndex.get(i);
                if(i >= 51){//发底牌
                    diPai.add(in);
                }else if(i % 3 == 0){//给玩家一
                    palyer01.add(in);
                }else if(i % 3 == 1){//给玩家二
                    palyer02.add(in);
                }else if(i % 3 == 2){//给玩家三
                    palyer03.add(in);
                }
            }
    
            //4排序Collections中的sort
            Collections.sort(palyer01);
            Collections.sort(palyer02);
            Collections.sort(palyer03);
            Collections.sort(diPai);
    
            //调用看牌方法lookPoker
            lookPoker("刘德华", poker, palyer01);
            lookPoker("周润发", poker, palyer02);
            lookPoker("周星驰", poker, palyer03);
            lookPoker("底牌", poker, diPai);
        }
    
        //定义一个看牌方法
    //    参数:
    //    String name:玩家名称
    //    HashMap<Integer,String> poker:存储牌的poker集合
    //    ArrayList<Integer> list:存储玩家和底牌的List集合
    //    查表法:
    //    遍历玩家或者底牌集合,获取牌的索引
    //            使用牌的索引,去Map集合中,找到对应的牌
        public static void lookPoker(String name, HashMap<Integer, String> poker, ArrayList<Integer> list){
            //输出玩家名称:
            System.out.println("name:" + name);
    
            //遍历玩家或底牌,获取索引
            for (Integer key : list) {
                String value = poker.get(key);
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }
复制代码

 

posted @   夫君  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示