使用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 + " ");}}
效果展示