zoukankan      html  css  js  c++  java
  • 斗地主案例

    案例的需求分析:

    1. 准备牌 54张,存储到一个集合
      • 特殊牌:大王 小王
      • 其他52张牌:
        • 定义一个数组 V 集合,存储4张花色:♣ ♠ ♥ ♦
        • 定义一个数组 V 集合,存储13个序号:2 A K Q .....3
      • 循环嵌套遍历两个 数组 V 集合,组装52张牌:♠2 ♣4......
    2. 洗牌
      • 使用集合工具类Collection的方法
      • Static void shuffle(List<?> list) 使用指定的随机源对指定列表进行置换
      • 会随机打乱集合中元素的顺序
    3. 发牌
      • 要求:1人17张,剩余3张作为底牌,一人一张牌轮流发牌:集合的索引(0-53)% 3
      • 定义4个集合,存储3个玩家的牌和底牌
      • 索引%2,有两个值(0,1)0%2=0 1%2=1 2%2=0 3%2=1
      • 索引%3,有三个值(0,1,2)0%3=0 1%3=1 2%3=2 3%3=0(给玩家发牌)
      • 索引>=51 改底牌发牌(保留三张底牌)
    4. 看牌
      • 直接打印集合,遍历存储玩家和底牌的集合

    代码实现:

    
    import java.util.ArrayList;
    import java.util.Collections;
    
    /*
    * 斗地主综合案例
    *  1.准备牌 2.洗牌 3.发牌 4.看牌
    * */
    public class DouDiZhu {
        public static void main(String[] args) {
             //1. 准备牌 54张,存储到一个集合中
            ArrayList<String> poker = new ArrayList<>();
            //定义两个数组,一个存花色,一个存牌的序号
            String[] colors = {"♠","♥","♣","♦"};
            String[] nums = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
            //先把大王小王存储到poker集合中
            poker.add("大王");
            poker.add("小王");
            //循环嵌套遍历两个数组,组装52张牌
            for (String number : nums) {
                for (String color : colors){
    //                System.out.println(color+number);
                    poker.add(color+number);
                }
            }
    //        System.out.println(poker);
            //2洗牌 使用集合的工具类collections中的方法shuffle(List<?> list)
            //默认随机源对指定列表进行置换
            Collections.shuffle(poker);
    //        System.out.println(poker);
            //3.发牌 定义4个集合,存储玩家的牌和底牌
            ArrayList<String> player01 = new ArrayList<>();
            ArrayList<String> player02 = new ArrayList<>();
            ArrayList<String> player03 = new ArrayList<>();
            ArrayList<String> diPai = new ArrayList<>();
            //遍历集合,获取每一张牌
            //使用poker集合的索引给3个玩家轮流发牌 最后三张为底牌
            for (int i = 0; i < poker.size(); i++) {
                //获取每一张牌
                String p = poker.get(i);
                if (i>=51){
                    diPai.add(p);
                }else if (i%3==0){
                    //给玩家1发牌
                    player01.add(p);
                }else if (i%3==1){
                    player02.add(p);
                }else if (i%3==2){
                    player03.add(p);
                }
            }
            //4.看牌
            System.out.println("张三:"+player01);
            System.out.println("李四:"+player02);
            System.out.println("王五:"+player03);
            System.out.println("底牌:"+diPai);
        }
    }
    

    运行结果(每次都不一样,洗牌是随机):

    张三:[♥A, ♦5, ♥K, ♠Q, ♠10, ♦K, ♠5, ♥9, ♠2, ♦7, ♥J, ♦6, ♠K, ♦3, ♥3, ♣6, ♦9]
    李四:[♥10, ♣2, ♣5, ♦2, ♣Q, ♠3, ♦4, ♣K, ♥8, ♦A, ♣3, ♠7, ♦10, ♥4, ♠9, ♣J, ♣A]
    王五:[♥2, ♣8, 小王, ♥7, ♠4, ♣9, ♥Q, ♦Q, ♦8, ♠A, ♥6, ♠8, ♥5, ♣7, ♠6, ♦J, 大王]
    底牌:[♣10, ♣4, ♠J]
    
  • 相关阅读:
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第50章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第49章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第48章 读书笔记(待更新)
    Spring Boot 中使用 Quartz 实现任务调度
    实战 FastDFS Java 客户端上传文件
    分布式文件系统之 FastDFS
    Java 持久层框架之 MyBatis
    C语言实现贪吃蛇
    [转载]分享三篇非常好的学习心得
    selenium加载cookie报错问题:selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain
  • 原文地址:https://www.cnblogs.com/Shuangyi/p/10929090.html
Copyright © 2011-2022 走看看