zoukankan      html  css  js  c++  java
  • Java模拟斗地主发牌和洗牌

    1. package cn.itcast_04;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.Collections;  
    5. import java.util.HashMap;  
    6. import java.util.TreeSet;  
    7.   
    8. /* 
    9.  * 思路: 
    10.  *      A:创建一个HashMap集合 
    11.  *      B:创建一个ArrayList集合 
    12.  *      C:创建花色数组和点数数组 
    13.  *      D:从0开始往HashMap里面存储编号,并存储对应的牌 
    14.  *        同时往ArrayList里面存储编号即可。 
    15.  *      E:洗牌(洗的是编号) 
    16.  *      F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收) 
    17.  *      G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌) 
    18.  */  
    19. public class PokerDemo {  
    20.     public static void main(String[] args) {  
    21.         // 创建一个HashMap集合  
    22.         HashMap<Integer, String> hm = new HashMap<Integer, String>();  
    23.   
    24.         // 创建一个ArrayList集合  
    25.         ArrayList<Integer> array = new ArrayList<Integer>();  
    26.   
    27.         // 创建花色数组和点数数组  
    28.         // 定义一个花色数组  
    29.         String[] colors = { "♠", "♥", "♣", "♦" };  
    30.         // 定义一个点数数组  
    31.         String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",  
    32.                 "K", "A", "2", };  
    33.   
    34.         // 从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。  
    35.         int index = 0;  
    36.   
    37.         for (String number : numbers) {  
    38.             for (String color : colors) {  
    39.                 String poker = color.concat(number);  
    40.                 hm.put(index, poker);  
    41.                 array.add(index);  
    42.                 index++;  
    43.             }  
    44.         }  
    45.         hm.put(index, "小王");  
    46.         array.add(index);  
    47.         index++;  
    48.         hm.put(index, "大王");  
    49.         array.add(index);  
    50.   
    51.         // 洗牌(洗的是编号)  
    52.         Collections.shuffle(array);  
    53.   
    54.         // 发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)  
    55.         TreeSet<Integer> fengQingYang = new TreeSet<Integer>();  
    56.         TreeSet<Integer> linQingXia = new TreeSet<Integer>();  
    57.         TreeSet<Integer> liuYi = new TreeSet<Integer>();  
    58.         TreeSet<Integer> diPai = new TreeSet<Integer>();  
    59.   
    60.         for (int x = 0; x < array.size(); x++) {  
    61.             if (x >= array.size() - 3) {  
    62.                 diPai.add(array.get(x));  
    63.             } else if (x % 3 == 0) {  
    64.                 fengQingYang.add(array.get(x));  
    65.             } else if (x % 3 == 1) {  
    66.                 linQingXia.add(array.get(x));  
    67.             } else if (x % 3 == 2) {  
    68.                 liuYi.add(array.get(x));  
    69.             }  
    70.         }  
    71.   
    72.         // 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)  
    73.         lookPoker("风清扬", fengQingYang, hm);  
    74.         lookPoker("林青霞", linQingXia, hm);  
    75.         lookPoker("刘意", liuYi, hm);  
    76.         lookPoker("底牌", diPai, hm);  
    77.     }  
    78.   
    79.     // 写看牌的功能  
    80.     public static void lookPoker(String name, TreeSet<Integer> ts,  
    81.             HashMap<Integer, String> hm) {  
    82.         System.out.print(name + "的牌是:");  
    83.         for (Integer key : ts) {  
    84.             String value = hm.get(key);  
    85.             System.out.print(value + " ");  
    86.         }  
    87.         System.out.println();  
    88.     }  
    89. }  
  • 相关阅读:
    leetcode643.滑动窗口例题
    BZOJ4195 离散化+并查集
    luogu线性表刷题
    2021-5-29 周报博客
    2021-5-28 日报博客
    2021-5-27 日报博客
    2021-5-26 日报博客
    2021-5-25 日报博客
    2021-5-24 日报博客
    梦断代码阅读笔记之二
  • 原文地址:https://www.cnblogs.com/Smina/p/6664910.html
Copyright © 2011-2022 走看看