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

    public class PokerDemo {
    public static void main(String[] args) {
    //1.创建一个HashMap集合
    //2.创建一个ArrayList集合
    //3.创建花色和点数数组
    //4.从0开始向HashMap中添加编号,并同时小ArrayList中添加编号
    //5.洗牌,发牌,看牌
    HashMap<Integer,String> hm=new HashMap<Integer,String>();
    ArrayList<Integer> array=new ArrayList<Integer>();
    String[] colors={"♠","♥","♣","♦"};
    String[] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

    int index=0;

    for(String number:numbers){
    for(String color:colors){
    String poker=color.concat(number);
    hm.put(index, poker);
    array.add(index);
    index++;
    }
    }
    hm.put(index,"大王");
    array.add(index);
    index++;
    hm.put(index,"小王");
    array.add(index);

    Collections.shuffle(array);

    TreeSet<Integer> zhangsan=new TreeSet<Integer>();
    TreeSet<Integer> lisi=new TreeSet<Integer>();
    TreeSet<Integer> wangwu=new TreeSet<Integer>();
    TreeSet<Integer> remain=new TreeSet<Integer>();

    for(int x=0;x<array.size();x++){
    if(x>=array.size()-3){
    remain.add(array.get(x));
    }else if(x%3==0){
    zhangsan.add(array.get(x));
    }else if(x%3==1){
    lisi.add(x);
    }else if(x%3==2){
    wangwu.add(array.get(x));
    }
    }

    lookPoker("张三",zhangsan,hm);
    lookPoker("李四",lisi,hm);
    lookPoker("王五",wangwu,hm);
    lookPoker("底牌",remain,hm);
    }

    public static void lookPoker(String name,TreeSet<Integer>ts,
    HashMap<Integer,String> hm){
    System.out.print(name+"的牌:");
    for(Integer key:ts){
    String value=hm.get(key);
    System.out.print(value+" ");
    }
    System.out.println();
    }
    }

  • 相关阅读:
    JavaScript中双叹号“!!”作用
    JavaScript两个变量的值交换的多种方式
    自定义npm包——typeScript版本
    自定义npm包的创建、发布、更新和撤销
    vuex概念总结及简单使用实例
    详解javascript: void(0);
    面向对象的CSS
    vue指令概览
    实现水平居中的办法
    C#分块下载文件
  • 原文地址:https://www.cnblogs.com/liumin-txgt/p/13404713.html
Copyright © 2011-2022 走看看