模拟保皇游戏开始的发牌过程。规则:4副扑克,5个玩家。1)有一个大王标记为皇上。每次发牌时,所发牌中有该大王的玩家是皇上。2)皇帝选择侍卫(也叫保儿、腿子,游戏过程中与皇帝一伙):作为皇上的玩家从自己的牌中选择一张拥有相同三张(点数和花色都相同)的一张牌(不能是2、3、大小王),其他四个玩家中有这张牌的就是侍卫。例如,皇上有三个红桃5,其他四个玩家中有红桃5的玩家就是侍卫。特殊情况是:1)皇上有一套四张牌相同的点数的牌,皇帝可以自己做侍卫;2)皇帝没有满足要求的牌,无法获得侍卫。程序要求:程序启动后生成5个玩家,并自动给他们发牌。然后输出:1)皇帝和侍卫的名字及其手里的牌(每张牌输出为“花色”+“点数”,如红桃5,牌之间用“,”分割),并按照大王、小王、2、A、K、Q、J、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 Poker {
// 建立一个列表储存牌
List<String> pokerList = new ArrayList<>();
//建立一个构建函数将一副牌储存到扑克列表中
public Poker() {
this.pokerList.add("小王");
this.pokerList.add("大王");
for (int i = 0; i < 4; i++) {
if (i == 0) {
for (int j = 2; j <= 10; j++)
this.pokerList.add("红桃" + j);
this.pokerList.add("红桃J");
this.pokerList.add("红桃Q");
this.pokerList.add("红桃K");
this.pokerList.add("红桃A");
} else if (i == 1) {
for (int j = 2; j <= 10; j++)
this.pokerList.add("黑桃" + j);
this.pokerList.add("黑桃J");
this.pokerList.add("黑桃Q");
this.pokerList.add("黑桃K");
this.pokerList.add("黑桃A");
} else if (i == 2) {
for (int j = 2; j <= 10; j++)
this.pokerList.add("方片" + j);
this.pokerList.add("方片J");
this.pokerList.add("方片Q");
this.pokerList.add("方片K");
this.pokerList.add("方片A");
} else if (i == 3) {
for (int j = 2; j <= 10; j++)
this.pokerList.add("梅花" + j);
this.pokerList.add("梅花J");
this.pokerList.add("梅花Q");
this.pokerList.add("梅花K");
this.pokerList.add("梅花A");
}
}
}
// 用于获得扑克
public List<String> getPoker() {
return pokerList;
}
}
//构建玩家类
class Player {
// 整了个比较器用来整理玩家的手牌
private class compareByshoupai implements Comparator<String> {
public int compare(String obj1, String obj2) {
if (obj1.equals("皇上"))
return -1;
if (obj2.equals("皇上"))
return 1;
if (obj1.equals("大王"))
return -1;
if (obj2.equals("大王"))
return 1;
if (obj1.equals("大王") && obj2.equals("小王"))
return -1;
if (obj1.equals("小王") && obj2.equals("大王"))
return 1;
if (obj1.equals("小王"))
return -1;
if (obj2.equals("小王"))
return 1;
String o1 = obj1.substring(0, 2);
String o2 = obj2.substring(0, 2);
int o11 = 0;
int o22 = 0;
if (o1.equals("方片")) {
o11 = 4;
} else if (o1.equals("梅花")) {
o11 = 3;
} else if (o1.equals("黑桃")) {
o11 = 2;
} else if (o1.equals("红桃")) {
o11 = 1;
}
if (o2.equals("方片")) {
o22 = 4;
} else if (o2.equals("梅花")) {
o22 = 3;
} else if (o2.equals("黑桃")) {
o22 = 2;
} else if (o2.equals("红桃")) {
o22 = 1;
}
String a = obj1.substring(2, obj1.length());
String b = obj2.substring(2, obj2.length());
int a1 = 0;
int b1 = 0;
if (a.equals("3")) {
a1 = 3;
} else if (a.equals("4")) {
a1 = 4;
} else if (a.equals("5")) {
a1 = 5;
} else if (a.equals("6")) {
a1 = 6;
} else if (a.equals("7")) {
a1 = 7;
} else if (a.equals("8")) {
a1 = 8;
} else if (a.equals("9")) {
a1 = 9;
} else if (a.equals("10")) {
a1 = 10;
} else if (a.equals("J")) {
a1 = 11;
} else if (a.equals("Q")) {
a1 = 12;
} else if (a.equals("K")) {
a1 = 13;
} else if (a.equals("A")) {
a1 = 14;
} else if (a.equals("2")) {
a1 = 15;
}
if (b.equals("3")) {
b1 = 3;
} else if (b.equals("4")) {
b1 = 4;
} else if (b.equals("5")) {
b1 = 5;
} else if (b.equals("6")) {
b1 = 6;
} else if (b.equals("7")) {
b1 = 7;
} else if (b.equals("8")) {
b1 = 8;
} else if (b.equals("9")) {
b1 = 9;
} else if (b.equals("10")) {
b1 = 10;
} else if (b.equals("J")) {
b1 = 11;
} else if (b.equals("Q")) {
b1 = 12;
} else if (b.equals("K")) {
b1 = 13;
} else if (b.equals("A")) {
b1 = 14;
} else if (b.equals("2")) {
b1 = 15;
}
if (a1 != b1)
return -a1 + b1;
return o22 - o11;
}
}
//构建手牌列表
List<String> shouPai = new ArrayList<>();
String name;
//玩家的构建函数 将分过来的手牌储存在手牌列表中
public Player(List<String> shouPai, String name) {
this.shouPai = shouPai;
this.name = name;
}
//建立整理函数来整理玩家手牌
public void zhengLi() {
shouPai.sort(new compareByshoupai());
}
//建立找卡函数 用于找到3张相同的的卡
public String findCard() {
int num = 0;
String guardCard = null;
for (int i = 0; i < shouPai.size() - 1; i++) {
if (!shouPai.get(i).equals("小王") && !shouPai.get(i).equals("大王")) {
if (shouPai.get(i).equals(shouPai.get(i + 1))) {
num++;
if (num == 2 && i + 2 == shouPai.size()) {
return shouPai.get(i);
}
if (num == 2 && !shouPai.get(i + 1).equals(shouPai.get(i + 2))) {
return shouPai.get(i);
}
} else
num = 0;
} else
num = 0;
}
return null;
}
//构判断是否是侍卫函数
public boolean findGurd(Player person) {
String a = findCard();
if (person.shouPai.contains(a))
return true;
return false;
}
//重写equals函数 用来outGuard中移除自身
@Override
public boolean equals(Object player) {
if (this == player)
return true;
if (player instanceof Player) {
return ((Player) player).name.equals(this.name);
}
return false;
}
//用于找到并输出侍卫
public void outGuard(List<Player> players) {
if (findCard() != null) {
players.remove(this);// 移除自身
for (Player i : players)
{
if (findGurd(i)) {
System.out.println("侍卫对应的牌是:" + findCard());
System.out.println();
System.out.println("侍卫是:" + i.name);
System.out.println();
i.zhengLi();
System.out.println("侍卫的牌是:" + i);
}
}
}
}
//判断是否为皇帝
public boolean judgeKing() {
return shouPai.contains("皇上");
}
public void kingPrint() {
if (judgeKing()) {
zhengLi();
System.out.println("皇上是:" + name);
System.out.println();
System.out.println("皇上的牌是:" + this);
System.out.println();
}
}
//重写 toString 用于打印手牌
public String toString() {
return shouPai.toString();
}
}
//建立Game类
class Game {
// 建立游戏牌库
List<String> gameCardList = new ArrayList<>();
//建立构造函数 将4副牌放进去 并将其中一个大王变成皇帝
public Game() {
for (int i = 0; i < 4; i++) {
Poker p = new Poker();
this.gameCardList.addAll(p.getPoker());
}
for (int i = 0; i < gameCardList.size(); i++) {
if (gameCardList.get(i).equals("大王")) {
gameCardList.remove(i);
gameCardList.add("皇上");
break;
}
}
}
//构建game开始函数
public void gameStart() {
int flag = 0;// 用来做是否皇帝找到三张相同的牌的标记 如果为0说明皇帝没找到三张相同的牌就重新洗牌发牌 如果为1就说明皇帝找到了 发牌结束
while (flag == 0) {
Collections.shuffle(gameCardList);// 打乱牌库顺序
// 建立五个手牌
List<String> shouPai1 = new ArrayList<>();
List<String> shouPai2 = new ArrayList<>();
List<String> shouPai3 = new ArrayList<>();
List<String> shouPai4 = new ArrayList<>();
List<String> shouPai5 = new ArrayList<>();
// 把牌库的牌分给五个手牌
for (int i = 0; i < 216; i++) {
if (i % 5 == 1)
shouPai1.add(gameCardList.get(i));
else if (i % 5 == 2)
shouPai2.add(gameCardList.get(i));
else if (i % 5 == 3)
shouPai3.add(gameCardList.get(i));
else if (i % 5 == 4)
shouPai4.add(gameCardList.get(i));
else if (i % 5 == 0)
shouPai5.add(gameCardList.get(i));
}
// 建立两个玩家列表
List<Player> playerList = new ArrayList<>();
List<Player> playerList2 = new ArrayList<>();
// 将手牌分给各个玩家 并将各个玩家放入玩家列表中
Player 玩家1 = new Player(shouPai1, "玩家1");
playerList.add(玩家1);
Player 玩家2 = new Player(shouPai2, "玩家2");
playerList.add(玩家2);
Player 玩家3 = new Player(shouPai3, "玩家3");
playerList.add(玩家3);
Player 玩家4 = new Player(shouPai4, "玩家4");
playerList.add(玩家4);
Player 玩家5 = new Player(shouPai5, "玩家5");
playerList.add(玩家5);
// 将第一个列表的内容放入第二的列表 用于寻找侍卫
playerList2.addAll(playerList);
// 遍历第一个玩家列表寻找皇帝和他侍卫
for (Player i : playerList) {
if (i.judgeKing()) {
i.kingPrint();
if (i.findCard() != null) {
flag = 1;
} else {
System.out.println("皇上无三张相同的牌 无法得到侍卫 重新发牌");
System.out.println();
}
i.outGuard(playerList2);// 如果用第一个玩家列表会报错 所以我整第二个列表来找侍卫并输出
}
}
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Game baohuang = new Game();
baohuang.gameStart();
}
}