排序版本,技巧是利用索引
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class HelloWorld {
public static void main(String[] args) {
// 1. 准备牌
// 创建一个Map集合, 存储牌的索引和组装好的牌
HashMap<Integer,String> poker = new HashMap<>();
// 存储牌的索引
ArrayList<Integer> pokerIndex = new ArrayList<>();
// 创建一个List集合, 存储牌的索引
List<String> colors = List.of("♥","♠", "♦", "♣");
List<String> numbers = List.of("2","A","J","Q","K","10","9","8","7","6","5","4","3");
int index = 0;
poker.put(index,"大王");
pokerIndex.add(index++);
poker.put(index,"小王");
pokerIndex.add(index++);
// 两重循环组装其他牌
for(String number:numbers){
for(String color:colors){
poker.put(index,color+number);
pokerIndex.add(index++);
}
}
Collections.shuffle(pokerIndex);
ArrayList<Integer> play1 = new ArrayList<>();
ArrayList<Integer> play2 = new ArrayList<>();
ArrayList<Integer> play3 = new ArrayList<>();
ArrayList<Integer> hand = new ArrayList<>();
for(int i=0;i<pokerIndex.size();i++){
Integer p = pokerIndex.get(i);
if(i>=51) hand.add(p);
else if(i%3==1) play1.add(p);
else if(i%3==2) play2.add(p);
else play3.add(p);
}
Collections.sort(play1);
Collections.sort(play2);
Collections.sort(play3);
lookPoker("play1",poker,play1);
lookPoker("play2",poker,play2);
lookPoker("play3",poker,play3);
}
public static void lookPoker(String name, HashMap<Integer,String> poker, ArrayList<Integer> list){
System.out.print(name+": ");
for(Integer key:list){
String value = poker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}