模拟斗地主扑克牌发牌
- 一、功能介绍
- 二、功能代码实现
- 1. 定义Card类
- 2. 定义Player类
- 3. 定义测试类
- 三、演示效果图
非常抱歉,之前写的代码有个发牌bug,导致三个人的牌是一样的,我没有注意到,感谢
@J_落羽
的提醒,目前这个bug已经修复,代码中有注释,位置是CardGame.java
的deal()方法
一、功能介绍
功能需求:
- 创建Card类,定义花色suit和点数rank的属性
- 创建玩家类 Player类 ,定义名字name ,手牌 cards (List),角色role
- 定义测试类CardGame
- 创建一副扑克牌,54张
- 实现洗牌
- 实现轮流发牌
- 实现理牌
二、功能代码实现
1. 定义Card类
Card.java
package com.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() {
}
public Card(int suit, int rank) {
this.suit = suit;
this.rank = rank;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
public int getRank() {
return 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()]);
}
}
2. 定义Player类
Player.java
package com.feonix;
import java.util.List;
public class Player {
private String name;
private String role;
private List<Card> cards;
public Player() {
}
public Player(String name, List<Card> cards) {
this.name = name;
this.cards = cards;
this.role = "农民";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public List<Card> getCards() {
return 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());
}
}
3. 定义测试类
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;
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++;
}
}
}
}
三、演示效果图
这是一开始的错误示例
下面才是正确的效果图
一个简单的扑克牌发牌小功能,今天就先分享到这里了,下次有了新的想法再拿来和大家分享。
====================================== 分隔线 ==========================================