zoukankan      html  css  js  c++  java
  • java 18

      1 /*
      2      模拟斗地主的发牌功能(发牌完毕后发到手上的牌是有顺序的)
      3      分析:    
      4          A:创建一个HashMap集合
      5          B:创建一个ArrayList集合
      6          C:创建两个字符串,一个是花色,一个是牌的数字      (发牌得有一盒牌)
      7              为了方便以后的排序,创建这两个字符串的时候,按照大小顺序排列(斗地主中的大小顺序)
      8              大小王除外
      9          D:把这两个字符串放进HashMap集合中(拼接一起 花色+牌号)    同时给每个放进去牌进行编码0--52并存储
     10                 同时也给ArrayList集合中存储编码,大小王在这些编码完成后再进行编码 53 , 54
     11          E:洗牌
     12          F:发牌,其实发的是编号,由于要排序,所以创建TreeSet集合,斗地主是3人和底牌3张,对应4个集合,
     13              a:利用取模的方法给3个人发牌,x%3=0;x%3=1;x%3=2
     14              b:底牌发开始的3张
     15          G:看牌,因为要使用3次这个功能,所以设定一个方法:
     16              a:返回类型:void
     17              b:参数列表:String name(玩家名字) ; TreeSet<Integer> i(牌的编号) ; HashMap<Integer,String> hm(牌)
     18  */
     19 
     20 package zl_MapDemo;
     21 
     22 import java.util.ArrayList;
     23 import java.util.Collections;
     24 import java.util.HashMap;
     25 import java.util.TreeSet;
     26 
     27 public class PokerDemo {
     28     public static void main(String[] args) {
     29         
     30         //创建一个HashMap集合
     31         HashMap<Integer , String> poker = new HashMap<Integer , String>();
     32         
     33         //创建一个ArrayList集合
     34         ArrayList<Integer> index = new ArrayList<Integer>();
     35         
     36         //创建两个字符串,一个是花色,一个是牌的数字      (发牌得有一盒牌)
     37         String[] colors = {"♦","♣","♥","♠"};
     38         
     39         String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
     40                 "K", "A", "2", };
     41         //把这两个字符串放进HashMap集合中(拼接一起 花色+牌号)同时给每个放进去牌进行编码0--52并存储
     42         int count = 0;
     43         for(String number : numbers){
     44             for(String color : colors){
     45                 //拼接
     46                 String pk = color.concat(number);
     47                 //把编码和牌存储到HashMap中
     48                 poker.put(count, pk);
     49                 
     50                 //把编码存到ArrayList中
     51                 index.add(count);
     52                 
     53                 count ++;
     54             }
     55         }
     56         //给大小王编码并存储
     57         poker.put(count, "小王");
     58         index.add(count);
     59         count ++;
     60         poker.put(count, "大王");
     61         index.add(count);
     62         
     63         //洗牌
     64         Collections.shuffle(index);
     65         
     66         //发牌,由于要排序,所以创建TreeSet集合,斗地主是3人和底牌3张,对应4个集合,
     67         TreeSet<Integer> player1 = new TreeSet<Integer>();
     68         TreeSet<Integer> player2 = new TreeSet<Integer>();
     69         TreeSet<Integer> player3 = new TreeSet<Integer>();
     70         TreeSet<Integer> dipai = new TreeSet<Integer>();
     71         
     72         //a:利用取模的方法给3个人发牌,x%3=0;x%3=1;x%3=2    b:底牌剩下的3张
     73         //此时ArrayList中已经存储了0-54的编码
     74         for(int x = 0 ;x < index.size() ; x ++){
     75         if(x >= index.size() - 3){    
     76             dipai.add(index.get(x));//给底牌发ArrayList前3个编号,不能dipai.add(x),这样给底牌的是0,1,2
     77         }
     78         else if(x % 3 == 0){
     79             player1.add(index.get(x));
     80         }
     81         else if(x % 3 == 1){
     82             player2.add(index.get(x));
     83         }
     84         else if(x % 3 == 2){
     85             player3.add(index.get(x));
     86         }
     87     }
     88         
     89         //看牌
     90             lookpoker("玩家1", player1, poker);
     91             lookpoker("玩家2", player2, poker);
     92             lookpoker("玩家3", player3, poker);
     93             lookpoker("底牌", dipai, poker);
     94     }
     95     
     96         //创建一个看牌的方法,a:返回类型:void    
     97         //b:参数列表:String name(玩家名字) ; TreeSet<Integer> i(牌的编号) ; HashMap<Integer,String> hm(牌)
     98     public static void lookpoker(String name,TreeSet<Integer> i ,HashMap<Integer,String> hm) {
     99             //首先是名字
    100         System.out.print(name+"的牌是:");
    101             //其次是牌,遍历
    102         for(Integer key : i){
    103             //TreeSet的值就是HashMap的键,所以可以得到对应的值,也就是牌
    104             String result = hm.get(key);
    105             //输出看到的牌
    106             System.out.print(result+"  ");
    107         }
    108         System.out.println(" ");
    109 
    110     }
    111 
    112 }
    何事都只需坚持.. 难? 维熟尔。 LZL的自学历程...只需坚持
  • 相关阅读:
    openstack 杂记 备忘
    centos7 开机/etc/rc.local 不执行的问题
    Rabbit-service Message queue MQ 验证 校验
    MySQL max_connections 总是 214 。不能设大了? max_connections = 214
    Mariadb-lib
    SQL service 自动解决依赖包 验证
    SQL service
    createrepo
    mkisofs
    【Linux探索之旅】第四部分第三课:文件传输,潇洒同步
  • 原文地址:https://www.cnblogs.com/LZL-student/p/5910150.html
Copyright © 2011-2022 走看看