• Java基础知识强化之集合框架笔记71:模拟斗地主洗牌和发牌并对牌进行排序的案例


    1. 模拟斗地主洗牌和发牌并对牌进行排序的原理图解

     

    2. 代码实现:

    思路:
      • 创建一个HashMap集合
      • 创建一个ArrayList集合
      • 创建花色数组和点数数组
      • 从0开始往HashMap里面存储编号,并存储对应的牌
        同时往ArrayList里面存储编号即可。
      • 洗牌(洗的是编号)
      • 发牌发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收
      • 看牌遍历TreeSet集合,获取编号,到HashMap集合找对应的牌

     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 public class PokerDemo {
    10     public static void main(String[] args) {
    11         // 创建一个HashMap集合
    12         HashMap<Integer, String> hm = new HashMap<Integer, String>();
    13 
    14         // 创建一个ArrayList集合
    15         ArrayList<Integer> array = new ArrayList<Integer>();
    16 
    17         // 创建花色数组和点数数组
    18         // 定义一个花色数组
    19         String[] colors = { "♠", "♥", "♣", "♦" };
    20         // 定义一个点数数组
    21         String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
    22                 "K", "A", "2", };
    23 
    24         // 从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。
    25         int index = 0;
    26 
    27         for (String number : numbers) {
    28             for (String color : colors) {
    29                 String poker = color.concat(number);
    30                 hm.put(index, poker); // 把牌对应的键值对放到HashMap集合中
    31                 array.add(index);     // 把牌对应的键(即索引)放到ArrayList集合中
    32                 index++;
    33             }
    34         }
    35         hm.put(index, "小王");
    36         array.add(index);
    37         index++;
    38         hm.put(index, "大王");
    39         array.add(index);
    40 
    41         // 洗牌(洗的是编号)
    42         Collections.shuffle(array);
    43 
    44         // 发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
    45         TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
    46         TreeSet<Integer> linQingXia = new TreeSet<Integer>();
    47         TreeSet<Integer> liuYi = new TreeSet<Integer>();
    48         TreeSet<Integer> diPai = new TreeSet<Integer>();
    49 
    50         for (int x = 0; x < array.size(); x++) {
    51             if (x >= array.size() - 3) {
    52                 diPai.add(array.get(x));
    53             } else if (x % 3 == 0) {
    54                 fengQingYang.add(array.get(x));
    55             } else if (x % 3 == 1) {
    56                 linQingXia.add(array.get(x));
    57             } else if (x % 3 == 2) {
    58                 liuYi.add(array.get(x));
    59             }
    60         }
    61 
    62         // 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
    63         lookPoker("风清扬", fengQingYang, hm);
    64         lookPoker("林青霞", linQingXia, hm);
    65         lookPoker("刘意", liuYi, hm);
    66         lookPoker("底牌", diPai, hm);
    67     }
    68 
    69     // 写看牌的功能
    70     public static void lookPoker(String name, TreeSet<Integer> ts,
    71             HashMap<Integer, String> hm) {
    72         System.out.print(name + "的牌是:");
    73         for (Integer key : ts) {
    74             String value = hm.get(key);
    75             System.out.print(value + " ");
    76         }
    77         System.out.println();
    78     }
    79 }

     运行效果,如下:

  • 相关阅读:
    Beginning UICollectionView In iOS 6: Part 2/2
    Intermediate Debugging with Xcode 4.5
    Xcode4: Running Application Tests From The Command Line in iOS
    Mac 在open with列表中清除重复应用程序
    Beginning UICollectionView In iOS 6: Part 1/2
    Xcode Apple MachO Linker warning
    Running Xcode 4 unit tests from the command line
    25 iOS App Performance Tips & Tricks
    Introduction to InApp Purchases in iOS 6 Tutorial
    iPhone Development 101: Cocoa: Notifications
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4867585.html
Copyright © 2020-2023  润新知