public class Demo_Fight { public static void main(String[] args) { // TODO Auto-generated method stub List<String> colors =new ArrayList<>(); Collections.addAll(colors, "♦","♥","♠","♣"); List<String> numbers = new ArrayList<>(); Collections.addAll(numbers, "2","A","K","Q","J","10","9","8","7","6","5","4","3"); //创建一个List集合,存储牌的索引 ArrayList<Integer> pokerIndex = new ArrayList<>(); Map<Integer,String> map = new HashMap<>() ; int index=0; pokerIndex.add(index); map.put(index++, "大王"); pokerIndex.add(index); map.put(index++, "小王"); for(String num:numbers) { for(String s:colors) { pokerIndex.add(index); map.put(index++, s+num); } } //System.out.println(map); //System.out.println(pokerIndex); //根据牌的索引来洗牌 Collections.shuffle(pokerIndex); //发牌 ArrayList<Integer> deep = new ArrayList<>(); ArrayList<Integer> player1 = new ArrayList<>(); ArrayList<Integer> player2 = new ArrayList<>(); ArrayList<Integer> player3 = new ArrayList<>(); for(int i=0;i<54;i++) { if(i>=51) { deep.add(pokerIndex.get(i)); } else if(i%3==0) { player1.add(pokerIndex.get(i)); } else if(i%3==1) { player2.add(pokerIndex.get(i)); } else if(i%3==2) { player3.add(pokerIndex.get(i)); } } //System.out.println(player1); //分别排序 Collections.sort(deep); Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); //System.out.println(player1); //获取牌索引对应的牌 lookPoker("小明",player1,map); lookPoker("小光",player2,map); lookPoker("小强",player3,map); lookPoker("底牌",deep,map); } public static void lookPoker(String name,List<Integer> player,Map<Integer,String> map) { System.out.print(name+"的牌是:"); for(int i=0;i<player.size();i++) { System.out.print(map.get(player.get(i))+" "); } System.out.println(); }
原文:https://www.cnblogs.com/cocobear9/p/12864216.html