非常抱歉,之前写的代码有个发牌bug,导致三个人的牌是一样的,我没有注意到,感谢
@J_落羽
的提醒,目前这个bug已经修复,代码中有注释,位置是CardGame.java
的deal()方法
功能需求:
Card.java
package com.feonix;
/**
* 卡牌类
*
* @author FeoniX
*
*/
public class Card {
/** 花色常量 */
static final String[] SUITS = { "♥", "♠", "♦", "♣", "" };
/** 点数常量 */
static final String[] RANKS = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "♚", "♔" };
/** 花色 */
private int suit;
/** 点数 */
private int rank;
/**
* 无参构造器
*/
public Card() {
}
/**
* 有参构造器
*
* @param suit
* @param rank
*/
public Card(int suit, int rank) {
this.suit = suit;
this.rank = rank;
}
/**
* 获取花色
*
* @return
*/
public int getSuit() {
return suit;
}
/**
* 设置花色
*
* @param suit
*/
public void setSuit(int suit) {
this.suit = suit;
}
/**
* 获取点数
*
* @return
*/
public int getRank() {
return rank;
}
/**
* 设置点数
*
* @param rank
*/
public void setRank(int rank) {
this.rank = rank;
}
@Override
public int hashCode() {
int secret = 0x000031;
int result = 1;
result = getSuit() * result + secret;
result = getRank() * result + secret;
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (obj instanceof Card) {
Card card = (Card) obj;
return card.getSuit() == getSuit() && card.getRank() == getRank();
}
return super.equals(obj);
}
@Override
public String toString() {
return String.format("%s%s", SUITS[getSuit()], RANKS[getRank()]);
}
}
Player.java
package com.feonix;
import java.util.List;
/**
* 玩家类
*
* @author FeoniX
*
*/
public class Player {
/** 玩家名字 */
private String name;
/** 玩家角色 */
private String role;
/** 玩家手牌 */
private List<Card> cards;
/**
* 无参构造器
*/
public Player() {
}
/**
* 有参构造器
*
* @param suit
* @param rank
*/
public Player(String name, List<Card> cards) {
this.name = name;
this.cards = cards;
this.role = "农民";
}
/**
* 获取名字
*
* @return
*/
public String getName() {
return name;
}
/**
* 设置名字
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取角色
*
* @return
*/
public String getRole() {
return role;
}
/**
* 设置角色
*
* @param role
*/
public void setRole(String role) {
this.role = role;
}
/**
* 获取手牌
*
* @return
*/
public List<Card> getCards() {
return cards;
}
/**
* 设置手牌
*
* @param cards
*/
public void setCards(List<Card> cards) {
this.cards = cards;
}
@Override
public int hashCode() {
int secret = 0x000031;
int result = 1;
result = getName().hashCode() * result + secret;
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (obj instanceof Player) {
Player palyer = (Player) obj;
return palyer.getName().equals(getName());
}
return super.equals(obj);
}
@Override
public String toString() {
return String.format("[%s]%s手上的牌:%s", getRole(), getName(), getCards().toString());
}
}
CardGame.java
package com.feonix;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
/**
* 扑克牌斗地主
*
* @author FeoniX
*
*/
public class CardGame {
/** 一副牌的集合 */
private static List<Card> poker = new ArrayList<Card>(54);
/** 底牌 */
private static List<Card> handCards = new ArrayList<Card>();
/** 玩家的集合 */
private static List<Player> players = new ArrayList<Player>();
/** 确定地主的那张牌 */
private static Card landlordCard;
public static void main(String[] args) {
initPlayers();
initCards();
deal();
System.out.println("洗牌前:" + poker + "\n");
// 洗牌
Collections.shuffle(poker);
System.out.println("洗牌后:" + poker + "\n");
System.out.println("底牌:" + handCards + "\n");
System.out.println("翻牌:" + landlordCard + "\n");
for (Player player : players) {
// 理牌
player.getCards().sort(new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o1.getRank() - o2.getRank();
}
});
// 输出玩家手上的牌
System.out.println(player + "\n");
}
}
/**
* 初始化玩家
*/
private static void initPlayers() {
players.add(new Player("甜甜", new ArrayList<Card>()));
players.add(new Player("石头", new ArrayList<Card>()));
players.add(new Player("代代", new ArrayList<Card>()));
}
/**
* 初始化生成一副牌
*/
private static void initCards() {
for (int i = 0; i < Card.RANKS.length; i++) {
Card card = null;
if (!"♚".equals(Card.RANKS[i]) && !"♔".equals(Card.RANKS[i])) {
// 生成其他牌
for (int j = 0; j < Card.SUITS.length - 1; j++) {
card = new Card(j, i);
poker.add(card);
}
} else {
// 生成大小王
card = new Card(4, i);
poker.add(card);
}
}
}
/**
* 发牌
*/
private static void deal() {
Random rand = new Random();
// 随机生成翻牌索引,确定地主
int landlordIndex = rand.nextInt(poker.size() - 3);
// 发牌
for (int i = 0; i < poker.size() - 3;) {
// Card card = poker.get(i); // 这行代码写错位置了,移到下面去
for (int j = 0; j < players.size(); j++) {
Card card = poker.get(i); // 获取扑克牌应该写在内循环中
if (i == landlordIndex) {
// 获取翻牌
landlordCard = card;
// 设置玩家角色是地主
players.get(j).setRole("地主");
// 获取底牌
handCards = poker.subList(poker.size() - 3, poker.size());
// 底牌发给地主
players.get(j).getCards().addAll(handCards);
}
// 给玩家发牌
players.get(j).getCards().add(card);
i++;
}
}
}
}
一个简单的扑克牌发牌小功能,今天就先分享到这里了,下次有了新的想法再拿来和大家分享。
====================================== 分隔线 ==========================================