基于面向对象实现斗地主洗牌发牌功能

一、基于面向对象实现斗地主洗牌发牌功能

完成一个斗地主发牌,洗牌的功能
1.一共54张牌,除了大小王之外,每个点数(A-K)分为四种花色:“♥”,“♦”,“♣”,“♠”
Card类(id,字面点数,大小)
NormalCard(花色)
2.一共有三名玩家,每个玩家具备id,昵称,牌集合
Player(id,nickname,List,isBoss)
3.洗牌(Collections.shuffle())
4.发牌
4.1.随机一名地主
4.2发牌(按顺序)
4.3剩下最后三张牌给地址
5.显示发牌结果(根据点数大小排序显示)

二、解题思路

首先将54张牌放入牌集合中,在初始化三名玩家将三名玩家放入玩家集合当中,其次就是随机一名地主,再利用Collections类中的shuffle()方法对牌集合进行刷新来实现洗牌的功能,最后就是发牌过程,我们可以通过定义一个标记来指明谁谁来抽牌将51张牌分配完后最后再将3张牌分配给地主。

三、代码模块

Card.java

package com.softeem.lesson36.exp04;

/**
 * 	牌类
 * @author Administrator
 *
 */
public class Card implements Comparable<Card>{

	/**牌的id*/
	private int id;
	/**牌面值*/
	private String name;
	/**点数*/
	private int point;
	
	public Card(int id, String name, int point) {
		super();
		this.id = id;
		this.name = name;
		this.point = point;
	}
	
	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 int getPoint() {
		return point;
	}
	public void setPoint(int point) {
		this.point = point;
	}

	@Override
	public int compareTo(Card c) {
		
		return this.point - c.point;
	}
	
	@Override
	public String toString() {
		return name;
	}
}

NormalCar.java

package com.softeem.lesson36.exp04;

/**
 * 	普通牌类
 * @author Administrator
 *
 */
public class NormalCar extends Card{

	/**花色*/
	private String flower;
	
	public NormalCar(int id, String name, int point, String flower) {
		super(id, name, point);
		this.flower = flower;
	}


	public String getFlower() {
		return flower;
	}

	public void setFlower(String flower) {
		this.flower = flower;
	}


	@Override
	public String toString() {
		return flower + ":" + getName();
	}
	
}

Player.java

package com.softeem.lesson36.exp04;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.sun.corba.se.pept.transport.Connection;

/**
 * 玩家类
 * @author Administrator
 *
 */
public class Player {

	/**玩家id*/
	private int id;
	/**玩家昵称*/
	private String nickname;
	/**是否地主*/
	private boolean boos;
	/**玩家手中的所有牌*/
	private List<Card> cards = new ArrayList<Card>();
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public boolean isBoos() {
		return boos;
	}
	public void setBoos(boolean boos) {
		this.boos = boos;
	}
	public List<Card> getCards() {
		return cards;
	}
	public void setCards(List<Card> cards) {
		this.cards = cards;
	}
	@Override
	public String toString() {
		Collections.sort(cards);
		return "Player [id=" + id + ", nickname=" + nickname + ", boos=" + boos + ", cards=" + cards + "]";
	}
	
}

PokerGame.java

package com.softeem.lesson36.exp04;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class PokerGame {

	private static String[] names = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
	private static String[] flowers = {"♥","♦","♣","♠"};
	private static List<Card> allCards;
	private static int id;
	/**声明存储玩家的引用*/
	private List<Player> players;
	private Random r = new Random();
	
	static {
		allCards = new ArrayList<Card>();
		for (int i = 0; i < names.length; i++) {
			for (int j = 0; j < flowers.length; j++) {
				id++;
				//创建一张牌并加入集合
				allCards.add(new NormalCar(id, names[i],i + 1,flowers[j] ));
			}
		}
		allCards.add(new Card(++id, "小王", 666));
		allCards.add(new Card(++id, "大王", 888));
	}
	
	/**
	 * 初始化玩家
	 */
	public void initPlayer() {
		players = new ArrayList<Player>();
		Player p1 = new Player();
		p1.setId(1);
		p1.setNickname("艾希");
		
		Player p2 = new Player();
		p2.setId(2);
		p2.setNickname("盖伦");
		
		Player p3 = new Player();
		p3.setId(3);
		p3.setNickname("易大师");
		//将玩家加入集合
		players.add(p1);
		players.add(p2);
		players.add(p3);
		//随机地主
		randomBoss();
	}
	
	/**
	 * 随机地主
	 */
	public void randomBoss() {
		//随机地主的索引
		int i = r.nextInt(players.size());
		//设置指定索引的玩家为地主
		players.get(i).setBoos(true);
	}
	
	/**
	 * 发牌
	 */
	public void sendCard() {
		//声明临时变量表示该谁抽取
		int pos = 0;
		//遍历所有牌
		for (int i = 0; i < allCards.size() - 3; i++) {
			players.get(pos).getCards().add(allCards.get(i));
			//判断本次该由谁来抽牌
			pos = pos == 0 ? 1 : pos == 1 ? 2 : 0;			
			}
		for (Player p : players) {
			if(p.isBoos()) {
				//获取最后三张
				List<Card> cards = allCards.subList(allCards.size() - 3, allCards.size());
				//将最后三张牌给地主
				p.getCards().addAll(cards);
			}
		}
	}
	
	/**
	 * 显示所有牌
	 */
	public void show() {
		for (Player p : players) {
			System.out.println(p);
		}
	}
	
	/**
	 * 洗牌
	 */
	public void shuffleCard() {
		Collections.shuffle(allCards);
		Collections.shuffle(allCards);
	}
	
	public static void main(String[] args) throws IOException {
		PokerGame pg = new PokerGame();
		//初始化玩家
		pg.initPlayer();
		//洗牌
		pg.shuffleCard();
		//发牌
		pg.sendCard();
		//展示玩家牌
		pg.show();	
	}
}

运行展示
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值