模拟斗地主
public class M1 { public static void main(String args[]) { DouDiZhu02(); } private static void DouDiZhu02() { HashMap<Integer, String> hashMap = new HashMap<>(); ArrayList<Integer> array = new ArrayList<>(); String[] colors = {"♥", "♠", "♣", "?"}; String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}; int index = 0; for (String number : numbers) { for (String color : colors) { hashMap.put(index, color + number); array.add(index); index++; } } hashMap.put(index, "小王"); array.add(index); index++; hashMap.put(index, "大王"); array.add(index); Collections.shuffle(array); TreeSet<Integer> set0 = new TreeSet<>(); TreeSet<Integer> set1 = new TreeSet<>(); TreeSet<Integer> set2 = new TreeSet<>(); TreeSet<Integer> set9 = new TreeSet<>(); for (int i = 0; i < array.size(); i++) { int x = array.get(i); if (i >= array.size() - 3) { set9.add(x); } else if (i % 3 == 0) { set0.add(x); } else if (i % 3 == 1) { set1.add(x); } else if (i % 3 == 2) { set2.add(x); } } lookPoker2("0 ", set0, hashMap); lookPoker2("1 ", set1, hashMap); lookPoker2("2 ", set2, hashMap); lookPoker2("底牌 ", set9, hashMap); } private static void lookPoker2(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) { System.out.print(name + "的牌是:"); for (Integer key : ts) { String poker = hm.get(key); System.out.print(poker + " "); } System.out.println(); } private static void DouDiZhu01() { ArrayList<String> array = new ArrayList<>(); String[] colors = {"♥", "♠", "♣", "?"}; String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; for (String color : colors) { for (String number : numbers) { array.add(color + number); } } array.add("小王"); array.add("大王"); Collections.shuffle(array); //System.out.println(array); ArrayList<String> array0 = new ArrayList<>(); ArrayList<String> array1 = new ArrayList<>(); ArrayList<String> array2 = new ArrayList<>(); ArrayList<String> arrayPoker = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { String poker = array.get(i); if (i >= array.size() - 3) { arrayPoker.add(poker); } else if (i % 3 == 0) { array0.add(poker); } else if (i % 3 == 1) { array1.add(poker); } else if (i % 3 == 2) { array2.add(poker); } } lookPoker1("0 ", array0); lookPoker1("1 ", array1); lookPoker1("2 ", array2); } private static void lookPoker1(String name, ArrayList<String> array) { System.out.println(name + "的牌是:"); for (String poker : array) { System.out.print(poker + " "); } System.out.println(); } }
原文:https://www.cnblogs.com/kikyoqiang/p/11494713.html