package poker.bean;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.lang.annotation.ElementType;
import java.util.*;
/**
* @Author:Liku
* @Date:2022/11/26-14:05
* @Name:JavaSE
* @Since:jdk1.8
* @Description:
*/
@Setter
@Getter
public class Poker {
private Poker() {
}
public static void allPoker() {
String[] color = {"♠", "♥", "♦", "♣"};
String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
Map<Integer, String> pokers = new HashMap<>();
List<Integer> keyList = new ArrayList<>();
int index = 0;
//外层循环数字,内层循环花色,这样能保证k值跟牌面值相匹配
for (String s : num) {//外层循环数字
for (String s1 : color) {//内层加上颜色
pokers.put(index, s1 + s);
keyList.add(index++);
}
}
pokers.put(index, "♚");
keyList.add(index++);
pokers.put(index, "♔");
keyList.add(index);
Collections.shuffle(keyList);//打乱扑克牌下标序号,并且分配每个玩家的key值
List<Integer> player1 = new ArrayList<>(17);
List<Integer> player2 = new ArrayList<>(17);
List<Integer> player3 = new ArrayList<>(17);
List<Integer> dzPlay = new ArrayList<>(3);
//给每个人发牌
for (int i = 0; i < keyList.size(); i++) {
if (i >= keyList.size() - 3) {
dzPlay.add(keyList.get(i));
} else if (i % 3 == 1) player1.add(keyList.get(i));
else if (i % 3 == 2) player2.add(keyList.get(i));
else if (i % 3 == 0) player3.add(keyList.get(i));
}
//将各位玩家的手牌排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
Collections.sort(dzPlay);
//创建玩家的list来装牌
List<String> poker1 = new ArrayList<>();
List<String> poker2 = new ArrayList<>();
List<String> poker3 = new ArrayList<>();
List<String> dzPoker = new ArrayList<>();
//排完序根据key值,将扑克牌的value给玩家
for (Integer integer : player1) {
poker1.add(pokers.get(integer));
}
for (Integer integer : player2) {
poker2.add(pokers.get(integer));
}
for (Integer integer : player3) {
poker3.add(pokers.get(integer));
}
for (Integer integer : dzPlay) {
dzPoker.add(pokers.get(integer));
}
System.out.println(poker1);
System.out.println(poker2);
System.out.println(poker3);
System.out.println(dzPoker);
}
public static void main(String[] args) {
allPoker();
}
}
结果展示:
标签:pokers,java,扑克牌,ArrayList,List,add,new,keyList,发牌 From: https://www.cnblogs.com/Liku-java/p/16927686.html