皇帝是:玩家1
皇帝的牌是:[皇上, 小王, 小王, 小王, 小王, 方片2, 黑桃2, 黑桃A, 黑桃A, 红桃A, 方片K, 梅花K, 黑桃K, 红桃K, 梅花Q, 梅花Q, 黑桃Q, 方片J, 方片J, 方片J, 红桃J, 梅花9, 黑桃9, 黑桃9, 方片8, 梅花8, 红桃8, 梅花7, 黑桃7, 黑桃7, 红桃7, 梅花6, 梅花6, 黑桃6, 黑桃6, 方片5, 梅花5, 黑桃5, 黑桃5, 梅花4, 梅花4, 梅花4, 方片3, 红桃3]
侍卫对应的牌是:方片J
侍卫是:玩家2
侍卫的牌是:[方片2, 黑桃2, 红桃2, 方片A, 方片K, 梅花K, 梅花K, 黑桃K, 红桃K, 红桃K, 黑桃Q, 红桃Q, 方片J, 方片10, 黑桃10, 红桃10, 红桃10, 红桃10, 方片9, 红桃9, 方片8, 梅花8, 梅花8, 黑桃8, 黑桃8, 黑桃8, 红桃8, 红桃8, 方片7, 黑桃7, 黑桃7, 方片6, 黑桃6, 黑桃5, 梅花4, 黑桃4, 红桃4, 红桃4, 方片3, 梅花3, 黑桃3, 红桃3, 红桃3]
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Objects;
public class Main {
public static void main(String[] args) {
//选择pokers容器
ArrayList<String> pokers = new ArrayList<>();
//初始化
initPokers(pokers);
//洗牌
Collections.shuffle(pokers);
//发牌
ArrayList<Poker>[] pokersOfGamers = new ArrayList[5];
for (int i = 0; i < 5; i++) {
pokersOfGamers[i] = new ArrayList<>();
}
boolean judgeEnd = true;
int count = 0;
while (judgeEnd){
for (int i = 0; i < 5; i++) {
pokersOfGamers[i].add(new Poker(pokers.get(count++)));
if(count == 216){
judgeEnd = false;
break;
}
}
}
//排序
for (int i = 0; i < 5; i++) {
Collections.sort(pokersOfGamers[i], new PokerColorCompatator());
Collections.sort(pokersOfGamers[i], new PokerNumComparator());
}
//拿牌
Gamer[] gamers = new Gamer[5]; //玩家1~5
for (int i = 0; i < 5; i++) {
gamers[i] = new Gamer(pokersOfGamers[i]);
}
//输出皇帝
int emperorNo = searchPrintEmperor(gamers);
//输出侍卫
Poker[] thePoker = searchTriPoker(gamers[emperorNo].getPokers());
if(thePoker[1].getNo() != "NULL"){
System.out.println("皇帝可以自己做侍卫。");
}
else if(thePoker[0].getNo() != "NULL"){
System.out.print("侍卫对应的牌是:");
System.out.println(thePoker[0].getNo());
System.out.print("侍卫是:玩家");
int guardNo = searchGuard(gamers, emperorNo, thePoker[0]);
System.out.println(guardNo);
System.out.print("侍卫的牌是:");
System.out.println(gamers[guardNo].toString());
}
else{
System.out.println("无法得到侍卫");
}
}
static public void initPokers(ArrayList<String> pokers){
for (int j = 0; j < 4; j++) {
for(int i = 2; i < 11; ++i){
pokers.add("红桃" + i);
pokers.add("黑桃" + i);
pokers.add("方块" + i);
pokers.add("梅花" + i);
}
addspecial("A", pokers);
addspecial("J", pokers);
addspecial("Q", pokers);
addspecial("K", pokers);
}
for (int i = 0; i < 4; i++) {
pokers.add("大王");
pokers.add("小王");
}
//做皇上标记
pokers.set(pokers.indexOf("大王"), "皇上");
}
static public void addspecial(String tmp, ArrayList<String> pokers){
pokers.add("红桃" + tmp);
pokers.add("黑桃" + tmp);
pokers.add("方块" + tmp);
pokers.add("梅花" + tmp);
}
static public int searchPrintEmperor(Gamer[] gamers){
int emperor = 0;
for(int i = 0; i < 5; ++i){
if(gamers[i].getRole() == "皇帝"){
emperor = i;
break;
}
}
System.out.println("皇帝是:玩家" + (emperor + 1));
System.out.print("皇帝的牌是:");
System.out.println(gamers[emperor].toString());
return emperor;
}
static public Poker[] searchTriPoker(ArrayList<Poker> pokers){
Poker thePoker[] = new Poker[2];
thePoker[0] = new Poker("NULL");
thePoker[1] = new Poker("NULL");
Poker thePokert = pokers.get(0);
int count = 1;
int flag = 0;
for (int i = 1; i < pokers.size(); i++) {
if(count == 3){
flag = 1;
}
if(thePokert.equals(pokers.get(i))){
count++;
flag = 0;
}
else{
if(flag == 1){
thePoker[0] = thePokert;
flag = 0;
}
thePokert = pokers.get(i);
count = 1;
}
if(count == 4){
thePoker[1] = thePokert;
}
}
return thePoker;
}
static public int searchGuard(Gamer[] gamers, int emp, Poker thePoker){
for (int i = 0; i < 5; i++) {
if(i == emp)
continue;
for (int j = 0; j < gamers[i].getPokers().size(); j++) {
if(thePoker.equals(gamers[i].getPokers().get(j))){
gamers[i].setRole("侍卫");
return i;
}
}
}
return -1;
}
}
class Gamer{
private String role;
private ArrayList<Poker> pokers;
public Gamer(ArrayList<Poker> pokers) {
this.pokers = pokers;
if(pokers.contains(new Poker("皇上")))
this.role = "皇帝";
else
this.role = "平民";
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public ArrayList<Poker> getPokers() {
return pokers;
}
@Override
public String toString() {
return pokers.toString();
}
}
class Poker {
private String no;
public Poker(String no) {
this.no = no;
}
public String getNo() {
return no;
}
@Override
public String toString() {
return no;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Poker poker = (Poker) o;
return Objects.equals(no, poker.no);
}
}
class PokerNumComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Poker poker = (Poker) o1;
Poker poker1 = (Poker) o2;
if(poker1.getNo() == "皇上")
return 1;
if(poker.getNo() == "皇上")
return -1;
if(poker1.getNo().charAt(0) == '大' && poker.getNo().charAt(0) == '大'){
return 0;
}
if(poker1.getNo().charAt(0) == '小' && poker.getNo().charAt(0) == '小'){
return 0;
}
if(poker1.getNo().charAt(0) == '2' && poker.getNo().charAt(0) == '2'){
return 0;
}
if(poker1.getNo().charAt(0) == 'A' && poker.getNo().charAt(0) == 'A'){
return 0;
}
if(poker1.getNo().charAt(0) == 'K' && poker.getNo().charAt(0) == 'K'){
return 0;
}
if(poker1.getNo().charAt(0) == 'Q' && poker.getNo().charAt(0) == 'Q'){
return 0;
}
if(poker1.getNo().charAt(0) == 'J' && poker.getNo().charAt(0) == 'J'){
return 0;
}
if(poker1.getNo().charAt(0) == '1' && poker.getNo().charAt(0) == '1'){
return 0;
}
if(poker1.getNo().charAt(0) == '大')
return 1;
else if(poker.getNo().charAt(0) == '大'){
return -1;
}
else if(poker1.getNo().charAt(0) == '小'){
return 1;
}
else if(poker.getNo().charAt(0) == '小'){
return -1;
}
else if(poker1.getNo().charAt(2) == '2'){
return 1;
}
else if(poker.getNo().charAt(2) == '2'){
return -1;
}
else if(poker1.getNo().charAt(2) == 'A'){
return 1;
}
else if(poker.getNo().charAt(2) == 'A'){
return -1;
}
else if(poker1.getNo().charAt(2) == 'K'){
return 1;
}
else if(poker.getNo().charAt(2) == 'K'){
return -1;
}
else if(poker1.getNo().charAt(2) == 'Q'){
return 1;
}
else if(poker.getNo().charAt(2) == 'Q'){
return -1;
}
else if(poker1.getNo().charAt(2) == 'J'){
return 1;
}
else if(poker.getNo().charAt(2) == 'J'){
return -1;
}
else if(poker1.getNo().charAt(2) == '1'){
return 1;
}
else if(poker.getNo().charAt(2) == '1'){
return -1;
}
else if(poker1.getNo().charAt(2) > poker.getNo().charAt(2)){
return 1;
}
if(poker1.getNo().charAt(2) == poker.getNo().charAt(2))
return 0;
else
return -1;
}
}
class PokerColorCompatator implements Comparator<Poker>{
@Override
public int compare(Poker o1, Poker o2) {
if (o1.getNo().charAt(0) == o2.getNo().charAt(0))
return 0;
return o1.getNo().charAt(0) > o2.getNo().charAt(0) ? 1 : -1;
}
}