```java
package DemoCase.YouXuDouDiZhu;
import wangjiabao.demo5.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class Doumain {
public static void main(String[] args) {
//第一步:定义扑克的基本要素
HashMap<Integer,String> poker =new HashMap<>();
ArrayList<Integer> pokerindex=new ArrayList<>();
List<String> colors = List.of("♥", "♠", "♦", "♣");
List<String> numbers = List.of("2","A","K","Q","J","10","9","8","7","6","5","4","3");
int index=0;
poker.put(index,"大王");
pokerindex.add(index);
index++;
poker.put(index,"小王");
pokerindex.add(index);
index++;
for (String color : colors) {
for (String number : numbers) {
poker.put(index,color+number);
pokerindex.add(index);
index++;
}
}
//第二步:打乱牌的顺序
Collections.shuffle(pokerindex);
//第三步:发牌
ArrayList<Integer> player1=new ArrayList<>();
ArrayList<Integer> player2=new ArrayList<>();
ArrayList<Integer> player3=new ArrayList<>();
ArrayList<Integer> dipai=new ArrayList<>();
for (int i = 0; i <pokerindex.size() ; i++) {
Integer in=pokerindex.get(i);
if(i>=51){
dipai.add(in);
}else if(i%3==0){
player1.add(in);
}else if(i%3==1){
player2.add(in);
}else if(i%3==2){
player3.add(in);
}
}
//第四步:排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
Collections.sort(dipai);
//看牌
fa("周润发",poker,player1);
fa("梁朝伟",poker,player2);
fa("周星驰",poker,player3);
fa("底牌是",poker,dipai);
}
//为了代码复用,定义看牌的方法
public static void fa(String name,HashMap<Integer,String> ha,ArrayList<Integer> in){
System.out.print(name+"的手牌是:");
for (int i = 0; i <in.size() ; i++) {
Integer t=in.get(i);
System.out.print(ha.get(t)+" ");
}
System.out.println();
}
}
在这里插入代码片
运行图如下: