通过程序实现斗地主过程中的洗牌,发牌和看牌。要求:对牌进行排序
package com.itheima01; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.TreeSet; public class PokerDemo { public static void main(String[] args) { HashMap<Integer, String> hm = new HashMap<Integer, String>(); ArrayList<Integer> array = new ArrayList<Integer>(); String[] colors = {"♦", "♣", "♥", "♠"}; String[] numbers = {"3", "4", "5", "6", "8", "9", "10", "J", "Q", "K", "A", "2"}; int index = 0; for (String number : numbers) { for (String color : colors) { hm.put(index, color + number); array.add(index); index++; } } hm.put(index, "小王"); array.add(index); index++; hm.put(index, "大王"); array.add(index); Collections.shuffle(array); TreeSet<Integer> lqxSet = new TreeSet<>(); TreeSet<Integer> lySet = new TreeSet<>(); TreeSet<Integer> fqySet = new TreeSet<>(); TreeSet<Integer> dpSet = new TreeSet<>(); for (int i = 0; i < array.size(); i++) { int x = array.get(i); if (i >= array.size() - 3) { dpSet.add(x); } else if (i % 3 == 0) { lqxSet.add(x); } else if (i % 3 == 1) { lySet.add(x); } else if (i % 3 == 2) { fqySet.add(x); } } lookPoker("林青霞", lqxSet, hm); lookPoker("柳岩", lySet, hm); lookPoker("风清扬", fqySet, hm); lookPoker("底牌", dpSet, hm); } public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) { System.out.println(name + "的牌是:"); for (Integer key : ts) { String poker = hm.get(key); System.out.print(poker + " "); } System.out.println(); } }