zoukankan      html  css  js  c++  java
  • java之斗地主

    hashmap集合+ArrayList集合+Collections集合工具类shuffle()和sort()
    hashmap中get(key)、put(key/value)
    Arraylist中的add()、get(下标值),此时ArrayLIST集合相当于hashmap中的set集合,可以使用迭代器或者增强for遍历内容
    或者不使用Arraylist,直接使用entryset得到Map.entry<E>,使用getkey和getvalue得到key值
    1
    package com.oracle.demo01; 2 import java.util.ArrayList; 3 import java.util.Collections; 4 import java.util.HashMap; 5 6 public class Doudizhu { 7 public static void main(String[] args) { 8 //定义扑克牌Map 默认按照key值从小到大排序 9 HashMap<Integer,String> pooker=new HashMap<Integer,String>(); 10 //定义装有扑克牌号的集合 单独的与map集合数字一致2 11 ArrayList<Integer> pookerNum=new ArrayList<Integer>(); 12 //封装数据 4种花色(13个牌)+大小王 13 String[] color={"","","",""};//点击选择utf-8保存 14 String[] number={"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; 15 //嵌套for循环,先数字,后花色 16 int index=2; 17 for(String n:number){ 18 for(String c:color){ 19 pooker.put(index,c+n); 20 pookerNum.add(index); 21 index++; 22 } 23 } 24 //封装大小王 25 pooker.put(0, "大王"); 26 pooker.put(1, "小王"); 27 pookerNum.add(0); 28 pookerNum.add(1); 29 30 //测试 31 // System.out.println(pooker); 32 //洗牌 33 Collections.shuffle(pookerNum); 34 //创建四个容器 35 ArrayList<Integer> player1=new ArrayList<Integer>(); 36 ArrayList<Integer> player2=new ArrayList<Integer>(); 37 ArrayList<Integer> player3=new ArrayList<Integer>(); 38 ArrayList<Integer> bottom=new ArrayList<Integer>(); 39 for(int i=0;i<pookerNum.size();i++){ 40 if(i<3){ 41 bottom.add(pookerNum.get(i)); 42 }else if(i%3==0){ 43 player1.add(pookerNum.get(i)); 44 }else if(i%3==1){ 45 player2.add(pookerNum.get(i)); 46 }else if(i%3==2){ 47 player3.add(pookerNum.get(i)); 48 } 49 } 50 //排序 51 Collections.sort(player1); 52 Collections.sort(player2); 53 Collections.sort(player3); 54 Collections.sort(bottom); 55 //遍历看牌 56 System.out.println(); 57 look("1",pooker,player1); 58 look("2",pooker,player2); 59 look("3",pooker,player3); 60 look("底牌",pooker,bottom); 61 } 62 //通用的方法 63 public static void look(String Name,HashMap<Integer,String> pooker, 64 ArrayList<Integer> bottom){ 65 System.out.print(Name+":"); 66 for(Integer number:bottom){ 67 System.out.print(pooker.get(number)+" "); 68 } 69 System.out.println(); 70 } 71 }
     1 package com.oracle.demo01;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 import java.util.HashMap;
     6 import java.util.Iterator;
     7 
     8 public class DouDiZhu3 {
     9 public static void main(String[] args) {
    10     //定义map集合
    11     HashMap<Integer,String> big=
    12             new HashMap<Integer,String>();
    13     ArrayList<Integer> arr=new ArrayList<Integer>();
    14     //封装数据,通过内外循环,拼接字符串
    15     String[] arr1={"","","",""};
    16     String[] arr2={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
    17     //拼接字符串,并存入map集合中
    18     int index=2;
    19     for(String r2:arr2){
    20         for(String r1:arr1){
    21             //必须创建不同的对象,进行存储
    22             arr.add(index);
    23             //System.out.println(arr);
    24             big.put(index, r1+r2);
    25         /*    //对arr进行整体的清空
    26             arr.clear();*/
    27             index++;
    28         }
    29     }
    30     //封装大小王  
    31     big.put(0, "大王");
    32     big.put(1, "小王");
    33     arr.add(0);
    34     arr.add(1);
    35     //此时为一个对象,会出现值覆盖问题
    36     /*big.put(new ArrayList<Integer>(0), "大王");
    37     big.put(new ArrayList<Integer>(1), "小王");*/
    38     //测试是否存储成功,map自己调用tostring()
    39     System.out.print(big);
    40      //洗牌
    41     Collections.shuffle(arr);
    42     //创建四个容器
    43     ArrayList<Integer>  player1=new  ArrayList<Integer>();
    44     ArrayList<Integer>  player2=new  ArrayList<Integer>();
    45     ArrayList<Integer>  player3=new  ArrayList<Integer>();
    46     ArrayList<Integer>  bottom=new  ArrayList<Integer>();
    47     for(int i=0;i<arr.size();i++){
    48      if(i<3){
    49       bottom.add(arr.get(i));
    50      }else if(i%3==0){
    51       player1.add(arr.get(i));
    52      }else if(i%3==1){
    53       player2.add(arr.get(i));
    54      }else if(i%3==2){
    55       player3.add(arr.get(i));
    56      }
    57     }
    58     //排序
    59     Collections.sort(player1);
    60     Collections.sort(player2);
    61     Collections.sort(player3);   
    62     Collections.sort(bottom);
    63     //遍历看牌
    64     System.out.println();
    65     look("1",big,player1);
    66     look("2",big,player2);
    67     look("3",big,player3);
    68     look("底牌",big,bottom);
    69    }
    70    //通用的方法
    71    public static void look(String Name,HashMap<Integer,String>  big,
    72      ArrayList<Integer>  bottom){
    73     System.out.print(Name+":");
    74    //创建迭代器对象  数据类型与要遍历的集合数据类型一致
    75     Iterator<Integer> i=bottom.iterator();
    76     while(i.hasNext()){
    77         int ii=i.next();//对象调用
    78         System.out.print(big.get(ii)+"   ");
    79     }
    80    System.out.println();
    81    }
    82 }
  • 相关阅读:
    HDOJ_ACM_统计问题
    HDOJ_ACM_Queuing
    HDOJ_ACM_数塔
    HDOJ_ACM_免费馅饼
    HDOJ_ACM_FatMouse's Speed
    HDOJ_ACM_Monkey and Banana
    斐波南希数列
    .net framework 2.0的WinForm的ShowInTaskBar属性的bug
    寂寞的季节
    广告一下
  • 原文地址:https://www.cnblogs.com/mlf19920916/p/12154994.html
Copyright © 2011-2022 走看看