zoukankan      html  css  js  c++  java
  • 集合案例:模拟斗地主洗牌发牌

    1. 案例介绍

    按照斗地主的规则,完成洗牌发牌的动作。

    具体规则:

            1. 组装54张扑克牌

        2. 将54张牌顺序打乱

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

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

    l  手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3

    2. 案例需求分析

    l  准备牌:

    完成数字与纸牌的映射关系:

    使用双列Map(HashMap)集合,完成一个数字与字符串纸牌的对应关系(相当于一个字典)。

    l  洗牌:

    通过数字完成洗牌发牌

    l  发牌:

    将每个人以及底牌设计为ArrayList<String>,将最后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌。

    存放的过程中要求数字大小与斗地主规则的大小对应。

    将代表不同纸牌的数字分配给不同的玩家与底牌。

    l  看牌:

    通过Map集合找到对应字符展示。

    通过查询纸牌与数字的对应关系,由数字转成纸牌字符串再进行展示。

    代码实现:

     1 import java.util.ArrayList;
     2 import java.util.Collections;
     3 import java.util.HashMap;
     4 import java.util.List;
     5 
     6 /**
     7  * 实现模拟斗地主功能
     8  * 1. 组合牌
     9  * 2. 洗牌
    10  * 3. 发牌
    11  * 4. 看牌
    12  * @author vanguard
    13  *
    14  */
    15 public class DouDiZhu {
    16     public static void main(String[] args) {
    17         //1. 组合牌
    18         //创建Map集合,键是编号, 值是牌
    19         HashMap<Integer, String> pooker = new HashMap<Integer, String>();
    20         //创建List集合存储编号
    21         List<Integer> pookerNum = new ArrayList<Integer>();
    22         //创建花色数组
    23         String[] colors = {"♥", "♣", "♠", "♦"};
    24         //创建点数数组
    25         String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
    26         //定义整型数组,作为键出现
    27         int index = 2;
    28         //遍历数组:花色+点数进行组合,存储到Map集合中
    29         for(String number : numbers) {
    30             for(String color : colors) {
    31                 pooker.put(index, color + number);
    32                 pookerNum.add(index);
    33                 index++;
    34             }
    35         }
    36         //存储大王、小王到Map集合中
    37         pooker.put(0, "大王");
    38         pookerNum.add(0);
    39         pooker.put(1, "小王");
    40         pookerNum.add(1);
    41         
    42         //2. 洗牌,将牌的编号打乱
    43         Collections.shuffle(pookerNum);
    44         
    45         //3. 发牌
    46         //定义三个玩家和底牌集合
    47         List<Integer> play1 = new ArrayList<Integer>();
    48         List<Integer> play2 = new ArrayList<Integer>();
    49         List<Integer> play3 = new ArrayList<Integer>();
    50         List<Integer> bottom = new ArrayList<Integer>();
    51         for(int i = 0; i < pookerNum.size(); i++) {
    52             //先存底牌
    53             if(i < 3) {
    54                 //存底牌
    55                 bottom.add(pookerNum.get(i));
    56                 //对索引%3判断
    57             } else if(i % 3 == 0) {
    58                 //发给玩家1
    59                 play1.add(pookerNum.get(i));
    60             } else if(i %3 == 1) {
    61                 //发给玩家2
    62                 play2.add(pookerNum.get(i));
    63             } else if(i % 3 == 2) {
    64                 //发给玩家3
    65                 play3.add(pookerNum.get(i));
    66             }
    67         }
    68         //各个玩家的牌排序
    69         Collections.sort(play1);
    70         Collections.sort(play2);
    71         Collections.sort(play3);
    72         
    73         //4. 看牌
    74         look("玩家1", play1, pooker);
    75         look("玩家2", play2, pooker);
    76         look("玩家3", play3, pooker);
    77         look("底牌", bottom, pooker);
    78         
    79         
    80     }
    81     
    82     /**
    83      * 看牌的方法
    84      * 将玩家手中的编号,到Mpa集合中查找
    85      * @param name
    86      * @param play
    87      * @param pooker
    88      */
    89     private static void look(String name, List<Integer> play, HashMap<Integer, String> pooker) {
    90         //遍历ArrayList集合,获取元素,作为键到Map集合中查找
    91         System.out.print(name + ":");
    92         for(Integer key : play) {
    93             String value = pooker.get(key);
    94             System.out.print(value + " ");    
    95         }
    96         System.out.println();
    97     }
    98 }
  • 相关阅读:
    Java自学
    Java自学
    Java自学
    java自学
    Java自学
    mybatis基础及原理
    自定义swagger maven codegen插件
    spring学习总结016 --- spring事务原理
    spring学习总结015 --- spring事务
    windows解决端口占用问题
  • 原文地址:https://www.cnblogs.com/guodong-wang/p/7202608.html
Copyright © 2011-2022 走看看