ArrayList版
package com.yusian.poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class PokerDemo {
public static void main(String[] args) {
String[] colors = {"♠️", "♥️", "♣️", "♦️"};
String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
ArrayList<String> poker = new ArrayList<>();
poker.add("大王");
poker.add("小王");
for (String color : colors) {
for (String number : numbers) {
poker.add(number + color);
}
}
System.out.println(poker);
// 打乱顺序
Collections.shuffle(poker);
System.out.println(poker);
ArrayList<String> player1 = new ArrayList<>();
ArrayList<String> player2 = new ArrayList<>();
ArrayList<String> player3 = new ArrayList<>();
ArrayList<String> remain = new ArrayList<>();
for (int i = 0; i < poker.size(); i++) {
String p = poker.get(i);
if (i >= 51) {
remain.add(p);
} else if (i % 3 == 0) {
player1.add(p);
} else if (i % 3 == 1) {
player2.add(p);
} else {
player3.add(p);
}
}
// 排序
player1.sort(Comparator.comparingInt((String o) -> o.charAt(0)));
player2.sort(Comparator.comparingInt((String o) -> o.charAt(0)));
player3.sort(Comparator.comparingInt((String o) -> o.charAt(0)));
System.out.println("------------------");
System.out.println("player1:" + player1);
System.out.println("player2:" + player2);
System.out.println("player3:" + player3);
System.out.println("底牌:" + remain);
}
}
[大王, 小王, 2♠️, A♠️, K♠️, Q♠️, J♠️, 10♠️, 9♠️, 8♠️, 7♠️, 6♠️, 5♠️, 4♠️, 3♠️, 2♥️, A♥️, K♥️, Q♥️, J♥️, 10♥️, 9♥️, 8♥️, 7♥️, 6♥️, 5♥️, 4♥️, 3♥️, 2♣️, A♣️, K♣️, Q♣️, J♣️, 10♣️, 9♣️, 8♣️, 7♣️, 6♣️, 5♣️, 4♣️, 3♣️, 2♦️, A♦️, K♦️, Q♦️, J♦️, 10♦️, 9♦️, 8♦️, 7♦️, 6♦️, 5♦️, 4♦️, 3♦️]
[2♠️, J♣️, 4♦️, 5♠️, 9♠️, Q♠️, 6♠️, 7♠️, Q♦️, 3♦️, 2♣️, 4♣️, 10♦️, 9♦️, 3♥️, 7♦️, A♥️, 3♠️, 5♥️, K♥️, 9♥️, 8♠️, K♦️, A♦️, 5♣️, J♥️, K♠️, 2♦️, 6♣️, A♣️, 8♦️, 6♥️, 9♣️, 4♠️, 10♥️, 10♣️, 8♥️, 大王, J♠️, 5♦️, A♠️, Q♥️, 6♦️, 4♥️, 8♣️, 3♣️, K♣️, 2♥️, Q♣️, 7♥️, 10♠️, 7♣️, J♦️, 小王]
------------------
player1:[10♦️, 2♠️, 2♦️, 3♦️, 3♣️, 4♠️, 5♠️, 5♥️, 5♣️, 5♦️, 6♠️, 6♦️, 7♦️, 8♠️, 8♦️, 8♥️, Q♣️]
player2:[10♥️, 2♣️, 4♥️, 6♣️, 6♥️, 7♠️, 7♥️, 9♠️, 9♦️, A♥️, A♠️, J♣️, J♥️, K♥️, K♦️, K♣️, 大王]
player3:[10♣️, 10♠️, 2♥️, 3♥️, 3♠️, 4♦️, 4♣️, 8♣️, 9♥️, 9♣️, A♦️, A♣️, J♠️, K♠️, Q♠️, Q♦️, Q♥️]
底牌:[7♣️, J♦️, 小王]
HashMap版
private static void demo02() {
List<String> colors = List.of("♠️", "♥️", "♣️", "♦️");
List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
LinkedHashMap<Integer, String> poker = new LinkedHashMap<>();
poker.put(0, "大王");
poker.put(1, "小王");
for (int i = 0; i < numbers.size(); i++) {
for (int i1 = 0; i1 < colors.size(); i1++) {
Integer index = (i + 1) * colors.size() + i1;
String value = numbers.get(i) + colors.get(i1);
poker.put(index, value);
}
}
// 输出所有的牌
System.out.println(poker);
// 输出牌索引
ArrayList<Integer> pokerIndex = new ArrayList<>(poker.keySet());
System.out.println(pokerIndex);
// 打乱索引值
Collections.shuffle(pokerIndex);
System.out.println(pokerIndex);
// 发牌
ArrayList<Integer> remain = new ArrayList<>();
ArrayList<Integer> player1 = new ArrayList<>();
ArrayList<Integer> player2 = new ArrayList<>();
ArrayList<Integer> player3 = new ArrayList<>();
for (int i = 0; i < pokerIndex.size(); i++) {
Integer index = pokerIndex.get(i);
if (i > 50) {
remain.add(index);
} else if (i % 3 == 0) {
player1.add(index);
} else if (i % 3 == 1) {
player2.add(index);
} else if (i % 3 == 2) {
player3.add(index);
}
}
System.out.println("-------------");
watchPoker("玩家一", player1, poker);
watchPoker("玩家二", player2, poker);
watchPoker("玩家三", player3, poker);
watchPoker("底牌", remain, poker);
}
/**
* 看牌
* @param player 玩家
* @param poker 牌
*/
private static void watchPoker(String name, ArrayList<Integer> player, HashMap poker) {
Collections.sort(player);
System.out.print(name + ": [");
for (int i = 0; i < player.size(); i++) {
System.out.print(poker.get(player.get(i)));
if (i == player.size() - 1) {
System.out.println("]");
} else {
System.out.print(", ");
}
}
}
{0=大王, 1=小王, 4=2♠️, 5=2♥️, 6=2♣️, 7=2♦️, 8=A♠️, 9=A♥️, 10=A♣️, 11=A♦️, 12=K♠️, 13=K♥️, 14=K♣️, 15=K♦️, 16=Q♠️, 17=Q♥️, 18=Q♣️, 19=Q♦️, 20=J♠️, 21=J♥️, 22=J♣️, 23=J♦️, 24=10♠️, 25=10♥️, 26=10♣️, 27=10♦️, 28=9♠️, 29=9♥️, 30=9♣️, 31=9♦️, 32=8♠️, 33=8♥️, 34=8♣️, 35=8♦️, 36=7♠️, 37=7♥️, 38=7♣️, 39=7♦️, 40=6♠️, 41=6♥️, 42=6♣️, 43=6♦️, 44=5♠️, 45=5♥️, 46=5♣️, 47=5♦️, 48=4♠️, 49=4♥️, 50=4♣️, 51=4♦️, 52=3♠️, 53=3♥️, 54=3♣️, 55=3♦️}
[0, 1, 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]
[0, 36, 9, 27, 42, 6, 29, 47, 54, 52, 5, 41, 19, 38, 1, 45, 14, 25, 48, 39, 22, 11, 13, 18, 15, 8, 28, 23, 7, 33, 16, 34, 44, 4, 17, 53, 10, 37, 12, 26, 50, 30, 32, 31, 24, 20, 21, 49, 35, 55, 43, 46, 40, 51]
-------------
玩家一: [大王, 2♠️, A♣️, A♦️, K♦️, Q♠️, Q♦️, J♠️, J♦️, 10♣️, 10♦️, 9♥️, 8♠️, 8♦️, 5♥️, 4♠️, 3♠️]
玩家二: [2♥️, 2♦️, A♠️, K♥️, K♣️, Q♥️, J♥️, 9♦️, 8♣️, 7♠️, 7♥️, 7♣️, 7♦️, 6♣️, 5♦️, 4♣️, 3♦️]
玩家三: [小王, 2♣️, A♥️, K♠️, Q♣️, J♣️, 10♠️, 10♥️, 9♠️, 9♣️, 8♥️, 6♥️, 6♦️, 5♠️, 4♥️, 3♥️, 3♣️]
底牌: [6♠️, 5♣️, 4♦️]