zoukankan      html  css  js  c++  java
  • javase模拟斗地主洗牌和发牌(54)

    1、使用Arraylist集合:

     1 package com.it18zhang.day08;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 
     6 public class PokerDemo {
     7 
     8     public static void main(String[] args) {
     9         // TODO Auto-generated method stub
    10         //牌合
    11         ArrayList<String> al = new ArrayList<>();
    12         String[] colors={ "♠", "♥", "♣", "♦" };
    13         String[] nums={ "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
    14                 "J", "Q", "K" };
    15         for (String color : colors) {
    16             for (String num : nums) {
    17                 String pai=color.concat(num);
    18                 al.add(pai);
    19             }
    20         }
    21         al.add("大王");
    22         al.add("小王");
    23         ArrayList<String> p1 = new ArrayList<>();
    24         ArrayList<String> p2 = new ArrayList<>();
    25         ArrayList<String> p3 = new ArrayList<>();
    26         ArrayList<String> dipai = new ArrayList<>();
    27         Collections.shuffle(al);
    28         for(int i=0;i<al.size();i++){
    29             if(i>=al.size()-3){
    30                 dipai.add(al.get(i));
    31             }
    32             else if(i%3==0){
    33                 p1.add(al.get(i));
    34             }
    35             else if(i%3==1){
    36                 p2.add(al.get(i));
    37             }
    38             else if(i%3==2){
    39                 p3.add(al.get(i));
    40             }
    41         }
    42             lookPoker("p1", p1);
    43             lookPoker("p2", p2);
    44             lookPoker("p3", p3);
    45         }
    46     public static void lookPoker(String name,ArrayList<String> array){
    47         System.out.println(name+"的牌是:");
    48         for (String string : array) {
    49             System.out.print(string+" ");
    50         }
    51         System.out.println();
    52     }
    53 }

    2、使用HashMap集合

     1 package com.lianxi1;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 import java.util.HashMap;
     6 import java.util.TreeSet;
     7 
     8 public class PokerDemo {
     9 
    10     public static void main(String[] args) {
    11         //牌盒,用于存放牌及编号
    12         HashMap<Integer, String> map = new HashMap<>();
    13         //集合,用于存放编号
    14         ArrayList<Integer> list = new ArrayList<>();
    15         //花色数组
    16         String[] colors={"♠", "♥", "♣", "♦"};
    17         //点数数组
    18         String[] nums={"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
    19             "K", "A", "2", };
    20         int index=0;
    21         //分别存放牌及编号到集合中
    22         for (String color : colors) {
    23             for (String num : nums) {
    24                 String pork=color.concat(num);
    25                 map.put(index, pork);
    26                 list.add(index);
    27                 index++;
    28             }
    29         }
    30         map.put(index, "小王");
    31         list.add(index);
    32         index++;
    33         map.put(index, "大王");
    34         list.add(index);
    35         //洗牌(对编号集合进行)
    36         Collections.shuffle(list);
    37         //定义四个集合,用于存放底牌及玩家的手牌编号(由于排序,使用treeSet)
    38         TreeSet<Integer> dipai = new TreeSet<>();
    39         TreeSet<Integer> p1 = new TreeSet<>();
    40         TreeSet<Integer> p2 = new TreeSet<>();
    41         TreeSet<Integer> p3 = new TreeSet<>();
    42         //发牌,实际是编号
    43         for(int i=0;i<list.size();i++){
    44             if(i>=(list.size()-3)){
    45                 dipai.add(list.get(i));
    46             }
    47             else if(i % 3==0){
    48                 p1.add(list.get(i));
    49             }
    50             else if(i % 3==1){
    51                 p2.add(list.get(i));
    52             }
    53             else if(i % 3==2){
    54                 p3.add(list.get(i));
    55             }
    56         }
    57         //看牌
    58         lookPork("p1", p1, map);
    59         lookPork("p2", p2, map);
    60         lookPork("p3", p3, map);
    61         lookPork("dipai", dipai, map);
    62         
    63     }
    64     //对人的牌进行遍历
    65     public static void lookPork(String name,TreeSet<Integer> ts,HashMap<Integer, String> map){
    66         System.out.println(name+"的手牌是:");
    67         for (Integer key : ts) {
    68             System.out.print(map.get(key)+" ");
    69         }
    70         System.out.println();
    71     }
    72 }
  • 相关阅读:
    java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header.
    spring-session-data-redis依赖冲突问题
    centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod
    图片上传后台服务报内存溢出 Out Of Memory Java heap space
    mysql 数据库密码忘记重置 进行远程连接
    打Jar包
    Type interface com.innovationV2.mapper.UserMapper is not known to the MapperRegistry
    关于java基础类型Integer String的clone()
    clion使用clang编译
    token & refresh token 机制总结
  • 原文地址:https://www.cnblogs.com/yihaifutai/p/6754353.html
Copyright © 2011-2022 走看看