您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用Java實(shí)現(xiàn)斗地主小游戲”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用Java實(shí)現(xiàn)斗地主小游戲”吧!
本文實(shí)例為大家分享了Java實(shí)現(xiàn)斗地主小游戲的具體代碼,供大家參考,具體內(nèi)容如下
* 1、組合牌
* 2、洗牌
* 3、發(fā)牌
* 4、看牌
package itcast.demo6; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; public class DouDiZhu { public static void main(String[] args) { //1、組合牌 //創(chuàng)建Map集合,鍵:編號 值:牌 HashMap<Integer,String> pooker = new HashMap<Integer,String>(); //創(chuàng)建List集合,存儲編號 ArrayList<Integer> pookerNumber = new ArrayList<Integer>(); //定義13個(gè)點(diǎn)數(shù)的數(shù)組 String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; //定義4個(gè)花色組合 String[] colors = {"?","?","?","?"}; //定義一個(gè)整數(shù)變量,作為Map的鍵 int index = 2; //遍歷數(shù)組,用花色+點(diǎn)數(shù)的組合,存儲到Map集合中 for(String number : numbers) { for(String color : colors) { pooker.put(index, color + number); pookerNumber.add(index); index++; } } //System.out.println(pooker); //System.out.println(pookerNumber); //單獨(dú)存儲大王和小王 pooker.put(0, "大王"); pookerNumber.add(0); pooker.put(1, "小王"); pookerNumber.add(1); //洗牌,將牌的編號打亂 Collections.shuffle(pookerNumber); //System.out.println(pookerNumber); //發(fā)牌,將牌編號,發(fā)給3個(gè)玩家集合+1個(gè)底牌集合 ArrayList<Integer> player1 = new ArrayList<Integer>(); ArrayList<Integer> player2 = new ArrayList<Integer>(); ArrayList<Integer> player3 = new ArrayList<Integer>(); ArrayList<Integer> buttom = new ArrayList<Integer>(); //發(fā)牌,采用的是集合的索引%3 for(int i = 0; i < pookerNumber.size();i++) { //現(xiàn)將底牌做好 if(i<3) { //存到底牌去 buttom.add(pookerNumber.get(i)); //對索引%3判斷 }else if(i % 3 == 0) { //索引上的編號,發(fā)給玩家1 player1.add(pookerNumber.get(i)); }else if(i % 3 == 1) { //發(fā)給玩家2 player2.add(pookerNumber.get(i)); }else if(i % 3 == 2) { //發(fā)給玩家3 player3.add(pookerNumber.get(i)); } } //對玩家手中的編號進(jìn)行排序 Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); //看牌,就是將玩家手中的編號,到Map集合中查找,根據(jù)鍵找值 //定義實(shí)現(xiàn)方法 look("包身工 ",player1,pooker); look("清潔工 ",player2,pooker); look("洗碗工 ",player3,pooker); look("底牌 ",buttom,pooker); } public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker) { //遍歷ArrayList集合,獲取元素,作為鍵,到集合Map中找值 System.out.print(name+" "); for(Integer key : player) { String value = pooker.get(key); System.out.print(value+" "); } System.out.println(); } }
運(yùn)行結(jié)果:
感謝各位的閱讀,以上就是“怎么用Java實(shí)現(xiàn)斗地主小游戲”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么用Java實(shí)現(xiàn)斗地主小游戲這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。