首页 > 其他 > 详细

斗地主JDK1.9中Map集合of用法

时间:2020-10-27 23:06:51      阅读:48      评论:0      收藏:0      [点我收藏+]
package cn.edu.ccut.four;

import java.util.*;

public class DouDiZhu {
    public static void main(String[] args) {
        //1、准备牌
        //创建一个Map集合,存储索引和组装好牌
        HashMap<Integer,String> puke=new HashMap<>();
        //创建list集合存储索引
        ArrayList<Integer> pukeIndex= new ArrayList<>();
        //定义两个数组,一个数组存储牌的花色,一个数组存储牌的序号
       List<String> colors= List.of("?","?","?","?");
       List<String> numbers = List.of("2","A","K","Q","J","10","9","8","7","6","5","4","3");
       //大王小王存入
        int index = 0;
        puke.put(index,"大王");
        index++;
        puke.put(index,"小王");
        pukeIndex.add(index);
        index++;
        for (String number : numbers) {
            for (String color : colors) {
                puke.put(index,color+number);
                pukeIndex.add(index);
                index++;
            }
        }
        /*System.out.println(puke);
        System.out.println(pukeIndex);*/
        Collections.shuffle(pukeIndex);
        //发牌
        ArrayList<Integer> player01 = new ArrayList<>();
        ArrayList<Integer> player02 = new ArrayList<>();
        ArrayList<Integer> player03 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();
        for (int i = 0; i < pukeIndex.size(); i++) {
            Integer in = pukeIndex.get(i);
            if (i>=51){
                dipai.add(in);
            }else if(i%3==0){
                player01.add(in);
            }else if(i%3==1){
                player02.add(in);
            }else if(i%3==2){
                player03.add(in);
            }

        }
        //排序
        Collections.sort(player01);
        Collections.sort(player02);
        Collections.sort(player03);
        Collections.sort(dipai);
        //看牌
        lookpuke("张飞",puke,player01);
        lookpuke("关羽",puke,player02);
        lookpuke("刘备",puke,player03);
        lookpuke("底牌",puke,dipai);
    }
    //看牌,提高代码复用性
    public static void lookpuke(String name,HashMap<Integer,String>puke,ArrayList<Integer> list){
        System.out.println(name+":");
        for (Integer key : list) {
            String value = puke.get(key);
            System.out.println(value+"");

        }
        System.out.println();
    }
}

 

斗地主JDK1.9中Map集合of用法

原文:https://www.cnblogs.com/jiangdi135792/p/13887672.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!