• 斗地主案例代码实现JAVA基础学习


    package com.wybing.demo1;
    
    /**
     * ClassName:    DouDiZhu
     * Package:    com.wybing.demo1
     * Description: 斗地主案例代码实现(https://www.bilibili.com/video/BV1uJ411k7wy?p=254&spm_id_from=pageDriver)
     * Datetime:    2021/7/4   19:24
     * Author:   wybing(wybingcom@126.com)
     */
    
    import java.util.ArrayList;
    import java.util.Collections;
    
    /**
     * 实现思路:
     *  1.准备牌
     *  2.洗牌
     *  3.发牌
     *  4.看牌
     *
     */
    
    public class DouDiZhu {
        public static void main(String[] args) {
            // 准备牌
            // 定义一个存储54张牌的ArrayList集合,泛型使用String
            ArrayList<String> poker = new ArrayList<>();
            // 定义两个数组,一个数组存储花色,一个数组存储牌的序号
            String[] colors = {"♠","♥","♣","♦"};
            String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
            // 先把大王和小王存储到poker集合中
            poker.add("大王");
            poker.add("小王");
            // 循环嵌套遍历两个数组,组装52张牌
            for (String number : numbers){
                for (String color : colors){
                    // System.out.println(color+number);
                    // 把组装好的牌存储到poker集合中
                    poker.add(color + number);
                }
            }
            // System.out.println(poker);
            /**
             * 洗牌
             * 使用集合工具类Collection中的方法
             * static void shuffle(List<?> list) 使用默认随机源堆指定列表进行置换
             *
             */
    
            Collections.shuffle(poker);
    
            // System.out.println(poker);
    
            /**
             * 发牌
             * 定义4个集合,存储玩家的牌和底牌
             * 遍历poker集合,获取每一张牌
             * 使用poker集合的索引%3给3个玩家轮流发牌
             * 剩余3张牌给底牌
             * 注意:
             *      先判断底牌(i>= 51),否则牌就发没了
             */
            ArrayList<String> player1 = new ArrayList<>();
            ArrayList<String> player2 = new ArrayList<>();
            ArrayList<String> player3 = new ArrayList<>();
            ArrayList<String> dipai = new ArrayList<>();
    
            for (int i = 0; i < poker.size(); i++) {
                // 获取每一张牌
                String p = poker.get(i);
                // 轮流发牌
                if(i >= 51){
                    dipai.add(p);
                }else if(i % 3 == 0){
                    player1.add(p);
                }else if(i % 3 == 1){
                    player2.add(p);
                }else if(i % 3 == 2){
                    player3.add(p);
                }
            }
    
            // 看牌
            System.out.println("玩家1"+player1);
            System.out.println("玩家2"+player2);
            System.out.println("玩家3"+player3);
            System.out.println("底牌"+dipai);
        }
    }
    
    

    斗地主综合案例:有序版本

    package com.wybing.demo1.ddz;
    
    /**
     * ClassName:    DouDiZhu
     * Package:    com.wybing.demo1.ddz
     * Description:  斗地主综合案例:有序版本
     * Datetime:    2021/7/4   20:54
     * Author:   wybing(wybingcom@126.com)
     */
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    
    /**
     * 斗地主综合案例:有序版本
     * 1.准备牌
     * 2.洗牌
     * 3.发牌
     * 4.排序
     * 5.看牌
     *
     */
    
    public class DouDiZhu {
        public static void main(String[] args) {
            // 1.准备牌
            // 创建一个Map集合,存储牌的索引和组装好的牌
            HashMap<Integer, String> poker = new HashMap<>();
            // 创建一个List集合,存储牌的索引
            ArrayList<Integer> pokerIndex = new ArrayList<>();
            // 定义两个集合,存储花色和牌的序号
            // java9之后才有,我的java版本为8
            // List<String> colors = new List.of("♠","♥","♣","♦");
            // List<String> numbers = new List.of("2","A","K","Q","J","10","9","8","7","6","5","4","3");
    
            List<String> colors = new ArrayList<String>();
            Collections.addAll(colors, "♠","♥","♣","♦");
            List<String> numbers = new ArrayList<String>();
            Collections.addAll(numbers, "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++;
            // 循环嵌套遍历两个集合,组装52张牌,存储到集合中
            for (String number : numbers){
                for(String color : colors){
                    poker.put(index,color + number);
                    pokerIndex.add(index);
                    index++;
                }
            }
            // System.out.println(poker);
            // System.out.println(pokerIndex);
    
            /**
             * 2.洗牌
             * 使用集合工具类Collection中的方法
             * static void shuffle(List<?> list) 使用默认随机源堆指定列表进行置换
             *
             */
            Collections.shuffle(pokerIndex);
            // System.out.println(pokerIndex);
    
            /**
             * 3.发牌
             * 定义4个集合,存储玩家的索引和底牌的索引
             *
             */
            ArrayList<Integer> player1 = new ArrayList<>();
            ArrayList<Integer> player2 = new ArrayList<>();
            ArrayList<Integer> player3 = new ArrayList<>();
            ArrayList<Integer> dipai = new ArrayList<>();
            // 遍历存储牌的索引的List集合,获取每一个牌的索引
            for(int i = 0; i < pokerIndex.size(); i++){
                Integer in = pokerIndex.get(i);
                // 先判断底牌
                if(i >= 51){
                    dipai.add(in);
                }else if(i % 3 == 0){
                    // 给玩家1发牌
                    player1.add(in);
                }else if(i % 3 == 1){
                    // 给玩家2发牌
                    player2.add(in);
                }else if(i % 3 == 2){
                    // 给玩家3发牌
                    player3.add(in);
                }
            }
    
            /**
             * 4.排序
             * 使用Collections中的方法sort(list)
             * 默认是升序排序
             */
            Collections.sort(player1);
            Collections.sort(player2);
            Collections.sort(player3);
            Collections.sort(dipai);
    
            // 5.看牌
            lookPoker("玩家1",poker,player1);
            lookPoker("玩家2",poker,player2);
            lookPoker("玩家3",poker,player3);
            lookPoker("底牌",poker,dipai);
        }
    
        /**
         * 定义一个看牌的方法,提高代码的复用性
         * 参数:
         *      String name : 玩家名称
         *      HashMap<Integer, String> poker : 存储牌的集合
         *      ArrayList<Integer> list : 存储玩家和底牌的集合
         * 查表法:
         *      遍历玩家或底牌集合,获取牌的索引
         *      使用牌的索引,去Map集合中,找到对应的牌
         *
         */
    
        public static void lookPoker(String name, HashMap<Integer, String> poker, ArrayList<Integer> list){
            // 输出玩家的名称,不换行
            System.out.print(name + ": ");
            // 遍历玩家或底牌集合,获取牌的索引
            for(Integer key : list){
                // 使用牌的索引,去map集合中,找到对应的牌
                String value = poker.get(key);
                System.out.print(value + " ");
            }
            System.out.println(); // 打印完每一个玩家的牌,换行
        }
    }
    
    
  • 相关阅读:
    10.19的一些题
    10.18 模拟赛
    bzoj 2212 Tree Rotations
    10.11的一些题
    10.15 模拟赛
    bzoj 5329 战略游戏
    php面向对象基础(二)
    php面向对象基础(一)
    PHP基础-数组与数据结构
    php基础-字符串处理
  • 原文地址:https://www.cnblogs.com/wybing/p/14969795.html
Copyright © 2020-2023  润新知