本文实例为大家分享了java实现斗地主发牌的具体代码,供大家参考,具体内容如下 参考斗地主的游戏规则,完成一个发牌的功能(54张牌,考虑点数,花色;三名玩家,其中地主比其他
本文实例为大家分享了java实现斗地主发牌的具体代码,供大家参考,具体内容如下
参考斗地主的游戏规则,完成一个发牌的功能(54张牌,考虑点数,花色;三名玩家,其中地主比其他玩家多3张牌)
代码如下:
牌类
import java.util.Objects; /** * 3-10 J Q K A 2 King Queen 牌类 * * @author Administrator * */ public class Card { /** 牌面值 */ private String name; /** 花色 */ private String flower; /** 大小点数 */ private int num; public Card() { super(); } public Card(String name, String flower, int num) { super(); this.name = name; this.flower = flower; this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFlower() { return flower; } public void setFlower(String flower) { this.flower = flower; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } @Override public String toString() { if(Objects.nonNull(flower)) { return name + "-" + flower; } return name; } }
玩家类
public class Player { /**玩家ID*/ private int id; /**玩家姓名*/ private String name; /**是否是地主*/ private boolean boss; /**牌集合*/ private ArrayList<Card> cards = new ArrayList<Card>(); public Player(int id, String name, boolean boss, ArrayList<Card> cards) { super(); this.id = id; this.name = name; this.boss = boss; this.cards = cards; } public Player() { super(); } 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 isBoss() { return boss; } public void setBoss(boolean boss) { this.boss = boss; } public ArrayList<Card> getCards() { return cards; } public void setCards(ArrayList<Card> cards) { this.cards = cards; } @Override public String toString() { return name +(boss? "(地主):":"(农名):")+ cards; } }
管理类
public class GameManage { private static Random randomGen = new Random(); /** 声明所有牌的集合 */ private static ArrayList<Card> all = new ArrayList<Card>(); /** 用于生成牌的牌面值 */ private static String[] names = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "小王","大王" }; /** 用于生成牌的花色 */ private static String[] flowers = { "红桃", "方块", "梅花", "黑桃" }; private ArrayList<Player> players = new ArrayList<Player>(); static { int i = 0; for (; i < names.length - 2; i++) { for (int j = 0; j < flowers.length; j++) { Card c = new Card(names[i], flowers[j], i); all.add(c); } } // 将大小王加入 all.add(new Card(names[names.length - 2], null, i++)); all.add(new Card(names[names.length - 1], null, i++)); } /** * 加入玩家 */ private void addPlayer() { Scanner sc = new Scanner(System.in); System.out.println("请输入玩家1名称:"); String name1 = sc.nextLine(); System.out.println("请输入玩家2名称:"); String name2 = sc.nextLine(); System.out.println("请输入玩家3名称:"); String name3 = sc.nextLine(); Player p1 = new Player(); p1.setId(1); p1.setName(name1); Player p2 = new Player(); p1.setId(2); p2.setName(name2); Player p3 = new Player(); p1.setId(3); p3.setName(name3); // 将三名玩家加入集合 players.add(p1); players.add(p2); players.add(p3); } /** * 随机地主 */ private void randomBoss() { // 添加玩家 addPlayer(); // 随机地主索引 int i = randomGen.nextInt(players.size()); // 设置指定位置的玩家为地主 players.get(i).setBoss(true); } public ArrayList<Player> sendCard() { randomBoss(); // 对每一名玩家遍历 for (Player p : players) { // 先为每一位玩家随机发17张牌 for (int i = 0; i < 17; i++) { // 随机一张牌的索引 int cardIndex = randomGen.nextInt(all.size()); // 获取随机索引位置的牌 Card card = all.get(cardIndex); // 将随机的牌加入当前遍历玩家的集合 p.getCards().add(card); // 从源集合中移除这张牌 all.remove(card); } } // 最后三张牌给地主 for (Player p : players) { if (p.isBoss()) { // 将all集合中的所有元素加入地主的集合 p.getCards().addAll(all); } } return players; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。