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(); } }
原文:https://www.cnblogs.com/jiangdi135792/p/13887672.html