java代碼實(shí)現(xiàn)斗地主發(fā)牌功能
本文實(shí)例為大家分享了java實(shí)現(xiàn)斗地主發(fā)牌功能的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)斗地主發(fā)牌功能共54張牌,地主比其他兩名玩家多三張牌。
有一個(gè)card牌類和player玩家類,還有一個(gè)發(fā)牌類用于實(shí)現(xiàn)發(fā)牌的方法。為了模擬每個(gè)玩家的牌都是隨機(jī)的,我是這樣想的:1)初始化方法:用于將54張牌存到一個(gè)數(shù)組里,每張牌都一個(gè)唯一的序號(hào)。2) 利用隨機(jī)數(shù),將每個(gè)序號(hào)打亂存到一個(gè)新數(shù)組里。3)再根據(jù)序號(hào)取到初始化牌庫(kù)數(shù)組內(nèi)的牌,存到每個(gè)玩家的牌集合內(nèi)。附一個(gè)在老師指導(dǎo)下寫的:斗地主發(fā)牌功能,自己還是有些沒(méi)考慮周到。/_
代碼如下:
牌類
public class Card { /**花色*/ private String HuaSe; /**點(diǎn)數(shù)*/ private String DianShu; /**序號(hào)*/ private int XuHao; public Card(String huaSe, String dianShu, int xuHao) { super(); HuaSe = huaSe; DianShu = dianShu; XuHao = xuHao; } public String getHuaSe() { return HuaSe; } public void setHuaSe(String huaSe) { HuaSe = huaSe; } public String getDianShu() { return DianShu; } public void setDianShu(String dianShu) { DianShu = dianShu; } public int getXuHao() { return XuHao; } public void setXuHao(int xuHao) { XuHao = xuHao; } @Override public String toString() { return '[' + HuaSe + DianShu + ']'; }}
玩家類
public class Player { /**玩家id*/ private int id; /**玩家姓名*/ private String name; /**是否是地主*/ private boolean dizhu; /**牌的集合*/ private ArrayList<Card> list; public Player(int id, String name, boolean dizhu) { super(); this.id = id; this.name = name; this.dizhu = dizhu; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isDizhu() { return dizhu; } public void setDizhu(boolean dizhu) { this.dizhu = dizhu; } public ArrayList<Card> getList() { return list; } public void setList(ArrayList<Card> list) { this.list = list; } @Override public String toString() { return name + ', 牌:' + list ; }}
發(fā)牌類: 這里還有許多缺陷 :例如地主是需要自己指定而不是隨機(jī)的,在給每個(gè)人發(fā)牌時(shí),可以利用remove()方法將已經(jīng)發(fā)過(guò)的牌移除,這樣可以節(jié)省很多重復(fù)代碼。
public class SendCard { static ArrayList<Card> arrayList = new ArrayList<Card>(); Random r = new Random(); /** * 初始話牌庫(kù) */ public void init() { for (int i = 1; i < 14; i++) { arrayList.add(new Card('梅花', Integer.toString(i), i)); arrayList.add(new Card('方塊',Integer.toString(i),13 + i)); arrayList.add(new Card('紅心', Integer.toString(i), 26 + i)); arrayList.add(new Card('黑桃', Integer.toString(i), 39 + i)); } arrayList.add(new Card('','大王',53)); arrayList.add(new Card('', '小王', 54)); } /** * 發(fā)牌(默認(rèn)p3為地主) * @param p1 * @param p2 * @param p3 */ public void send(Player p1,Player p2,Player p3) { ArrayList<Integer> intList = new ArrayList<Integer>(); intList = fenpei(intList); //給p1發(fā)牌 ArrayList<Card> clist = new ArrayList<Card>(); for (int i = 0; i < 17; i++) { clist.add(arrayList.get(intList.get(i))); } p1.setList(clist); //給p2發(fā)牌 clist = new ArrayList<Card>(); for (int i = 17; i < 34; i++) { clist.add(arrayList.get(intList.get(i))); } p2.setList(clist); //給p3發(fā)牌 clist = new ArrayList<Card>(); for (int i = 34; i < 54; i++) { clist.add(arrayList.get(intList.get(i))); } p3.setList(clist); } /** * 將初始化牌庫(kù)打亂后存入新數(shù)組 * @param list * @return */ public ArrayList<Integer> fenpei(ArrayList<Integer> list) { int index = 0; while (true) { int i = r.nextInt(54); for (Integer integer : list) { if (integer == i) { index = 1; break; } index = 0; } if(index == 0) list.add(i); if(list.size() == 54) break; } return list; }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php測(cè)試程序運(yùn)行速度和頁(yè)面執(zhí)行速度的代碼2. ASP中常用的22個(gè)FSO文件操作函數(shù)整理3. 三個(gè)不常見(jiàn)的 HTML5 實(shí)用新特性簡(jiǎn)介4. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問(wèn)題分析5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. SharePoint Server 2019新特性介紹7. React+umi+typeScript創(chuàng)建項(xiàng)目的過(guò)程8. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)9. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過(guò)程解析10. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究
