zoukankan      html  css  js  c++  java
  • 中国传统文化---------斗地主----------

    package Mapdemo;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;


    public class doudizhu {
    public static void main(String[] args) {
    // 组合牌
    //创建Map集合,键是编号,值是牌
    HashMap<Integer,String> pooker = new HashMap<Integer, String>();
    //创建ArrayList集合,存储编号
    ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
    //定义出13个点数的数组
    String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
    //定义梅花 方块 红桃 黑桃 并用 数组保存
    String[] colors = {"♠","♥","♣","♦"};
    //定义整数变量,作为键出现
    int index = 2;
    //遍历数组,花色+点数的组合,存储到Map集合
    for(String number : numbers){
    for(String color : colors){
    pooker.put(index, color+number);
    pookerNumber.add(index);
    index++;
    }
    }
    //存储大王,和小王
    pooker.put(0, "大王");
    pookerNumber.add(0);
    pooker.put(1, "小王");
    pookerNumber.add(1);

    //洗牌,将牌的编号打乱
    Collections.shuffle(pookerNumber);

    //发牌功能,将牌编号,发给玩家集合,底牌集合
    ArrayList<Integer> player1 = new ArrayList<Integer>();
    ArrayList<Integer> player2 = new ArrayList<Integer>();
    ArrayList<Integer> player3 = new ArrayList<Integer>();
    ArrayList<Integer> bottom = new ArrayList<Integer>();

    //发牌采用的是集合索引%3
    for(int i = 0 ; i < pookerNumber.size() ; i++){
    //先将底牌做好
    if(i < 3){
    //存到底牌去
    bottom.add( pookerNumber.get(i));
    //对索引%3判断
    }else if(i % 3 == 0){
    //索引上的编号,发给玩家1
    player1.add( pookerNumber.get(i) );
    }else if( i % 3 == 1){
    //索引上的编号,发给玩家2
    player2.add( pookerNumber.get(i) );
    }else if( i % 3 == 2){
    //索引上的编号,发给玩家3
    player3.add( pookerNumber.get(i) );
    }
    }
    //对玩家手中的编号排序
    Collections.sort(player1);
    Collections.sort(player2);
    Collections.sort(player3);
    //看牌,将玩家手中的编号,到Map集合中查找,根据键找值
    //定义方法实现
    look("张无忌",player1,pooker);
    look("张三丰",player2,pooker);
    look("张学友",player3,pooker);
    look("底牌",bottom,pooker);
    }
    public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker){
    //遍历ArrayList集合,获取元素,作为键,到集合Map中找值
    System.out.print(name+" ");
    for(Integer key : player){
    String value = pooker.get(key);
    System.out.print(value+" ");
    }
    System.out.println();
    }
    }

  • 相关阅读:
    通过PowerShell发送TCP请求
    移动端h5全屏body背景图底部未到底bug
    前端开发工具——utils
    微信公众号网页开发——授权登录,js安全域名,jssdk使用
    移动端开发——移动端遮罩层滚动防穿透body解决方案
    js实现cookie有效期至当次日凌晨
    js获取数组中任意一项
    mysql连接错误,error1251解决方式
    Vue packages version mismatch
    js实现拖动效果
  • 原文地址:https://www.cnblogs.com/Fisherman13/p/10393780.html
Copyright © 2011-2022 走看看