1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| package demo03;
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap;
public class PokerGame01 { static ArrayList<String> list = new ArrayList<>();
static HashMap<String, Integer> hm = new HashMap<>();
static { String[] color = {"♦", "♣", "♥", "♠"}; String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
for (String c : color) { for (String n : number) { list.add(c + n); } } list.add(" 小王"); list.add(" 大王");
hm.put("J", 11); hm.put("Q", 12); hm.put("K", 13); hm.put("A", 14); hm.put("2", 15); hm.put("小王", 50); hm.put("大王", 100);
}
public PokerGame01() { Collections.shuffle(list);
ArrayList<String> lord = new ArrayList<>(); ArrayList<String> player1 = new ArrayList<>(); ArrayList<String> player2 = new ArrayList<>(); ArrayList<String> player3 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) { String poker = list.get(i); if (i <= 2) { lord.add(poker); continue; }
if (i % 3 == 0) { player1.add(poker); } else if (i % 3 == 1) { player2.add(poker); } else { player3.add(poker); } }
order(lord); order(player1); order(player2); order(player3);
lookPoker("底牌", lord); lookPoker("钢脑壳", player1); lookPoker("大帅比", player2); lookPoker("蛋筒", player3); }
public void lookPoker(String name, ArrayList<String> list) { System.out.print(name + ": "); for (String poker : list) { System.out.print(poker + " "); } System.out.println();
}
public void order(ArrayList<String> list) { Collections.sort(list, (o1, o2) -> {
String color1 = o1.substring(0, 1); int value1 = getValue(o1);
String color2 = o2.substring(0, 1); int value2 = getValue(o2);
int i = value1 - value2; return i == 0 ? color1.compareTo(color2) : i;
}); }
public int getValue(String poker) { String number = poker.substring(1); if (hm.containsKey(number)) { return hm.get(number); } else { return Integer.parseInt(number); } } }
|