欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

游戏开发--斗地主发牌功能

程序员文章站 2022-03-23 22:13:48
老师指导下写的。...

斗地主发牌功能

参考斗地主的游戏规则,完成一个发牌的功能(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;
	}

}

本文地址:https://blog.csdn.net/qq_40269704/article/details/109954226

相关标签: java