public class Demo {
public static void main(String[] args) {
String[] color = { "黑桃", "红桃", "梅花", "方块" };
String[] number = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" };
ArrayList<String> array = new ArrayList<String>();
array.add("大王");
array.add("小王");
for (String c : color) {
for (String n : number) {
array.add(c.concat(n));
}
}
Collections.shuffle(array);
// System.out.println(array);
ArrayList<String> player1 = new ArrayList<String>();
ArrayList<String> player2 = new ArrayList<String>();
ArrayList<String> player3 = new ArrayList<String>();
for (int i = 0; i < array.size()-3; i++) {
if (i%3 == 0) {
player1.add(array.get(i));
}
if (i%3 == 1) {
player2.add(array.get(i));
}
if (i%3 == 2) {
player3.add(array.get(i));
}
}
Collections.sort(player1, new MyComparator());
Collections.sort(player2, new MyComparator());
Collections.sort(player3, new MyComparator());
System.out.println("player1"+player1);
System.out.println("player1"+player2);
System.out.println("player1"+player3);
for (int i = array.size()-3; i < array.size(); i++) {
System.out.print(array.get(i)+",");
}
}
}
public class MyComparator implements Comparator<String>{
public int compare(String s1, String s2) {
// 截取花色
String c1 = s1.substring(0, 2);
String c2 = s2.substring(0, 2);
// 截取点数
String n1 = s1.substring(2);
String n2 = s2.substring(2);
int num1 = StringToInt(c2, n2) - StringToInt(c1, n1);
int num2 = num1 == 0 ? c2.compareTo(c1) : num1;
return num2;
}
public static int StringToInt(String c, String n) {
int rut = 0;
if (c.equals("大王")) {
return 17;
}
if (c.equals("小王")) {
return 16;
}
if (n != null) {
switch (n) {
case "2":
rut = 15;
break;
case "A":
rut = 14;
break;
case "J":
rut = 11;
break;
case "Q":
rut = 12;
break;
case "K":
rut = 13;
break;
default:
rut = Integer.parseInt(n);
break;
}
}
return rut;
}
}
|
|