保皇Java
模擬保皇游戲開始的發牌過程。規則: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);}} elsenum = 0;} elsenum = 0;}return null;}//構判斷是否是侍衛函數 public boolean findGurd(Player person) {String a = findCard();if (person.shouPai.contains(a))return true;return false;}//重寫equals函數 用來outGuard中移除自身@Overridepublic 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();} }總結
- 上一篇: 马化腾教会我的
- 下一篇: MATLAB 对试验点进行三次拟合