您好,登錄后才能下訂單哦!
這篇文章主要介紹java如何實現(xiàn)多人多牌數(shù)比較游戲,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體實現(xiàn)步驟如下:
實現(xiàn)撲克Card類,用于儲存撲克牌,1-13代表點數(shù),4-1代表花色(黑桃,紅桃,梅花,分塊)
實現(xiàn)撲克列表CardList類,用于實現(xiàn)生成一副牌,洗牌,發(fā)牌的功能
實現(xiàn)玩家Player類,用于儲存玩家信息
實現(xiàn)玩家列表PlayerList類,用于儲存所有玩家信息,并且實現(xiàn)玩家洗牌、發(fā)牌、比較牌大小的方法
主方法Index05類,用于啟動撲克游戲
Card.java
/** * */ package dk.card; /** * @author Administrator * 撲克類 */ public class Card implements Comparable<Card>{ private int number;//牌的點數(shù)--1到13 private int type;//牌的花色--4種花色:?(黑桃):4 ?(紅桃):3 ?(梅花):2 方塊:1 public Card() { super(); } public Card(int number, int type){ if(number==1) number=14; this.number = number; this.type = type; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public int getType() { return type; } public void setType(int type) { this.type = type; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + number; result = prime * result + type; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Card other = (Card) obj; if (number != other.number) return false; if (type != other.type) return false; return true; } @Override public int compareTo(Card o) { if(this.number>o.number) return -1; else if(this.number<o.number) return 1; else if(this.type>o.type) return -1; else if(this.type<o.type) return 1; return 0; } public String toString(){ String str =""; switch(this.type){ case 1: str = "方塊";break; case 2: str = "梅花";break; case 3: str = "紅桃";break; case 4: str = "黑桃";break; } String n=""; switch(this.number){ case 14:n="A";break; case 11:n="J";break; case 12:n="Q";break; case 13:n="K";break; default:n=this.number+""; } return str+n; } }
CardList.java
package dk.card; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * @author Administrator * 牌表操作類 */ public class CardList { public static List<Card> cardlist = new ArrayList<Card>(52);//儲存撲克牌 public static List<ArrayList<Card>> poker;//儲存發(fā)牌結(jié)果 //生成一副牌 public static void generateCard() { for(int i=1;i<=13;i++){ for(int j=1;j<=4;j++){ Card c = new Card(i,j); cardlist.add(c); } } } //打印一副牌 public static void printCardList(){ System.out.print("["); for(Card c:cardlist){ System.out.print(c.toString()+" "); } System.out.println("]"); } //洗牌 public static void shuffleCard(){ List<Card> scard = new ArrayList<Card>(52); Card c; Random random = new Random(); for(int i=1;i<=52;i++){ do{ c = cardlist.get(random.nextInt(52)); }while(scard.contains(c)); scard.add(c); } cardlist.clear(); cardlist = scard; printCardList(); } /** * 發(fā)牌 * @param i number of player * @param j number of card * @return */ public static List<ArrayList<Card>> dealCard(int i,int j){ try{ if(i<=1){ throw new Exception("發(fā)牌規(guī)則有誤:玩家至少為2人,請重新定義規(guī)則!"); } if(i*j>52){ throw new Exception("發(fā)牌規(guī)則有誤:牌數(shù)超出撲克張數(shù),請重新定義規(guī)則!"); } poker = new ArrayList<ArrayList<Card>>(i);//儲存玩家發(fā)到的牌 //生成玩家手中的牌列表 for(int h=0;h<i;h++){ ArrayList<Card> player_card_list = new ArrayList<Card>(j); poker.add(player_card_list); } //給每個玩家發(fā)牌 for(int k=0;k<j;k++){ for(int d=0;d<i;d++){ ArrayList<Card> player_card_list = poker.get(d); Card card = getCard(); player_card_list.add(card); } } }catch(Exception e){ e.printStackTrace(); } return poker; } //抓取一張牌 public static Card getCard(){ return cardlist.remove(0); } //獲取一位玩家的牌 public static ArrayList<Card> getPoker(){ return poker.remove(0); } public static void clear(){ cardlist.clear(); poker.clear(); printCardList(); } }
Player.java
/** * */ package dk.card; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * @author Administrator * 玩家類 */ public class Player implements Comparable<Player>{ private String id;//玩家id private String name;//玩家姓名 private List<Card> poker;//玩家手牌 private Card max;//玩家最大的牌 public Player(String id ,String name){ this.id = id; this.name = name; this.setPoker(new ArrayList<Card>()); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Card> getPoker() { return poker; } public Card getMax() { return max; } public void setMax(Card max) { this.max = max; } public void setPoker(List<Card> poker) { this.poker = poker; Collections.sort(this.poker); for(Card c :this.poker){ this.setMax(c); break; } } public String toString(){ return "玩家Id:"+this.id+"\t玩家姓名:"+this.name; } //玩家攤牌 public String showCard(){ String msg=""; for(Card c:this.poker){ msg+=c.toString()+" "; } return this.toString()+"\t\n"+"玩家牌信息:\t\n"+msg; } @Override public int compareTo(Player o) { return this.max.compareTo(o.getMax()); } }
PlayerList.java
package dk.card; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * @author Administrator * 儲存玩家列表 */ public class PlayerList { public static List<Player> playerlist = new ArrayList<Player>();//儲存撲玩家列表 public static int sum; public static List<String> rules = new ArrayList<String>(); public PlayerList(){ } public static void setRule(String rule){ rules.add(rule); } public static String getRules(){ String str=""; for(String s:rules){ str+=s+"\t\n"; } return str; } public static void printRules(){ show(getRules()); } //添加玩家 public static boolean addplayer(Player player){ show("添加玩家:"+player.toString()); show("----------------------------------------"); sum++; return playerlist.add(player); } //給所有玩家發(fā)牌 public static void shuffleCardToPlayer(){ for(Player player:playerlist){ player.setPoker(CardList.getPoker()); } } //顯示發(fā)牌結(jié)果 public static void showCard(){ for(Player player:playerlist){ show(player.showCard()); show("----------------------------------------"); } } //比較牌點大小并且顯示贏家信息 public static void showWinner(){ Collections.sort(playerlist); Player p = playerlist.get(0); show(p.showCard()); show("本局贏家是:"+p.getName()); } //玩家人數(shù) public static int size(){ return sum; } public static void show(Object obj){ System.out.println(obj.toString()); } public static void clear(){ playerlist.clear(); sum = 0 ; } }
Index05.java
/** * */ package dk.main; import java.util.Scanner; import dk.card.CardList; import dk.card.Player; import dk.card.PlayerList; /** * @author Administrator * */ public class Index05 extends Show { public static Scanner scanner = new Scanner(System.in); /** * 調(diào)用的主方法 * @param args */ public static void main(String[] args) { show("是否開始游戲?0/1"); int isStart = scanner.nextInt(); if(isStart==1){ startGame(); } else{ show("退出游戲!"); } } //啟動撲克游戲 public static void startGame(){ show("游戲規(guī)則如下:"); PlayerList.setRule("1.至少需要兩名玩家"); PlayerList.setRule("2.所有玩家的牌數(shù)加起來不超過52張牌"); PlayerList.setRule("3.比較所有玩家手中最大的牌點,最大的牌點數(shù)的玩家獲勝"); PlayerList.setRule("4.總的牌數(shù)只有52張,沒有大小王"); PlayerList.setRule("5.黑桃牌A最大,方塊3最小,點數(shù)相同時,比較花色,黑桃較大。"); PlayerList.printRules(); show("請輸入玩家人數(shù):"); int players = 2;//默認玩家數(shù)目 int cardofplay = 2;//默認每個玩家的手牌數(shù)目 try { players = scanner.nextInt(); while(players<2){ show("玩家人數(shù)至少是兩人"); show("請重新輸入人數(shù):"); players = scanner.nextInt(); } show("請輸入每個玩家持有的牌數(shù):"); cardofplay = scanner.nextInt(); while(cardofplay*players>52){ show("玩家*牌數(shù)超出范圍!請重新輸入牌數(shù)"); cardofplay = scanner.nextInt(); } show("開始輸入玩家信息:"); for(int i=0;i<players;i++){ show("請輸入第"+(i+1)+"個玩家姓名:"); String name = scanner.next(); Player p =new Player("00"+i,name); PlayerList.addplayer(p); } show("一副沒有洗的牌:"); CardList.generateCard();//生成一副撲克 CardList.printCardList();//打印撲克 show("========================================"); show("開始洗牌:"); CardList.shuffleCard();//洗牌 show("========================================"); show("開始發(fā)牌:........發(fā)牌中......"); CardList.dealCard(players,cardofplay);//發(fā)牌 PlayerList.shuffleCardToPlayer();//給玩家發(fā)牌 show("發(fā)牌結(jié)果:"); PlayerList.showCard();//顯示所有玩家牌信息 show("========================================"); show("比較結(jié)果:比較牌點大小并且顯示贏家信息"); PlayerList.showWinner();//顯示本輪贏家 show("========================================"); } catch (Exception e) { e.printStackTrace(); } } //清空游戲 public static void clearGame(){ CardList.clear(); PlayerList.clear(); } //測試撲克游戲 public static void testGame(){ try { show("一副沒有洗的牌:"); CardList.generateCard(); CardList.printCardList(); show("========================================"); show("開始添加玩家:"); Player p =new Player("001","DekingChen"); Player p1 =new Player("002","chenzhenquan"); Player p2 =new Player("003","czq"); PlayerList.addplayer(p); PlayerList.addplayer(p1); PlayerList.addplayer(p2); show("開始洗牌:"); CardList.shuffleCard(); show("========================================"); show("開始發(fā)牌:........發(fā)牌中......"); int players = PlayerList.size();//玩家數(shù)目 int cardofsum = 3;//每個玩家的手牌數(shù)目 CardList.dealCard(players,cardofsum); PlayerList.shuffleCardToPlayer(); show("發(fā)牌結(jié)果:"); PlayerList.showCard(); show("========================================"); show("比較結(jié)果:比較牌點大小并且顯示贏家信息"); PlayerList.showWinner(); show("========================================"); } catch (Exception e) { e.printStackTrace(); } } }
Show.java(輔助顯示類)
package dk.main; public abstract class Show { public static void show(Object obj){ System.out.println(obj.toString()); } }
實驗結(jié)果:
是否開始游戲?0/1 1 游戲規(guī)則如下: 1.至少需要兩名玩家 2.所有玩家的牌數(shù)加起來不超過52張牌 3.比較所有玩家手中最大的牌點,最大的牌點數(shù)的玩家獲勝 4.總的牌數(shù)只有52張,沒有大小王 5.黑桃牌A最大,方塊3最小,點數(shù)相同時,比較花色,黑桃較大。 請輸入玩家人數(shù): 5 請輸入每個玩家持有的牌數(shù): 5(<a href="http://www.dztcsd.com/">資質(zhì)代辦</a>) 開始輸入玩家信息: 請輸入第1個玩家姓名: DekingChen 添加玩家:玩家Id:000 玩家姓名:DekingChen ---------------------------------------- 請輸入第2個玩家姓名: czq 添加玩家:玩家Id:001 玩家姓名:czq ---------------------------------------- 請輸入第3個玩家姓名: chenzhenquan 添加玩家:玩家Id:002 玩家姓名:chenzhenquan ---------------------------------------- 請輸入第4個玩家姓名:
以上是“java如何實現(xiàn)多人多牌數(shù)比較游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。