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

模拟斗地主发牌功能的设计与实现

程序员文章站 2022-07-09 14:30:09
模拟斗地主发牌功能的设计与实现参考斗地主的游戏规则,完成一个发牌的功能(54张牌,考虑点数,花色;三名玩家,其中地主比其他玩家多3张牌)...

模拟斗地主发牌功能的设计与实现

参考斗地主的游戏规则,完成一个发牌的功能(54张牌,考虑点数,花色;三名玩家,其中地主比其他玩家多3张牌)

牌类

牌有牌面值、花色及实际大小三个属性

import java.util.Objects;

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 flower + name + "-" + num + "点";
		}
		return name + "-" + num + "点";
	}
}

玩家类

玩家有编号、姓名、是否是地主、手中牌集合四个属性

import java.util.ArrayList;

public class Player {
	
	private int id;
	private String name;
	private boolean boss;
	private ArrayList<Card> cards = new ArrayList<>();
	public Player() {
		super();
	}
	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 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;
	}
}

游戏类

实现一副扑克牌(54张),并实现随机一位地主和根据玩家角色分发牌的功能。
1、定义牌集合(动态数组)、牌面值集合(不含大小王)、牌花色集合、玩家集合(动态数组)、随机数生成器
2、用笛卡尔积组合牌面值和花色得到52张牌,添加进牌集合,再添加大小王
3、创建三名玩家对象并添加进玩家集合
4、在玩家集合中根据动态数组下标随机一位地主
5、遍历玩家集合得到三位玩家,每位玩家在牌集合里随机抽取17次,每抽取一张,牌数组删除一张,玩家牌集合添加一张,最后剩下三张加入地主的牌集合,完成发牌

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class GameManage {

	/**牌集合*/
	private static ArrayList<Card> all = new ArrayList<>();
	/**牌面值集合*/
	private static String[] names = { "3", "4", "5", "6", "7", "8", "9", "10", 
										"J", "Q", "K", "A", "2", "Queen","King" };
	/**花色集合*/
	private static String[] flowers = { "黑桃", "红桃", "梅花", "方块" };
	/**玩家集合*/
	private ArrayList<Player> players = new ArrayList<>();
	/**随机数生成器*/
	private static Random randomGen = new Random();
	
	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++));
		
		for (Card card : all) {
			System.out.println(card);
		}
		System.out.println("=========================");
	}
	
	/**
	 * 添加玩家
	 */
	public 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();
		p2.setId(2);
		p2.setName(name2);
		Player p3 = new Player();
		p3.setId(3);
		p3.setName(name3);
		players.add(p1);
		players.add(p2);
		players.add(p3);
	}
	
	/**
	 * 随机地主
	 */
	public void randomBoss(){
		addPlayer();
		int i = randomGen.nextInt(players.size());
		players.get(i).setBoss(true);
	}
	
	/**
	 * 发牌
	 * @return 
	 */
	public ArrayList<Player> sendCard(){
		randomBoss();
		for (Player p : players) {
			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()) {			
				p.getCards().addAll(all);
			}
		}
		return players;
	}
	
	public static void main(String[] args) {
		ArrayList<Player> playerss = new GameManage().sendCard();
		for (Player p : playerss) {
			System.out.println(p);
		}
	}
}

本文地址:https://blog.csdn.net/GeiWo_YunCheYao/article/details/109899060