模拟:保皇游戏开始的发牌过程。规则: 4 副扑克, 5 个玩家。1 )有一个大王标记为皇上。每次发牌时,所发牌中有该大王的玩家是皇上。2 )皇帝选择侍卫(也叫保儿,腿子,游戏过程中与皇帝一伙):作为皇上的玩家从自己的牌中选择一张拥有相同三张(点数和花色都相同)的一张牌(不能是 2 , 3 ,大小王),其他四个玩家中有这张牌的就是侍卫。例如,皇上有三个红桃 5 ,其他四个玩家中有红桃 5 的玩家就是侍卫。特殊情况是: 1 )皇上某个套四张牌相同的点数的牌,皇帝可以自己做侍卫; 2 )皇帝没有满足要求的牌,无法获得侍卫。程序要求:程序启动后生成 5 个玩家,并自动给他们发牌。然后输出: 1 )皇帝和侍卫的名字及其持有的牌(每张牌输出为“花色” + “点数”,如红桃 5 ,牌之间用“,”分割),并按照大王,小王, 2 , A , K , Q, Ĵ , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 的顺序排列,相同点数但不同花色的牌要把相同花色的牌放在一起; 2 )那张作为侍卫所特有的牌( “花色” + “点数”)如果无法得到侍卫,则程序输出相应的提示例如,程序运行后输出如下的结果。:
皇帝是:玩家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.*;
class Card implements Comparable{
private String Suit;
private String num;
private boolean flag;
private int ranking1;
private int ranking2;
Card(String Suit,String num,int ranking1,int ranking2){
this.Suit=Suit;
this.num=num;
this.flag=false;
this.ranking1=ranking1;
this.ranking2=ranking2;
}
public int getRanking1() {
return this.ranking1;
}
public int getRanking2() {
return this.ranking2;
}
public void FlagCard() {
this.flag=true;
}
public boolean getFlag() {
return this.flag;
}
public String getSuit() {
return this.Suit;
}
public String getNum() {
return this.num;
}
public int compareTo(Object arg0) {
Card p=(Card)arg0;
if(this.flag)
return -1;
if(this.getRanking1()<p.getRanking1()) {
return -1;
}
else if(this.getRanking1()==p.getRanking1()) {
if(this.getRanking2()<p.getRanking2()) {
return -1;
}
else if(this.getRanking2()==p.getRanking2()) {
return 0;
}
}
return 1;
}
}
class People{
private int Identity;
private Map<Card,Integer> HandCnt=new TreeMap<Card,Integer>();
private Queue<Card> Hand = new PriorityQueue<>();
private Queue<Card> Hand2 = new PriorityQueue<>();
private int work;
public void setIdentity(int Identity) {
this.Identity=Identity;
}
public void setWork(int n) {
this.work=n;
}
public int getIdentity() {
return this.Identity;
}
public void setCard(List<Card> p) {
for(int i=0;i<p.size();i++) {
Hand.add(p.get(i));
if(HandCnt.containsKey(p.get(i))) {
int tmp=HandCnt.get(p.get(i))+1;
HandCnt.remove(p.get(i));
HandCnt.put(p.get(i), tmp);
}
else {
HandCnt.put(p.get(i),1);
}
}
}
public void printAns2() {
int k=0;
while(!Hand2.isEmpty()) {
if(k>0)
System.out.print(", ");
Card p=Hand2.poll();
if(p.getFlag()) {
System.out.print("皇帝");
}
else {
if(p.getRanking1()==0||p.getRanking1()==1) {
System.out.print(p.getSuit());
}
else {
System.out.print(p.getSuit()+p.getNum());
}
}
k++;
}
}
public void printAns1() {
int k=0;
while(!Hand.isEmpty()) {
if(k>0)
System.out.print(", ");
Card p=Hand.poll();
Hand2.add(p);
if(p.getFlag()) {
System.out.print("皇帝");
}
else {
if(p.getRanking1()==0||p.getRanking1()==1) {
System.out.print(p.getSuit());
}
else {
System.out.print(p.getSuit()+p.getNum());
}
}
k++;
}
}
public Map<Card,Integer> getMap(){
return this.HandCnt;
}
public Queue<Card> getQueue(){
return this.Hand;
}
public boolean solve(Card p) {
for (Map.Entry<Card, Integer> entry : HandCnt.entrySet()) {
if(p.getSuit().equals(entry.getKey().getSuit())&&p.getNum().equals(entry.getKey().getNum()))
return true;
}
return false;
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] Suit= {
"大王","小王","黑桃","红桃","方片","梅花"};
String[] Num= {
"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
List<Card> Start=new ArrayList();
boolean king =false;
for(int i=0;i<4;i++) {
for(int j=0;j<6;j++) {
if(Suit[j].equals("大王")||Suit[j].equals("小王")) {
Card p=new Card(Suit[j],"0",j,-1);
if(!king&&Suit[j].equals("大王")) {
king=true;
p.FlagCard();
}
Start.add(p);
continue;
}
for(int k=0;k<13;k++) {
Start.add(new Card(Suit[j],Num[k],j,k));
}
}
}
List<Card> Fenfa=new ArrayList();
People[] peo=new People[5];
Set<Integer> judge=new HashSet<Integer>();
int len=Start.size(),kingNum = -1,knightNum=-1;
for(int i=0;i<5;i++) {
peo[i]=new People();
peo[i].setIdentity(i);
peo[i].setWork(0);
Fenfa.clear();
for(int j=0;j<43;j++) {
int k=(int)(Math.random()*1000)%len;
while(judge.contains(k)) {
k=(int)(Math.random()*1000)%len;
}
judge.add(k);
if(Start.get(k).getFlag()) {
peo[i].setWork(1);
kingNum=i;
}
Fenfa.add(Start.get(k));
}
if(i==0) {
int k=(int)(Math.random()*1000)%len;
while(judge.contains(k)) {
k=(int)(Math.random()*1000)%len;
}
judge.add(k);
if(Start.get(k).getFlag()) {
peo[i].setWork(1);
kingNum=i;
}
Fenfa.add(Start.get(k));
}
peo[i].setCard(Fenfa);
}
Map<Card,Integer> p=peo[kingNum].getMap();
Card kingCard = null;
int CardCnt=0;
for (Map.Entry<Card, Integer> entry : p.entrySet()) {
if(entry.getValue()>CardCnt) {
CardCnt=entry.getValue();
kingCard=entry.getKey();
}
}
if(CardCnt==4) {
System.out.println("皇帝是:玩家"+(kingNum+1));
System.out.print("皇帝的牌是:[");
Queue<Card> ans=new PriorityQueue<>();
ans=peo[kingNum].getQueue();
peo[kingNum].printAns1();
System.out.print("]\n");
if(kingCard.getRanking1()<2) {
System.out.print("侍卫对应的牌是:"+kingCard.getSuit()+"\n");
}
else {
System.out.print("侍卫对应的牌是:"+kingCard.getSuit()+kingCard.getNum()+"\n");
}
System.out.println("侍卫是:玩家"+(kingNum+1));
System.out.print("侍卫的牌是:[");
peo[kingNum].printAns2();
System.out.print("]\n");
}
else if(CardCnt==3) {
for(int i=0;i<5;i++) {
if(i==kingNum)
continue;
if(peo[i].solve(kingCard)) {
knightNum=i;
break;
}
}
System.out.println("皇帝是:玩家"+(kingNum+1));
System.out.print("皇帝的牌是:[");
peo[kingNum].printAns1();
System.out.print("]\n");
if(kingCard.getRanking1()<2) {
System.out.print("侍卫对应的牌是:"+kingCard.getSuit()+"\n");
}
else {
System.out.print("侍卫对应的牌是:"+kingCard.getSuit()+kingCard.getNum()+"\n");
}
System.out.println("侍卫是:玩家"+(knightNum+1));
System.out.print("侍卫的牌是:[");
peo[knightNum].printAns1();
System.out.print("]\n");
}
else {
System.out.println("无法得到骑士");
}
}
}