• 模拟斗地主发牌


    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();
    }
    }

  • 相关阅读:
    基于发布/订阅模型的应用程序的主循环设计
    C++使用继承时子对象的内存布局
    安装 CentOS 后的系统配置及软件安装备忘
    环形无锁队列
    并发编程基础
    线程池实现
    Git远程操作
    Unix权限管理
    jquery中,某些写法后来更新导致版本不支持的替代方法
    js相关
  • 原文地址:https://www.cnblogs.com/liumin-txgt/p/13404713.html
Copyright © 2020-2023  润新知