您好,登錄后才能下訂單哦!
這篇文章主要講解了java如何實現斗地主游戲,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
思路:
1、創建一個撲克牌的實體類Poker,設置了四個參數:花色、數字、牌值(判斷大小)、是否地主牌,實現getset方法和構造方法;
2、創建一個玩家的實體類Player,設置了四個參數: 初始牌集合,排序后牌集合,牌值集合,是否地主,實現getset方法和構造方法;
3、洗牌:循環嵌套花色數組跟數字數組生成52個Poker,手動加入大小王Poker,放進map(int,Poker)里面,利用Collections.shuffle隨機排序map的key,再根據key生成排序后的Poker集合
4、發牌:生成3個玩家對象,拿到洗牌后的集合,前51張牌為玩家牌,后3張為地主牌,在51張牌中隨機一張為地主牌,遍歷玩家牌分配個每一個Player,同時根據玩家牌的牌值生成集合grades存進Player對象中,將grades轉為int數組并進行升序排序,根據grade取得新Poker集合分配給玩家。
Poker類:
package com.company; public class Poker { private String color; private String number; private int grade; boolean isLandowners; public boolean isLandowners() { return isLandowners; } public void setLandowners(boolean landowners) { isLandowners = landowners; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Poker() { } public Poker(String color, String number ,int grade,boolean isLandowners) { this.color = color; this.number = number; this.grade=grade; this.isLandowners=isLandowners; } public String toString() { return this.getColor()+this.getNumber(); } }
Player類:
package com.company; import java.util.ArrayList; import java.util.LinkedList; public class Player { private ArrayList<Poker> pokers; private boolean isLandower; private ArrayList<Poker> newPokers; private LinkedList<Integer> grades; public LinkedList<Integer> getGrades() { return grades; } public void setGrades(LinkedList<Integer> grades) { this.grades = grades; } public ArrayList<Poker> getNewPokers() { return newPokers; } public void setNewPokers(ArrayList<Poker> newPokers) { this.newPokers = newPokers; } public boolean isLandower() { return isLandower; } public void setLandower(boolean landower) { isLandower = landower; } public Player() { } public ArrayList<Poker> getPokers() { return pokers; } public void setPokers(ArrayList<Poker> pokers) { this.pokers = pokers; } }
洗牌:
/*洗牌*/ public static LinkedList<Poker> getPokerList() { LinkedList<Poker> pokers = new LinkedList<Poker>(); LinkedList<Poker> newPokers = new LinkedList<Poker>(); ArrayList<Integer> arrayList = new ArrayList<>(); int[] arr; /*生成54張撲克牌(Poker對象)*/ /*用嵌套循環生成從方塊3到黑桃2的Poker對象,放進Poker的LinkedList保證順序*/ String[] colors = {"♦", "♣", "♥", "♠"}; String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}; for (int i = 0; i < numbers.length; i++) { for (int j = 0; j < colors.length; j++) { Poker p = new Poker(colors[j], numbers[i], (4 * i) + j + 1, false); pokers.add(p); } } /*添加大小王*/ pokers.add(new Poker("black", "☆", 53, false)); pokers.add(new Poker("red", "☆", 54, false)); /*將生成的54張撲克牌從1到54放進map*/ for (int z = 1; z <= pokers.size(); z++) { map.put(z, pokers.get(z - 1)); } /*遍歷Map,將每個Poker對應的key放進 ArrayList<Integer> arrayList */ Set<Map.Entry<Integer, Poker>> set = map.entrySet(); Iterator it = set.iterator(); while (it.hasNext()) { Map.Entry<Integer, Poker> e = (Map.Entry<Integer, Poker>) it.next(); arrayList.add(e.getKey()); } /*利用Collections.shuffle隨機排序key*/ Collections.shuffle(arrayList); /*將亂序后的key對于的Poker放進newPokers里面 */ for (Integer i : arrayList) { newPokers.add(map.get(i)); } return newPokers; }
發牌:
/*發牌*/ /*根據玩家人數生成Player,默認num=3*/ private static void sentPoker(int num) { /*獲取洗牌后的LinkedList,保證順序*/ LinkedList<Poker> Allpokers = getPokerList(); ArrayList<Poker> pokers = new ArrayList<>(); ArrayList<Poker> BottomPokers = new ArrayList<>(); /*生成玩家對象*/ for (int i = 0; i < num; i++) { Player p = new Player(); players.add(p); } /*前50張牌為玩家牌,后三張為底牌*/ for (int p = 0; p < Allpokers.size(); p++) { if (p <= 50) { pokers.add(Allpokers.get(p)); } else { BottomPokers.add(Allpokers.get(p)); } } /*玩家牌中隨機一張為地主牌*/ Random rand = new Random(); int s = rand.nextInt(51); pokers.get(s).setLandowners(true); System.out.println("地主牌: " + pokers.get(s)); /*根據num生成Player對象*/ for (int j = 0; j < num; j++) { /*玩家的牌值集合*/ LinkedList<Integer> grades = new LinkedList<>(); /*玩家的手牌集合*/ ArrayList<Poker> playerPokers = new ArrayList<Poker>(); /*根據玩家個數分配玩家牌*/ for (int z = j; z < pokers.size(); z = z + num) { playerPokers.add(pokers.get(z)); grades.add(pokers.get(z).getGrade()); players.get(j).setPokers(playerPokers); players.get(j).setGrades(grades); } /*根據玩家牌中的地主牌設置玩家是否地主*/ for (Poker p : players.get(j).getPokers()) { if (p.isLandowners() == true) { players.get(j).setLandower(true); } } /*根據玩家的牌值集合生成數組*/ Integer[] gs = new Integer[players.get(j).getGrades().size()]; grades.toArray(gs); /*排序數組,由低到高*/ Arrays.sort(gs); /*排序后的玩家牌*/ ArrayList<Poker> newPokers = new ArrayList<>(); for (int g = 0; g < gs.length; g++) { Poker p = map.get(gs[g]); newPokers.add(p); } players.get(j).setNewPokers(newPokers); System.out.println("玩家" + (j + 1) + ": " + players.get(j).getNewPokers() + " 是否地主:" + players.get(j).isLandower()); } System.out.print("底牌為: "); for (Poker p : BottomPokers) { System.out.print(p + " "); } }
效果如下:
看完上述內容,是不是對java如何實現斗地主游戲有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。