模拟保皇游戏开始的发牌过程。规则: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.*;
public class Main {
public static void main(String[] args) {
List<Poker> allpoker = new ArrayList();// 全部的牌
String[] color = { "黑桃", "梅花", "红桃", "方片" };
String[] point = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
Poker king_poker = new Poker("皇上", null, 16);
allpoker.add(king_poker);// 加入一张皇帝牌
for (int i = 0; i < 3; i++) { // 加入三张大王
allpoker.add(new Poker("大王", null, 15));
}
for (int i = 0; i < 4; i++) { // 加入三张小王
allpoker.add(new Poker("小王", null, 14));
}
for (int k = 0; k < 4; k++)// 将剩余全部的牌加入allpoker
{
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
allpoker.add(new Poker(point[i], color[j], i + 1));
}
}
}
Collections.shuffle(allpoker);// 将allpoker中扑克牌的顺序打乱
List<Poker>[] player = new List[5];// 生成五个玩家
for (int i = 0; i < 5; i++) {
player[i] = new ArrayList();
}
// 将allpoker分为五部分分给五个人
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 43; i++) {
player[j].add(allpoker.get(i + (43) * j));
}
}
for (int i = 172; i < 216; i++) {
player[4].add(allpoker.get(i));
}
// for(int i=0;i<5;i++)//测试
// {
// System.out.println(player[i].size());
// }
for (int i = 0; i < 5; i++) { // 将五个玩家的手牌按照规则排好序
Collections.sort(player[i], new Comparator<Poker>() {
public int compare(Poker o1, Poker o2) {
if (o1.level < o2.level)
return 1;
else if (o1.level > o2.level)
return -1;
else {
if (o1.color != null && o2.color != null)
return o1.color.compareTo(o2.color);
else
return 0;
}
}
});
}
for (int i = 0; i < 5; i++) { // 测试 输出所有玩家手牌
System.out.print("玩家" + (i + 1) + ": ");
for (int j = 0; j < player[i].size(); j++)
System.out.print(player[i].get(j) + " ");
System.out.println();
}
int king = -1;
for (int i = 0; i < 5; i++) { // 找到有皇帝牌的玩家,即为皇帝
if (player[i].contains(king_poker)) {
king = i;
break;
}
}
int count = 1;
int max_samecards = 0;
Poker guard_poker = new Poker();// 找到三张一样的牌
for (int i = player[king].size() - 1; i >= 1; i--) {
Poker t1 = (Poker) player[king].get(i);
Poker t2 = (Poker) player[king].get(i - 1);
if (t1.level == 1)
continue; // 3不能作为侍卫牌
if (t1.level >= 13)
break;// 2以上的牌不能作为侍卫牌
if (t1.equals(t2)) {// 计数器记录当前 相同牌的数目
count++;
}
else {
if (count > max_samecards) {// 记录最多的相同牌数,如果大于等于三,记录它的花色和点数
max_samecards = count;
if (max_samecards >= 3)
guard_poker = (Poker) player[king].get(i);
}
count = 1;// 计数器置1
}
}
int guard = -1;
if (max_samecards == 3) {// 通过侍卫牌在另外四个玩家中找到侍卫,并记录
for (int i = 0; i < 5; i++) {
if(Collections.frequency(player[i], guard_poker)==1)
{
guard=i;
}
}
}
if (max_samecards == 3) { // 输出
System.out.println("皇帝是:玩家" + (king + 1));
System.out.print("皇帝的牌是:[");
for (int i = 0; i < player[king].size(); i++) {
if (i == 0)
System.out.print(player[king].get(i));
else
System.out.print(", " + player[king].get(i));
}
System.out.println("]");
System.out.println("侍卫对应的牌是:" + guard_poker.toString());
System.out.println("侍卫是:玩家" + (guard + 1));
System.out.print("侍卫的牌是:[");
for (int i = 0; i < player[guard].size(); i++) {
if (i == 0)
System.out.print(player[guard].get(i));
else
System.out.print(", " + player[guard].get(i));
}
System.out.println("]");
} else if (max_samecards == 4) {
System.out.println("皇帝和侍卫都是:玩家" + (king + 1));
System.out.println("侍卫对应的牌是:" + guard_poker.toString());
System.out.print("皇帝(侍卫)的牌是:[");
for (int i = 0; i < player[king].size(); i++) {
if (i == 0)
System.out.print(player[king].get(i));
else
System.out.print(", " + player[king].get(i));
}
System.out.println("]");
} else {
System.out.println("皇帝没有满足要求的牌,无法获得侍卫");
}
}
}
class Poker {
public String point;// 点数
public String color;// 花色
public int level;// 排序依据
public Poker() {
}
public Poker(String point, String color, int level) {
this.point = point;
this.color = color;
this.level = level;
}
public String toString() {
if (color != null) {
return color + point;
} else
return point;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Poker other = (Poker) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (level != other.level)
return false;
if (point == null) {
if (other.point != null)
return false;
} else if (!point.equals(other.point))
return false;
return true;
}
}