java实现斗地主游戏
时间: 2023-10-29 11:08:44 浏览: 182
实现斗地主游戏需要以下几步:
1.准备工作
定义一个扑克牌的类,包括花色和点数,定义一个牌堆类,包括洗牌、发牌、排序等功能。
2.发牌
将牌堆中的牌洗乱,然后依次发给三个玩家和底牌。
3.叫地主
首先随机确定一个玩家为地主,然后其他两个玩家可以选择叫分或者不叫,每个玩家叫分的分数必须比前一个玩家高,直到分数最高的玩家成为地主。
4.出牌
地主先出牌,然后其他玩家依次出牌,出牌规则是必须比上一个玩家出的牌大,如果无法出牌则需要过牌。
5.结束游戏
当有一个玩家手中的牌出完或者其他玩家放弃出牌时,游戏结束,此时统计各个玩家的得分,包括出完牌的得分和叫分的得分。
下面是一个简单的实现代码,仅供参考:
```
public class DouDiZhu {
private static final int CARD_NUM = 54;
private static final int PLAYER_NUM = 3;
private List<PokerCard> cardList = new ArrayList<>();
private List<PokerCard> player1 = new ArrayList<>();
private List<PokerCard> player2 = new ArrayList<>();
private List<PokerCard> player3 = new ArrayList<>();
private List<PokerCard> bottomCards = new ArrayList<>();
private int landlordIndex;
public void initCardList() {
for (int i = 1; i <= 13; i++) {
for (int j = 1; j <= 4; j++) {
cardList.add(new PokerCard(i, j));
}
}
cardList.add(new PokerCard(0, 5)); // 小王
cardList.add(new PokerCard(0, 6)); // 大王
}
public void shuffle() {
Collections.shuffle(cardList);
}
public void dealCards() {
for (int i = 0; i < CARD_NUM - 3; i++) {
if (i % PLAYER_NUM == 0) {
player1.add(cardList.get(i));
} else if (i % PLAYER_NUM == 1) {
player2.add(cardList.get(i));
} else {
player3.add(cardList.get(i));
}
}
bottomCards.add(cardList.get(CARD_NUM - 3));
bottomCards.add(cardList.get(CARD_NUM - 2));
bottomCards.add(cardList.get(CARD_NUM - 1));
}
public void callLandlord() {
Random random = new Random();
landlordIndex = random.nextInt(PLAYER_NUM);
System.out.println("玩家" + (landlordIndex + 1) + "成为地主");
int score = 0;
int currentPlayerIndex = landlordIndex;
while (score < 3) {
currentPlayerIndex = (currentPlayerIndex + 1) % PLAYER_NUM;
System.out.println("玩家" + (currentPlayerIndex + 1) + "请叫分(0-3):");
Scanner sc = new Scanner(System.in);
int callScore = sc.nextInt();
if (callScore > score) {
score = callScore;
landlordIndex = currentPlayerIndex;
}
}
System.out.println("地主是玩家" + (landlordIndex + 1) + ",底牌是:");
for (PokerCard card : bottomCards) {
System.out.print(card + " ");
}
System.out.println();
}
public void playCards() {
List<PokerCard> currentCards = null;
int currentPlayerIndex = landlordIndex;
while (!player1.isEmpty() && !player2.isEmpty() && !player3.isEmpty()) {
if (currentPlayerIndex == 0) {
currentCards = player1;
} else if (currentPlayerIndex == 1) {
currentCards = player2;
} else {
currentCards = player3;
}
System.out.println("当前出牌人是玩家" + (currentPlayerIndex + 1));
System.out.print("当前手牌:");
for (PokerCard card : currentCards) {
System.out.print(card + " ");
}
System.out.println();
System.out.print("上一手牌:");
for (PokerCard card : lastCards) {
System.out.print(card + " ");
}
System.out.println();
System.out.println("请出牌(不出请输0):");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
if (input.equals("0")) {
System.out.println("玩家" + (currentPlayerIndex + 1) + "选择不出牌");
} else {
List<PokerCard> selectedCards = new ArrayList<>();
String[] cardStrs = input.split(" ");
for (String cardStr : cardStrs) {
String[] parts = cardStr.split("-");
int num = Integer.parseInt(parts[0]);
int suit = Integer.parseInt(parts[1]);
PokerCard card = new PokerCard(num, suit);
selectedCards.add(card);
}
if (PokerUtil.isCardsValid(selectedCards) && PokerUtil.isCardsLarger(selectedCards, lastCards)) {
currentCards.removeAll(selectedCards);
lastCards = selectedCards;
} else {
System.out.println("出牌不符合规则");
}
}
currentPlayerIndex = (currentPlayerIndex + 1) % PLAYER_NUM;
}
}
public void start() {
initCardList();
shuffle();
dealCards();
callLandlord();
playCards();
}
public static void main(String[] args) {
DouDiZhu game = new DouDiZhu();
game.start();
}
}
class PokerCard {
private int num; // 点数
private int suit; // 花色
public PokerCard(int num, int suit) {
this.num = num;
this.suit = suit;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
@Override
public String toString() {
String suitStr = "";
switch (suit) {
case 1:
suitStr = "♠";
break;
case 2:
suitStr = "♥";
break;
case 3:
suitStr = "♣";
break;
case 4:
suitStr = "♦";
break;
case 5:
suitStr = "小王";
break;
case 6:
suitStr = "大王";
break;
}
String numStr = "";
switch (num) {
case 1:
numStr = "A";
break;
case 11:
numStr = "J";
break;
case 12:
numStr = "Q";
break;
case 13:
numStr = "K";
break;
default:
numStr = String.valueOf(num);
break;
}
return suitStr + numStr;
}
}
class PokerUtil {
public static boolean isCardsValid(List<PokerCard> cards) {
if (cards == null || cards.size() == 0) {
return false;
}
if (cards.size() == 1) {
return true;
}
if (cards.size() == 2) {
if (cards.get(0).getNum() == cards.get(1).getNum()) {
return true;
}
return false;
}
if (cards.size() == 3) {
if (cards.get(0).getNum() == cards.get(1).getNum() && cards.get(0).getNum() == cards.get(2).getNum()) {
return true;
}
return false;
}
if (cards.size() == 4) {
if (cards.get(0).getNum() == cards.get(1).getNum() && cards.get(0).getNum() == cards.get(2).getNum() && cards.get(0).getNum() == cards.get(3).getNum()) {
return true;
}
if (cards.get(0).getNum() == cards.get(1).getNum() && cards.get(0).getNum() == cards.get(2).getNum()) {
return cards.get(3).getNum() == cards.get(2).getNum();
}
if (cards.get(1).getNum() == cards.get(2).getNum() && cards.get(1).getNum() == cards.get(3).getNum()) {
return cards.get(0).getNum() == cards.get(1).getNum();
}
return false;
}
if (cards.size() == 5) {
if (cards.get(0).getNum() == cards.get(1).getNum() && cards.get(0).getNum() == cards.get(2).getNum() && cards.get(0).getNum() == cards.get(3).getNum()) {
return cards.get(4).getNum() == cards.get(3).getNum();
}
if (cards.get(1).getNum() == cards.get(2).getNum() && cards.get(1).getNum() == cards.get(3).getNum() && cards.get(1).getNum() == cards.get(4).getNum()) {
return cards.get(0).getNum() == cards.get(1).getNum();
}
if (cards.get(0).getNum() == cards.get(1).getNum() && cards.get(0).getNum() == cards.get(2).getNum() && cards.get(3).getNum() == cards.get(4).getNum()) {
return true;
}
return false;
}
return false;
}
public static boolean isCardsLarger(List<PokerCard> cards, List<PokerCard> lastCards) {
if (lastCards == null || lastCards.size() == 0) {
return true;
}
if (cards.size() != lastCards.size()) {
return false;
}
int num1 = getCardsNum(cards);
int num2 = getCardsNum(lastCards);
if (num1 > num2) {
return true;
}
if (num1 < num2) {
return false;
}
return cards.get(cards.size() - 1).getNum() > lastCards.get(lastCards.size() - 1).getNum();
}
private static int getCardsNum(List<PokerCard> cards) {
if (isSingle(cards)) {
return 1;
}
if (isDouble(cards)) {
return 2;
}
if (isTriple(cards)) {
return 3;
}
if (isBomb(cards)) {
return 4;
}
if (isStraight(cards)) {
return cards.size();
}
if (isDoubleStraight(cards)) {
return cards.size() / 2;
}
return 0;
}
private static boolean isSingle(List<PokerCard> cards) {
return cards.size() == 1;
}
private static boolean isDouble(List<PokerCard> cards) {
return cards.size() == 2 && cards.get(0).getNum() == cards.get(1).getNum();
}
private static boolean isTriple(List<PokerCard> cards) {
return cards.size() == 3 && cards.get(0).getNum() == cards.get(1).getNum() && cards.get(0).getNum() == cards.get(2).getNum();
}
private static boolean isBomb(List<PokerCard> cards) {
return cards.size() == 4 && cards.get(0).getNum() == cards.get(1).getNum() && cards.get(0).getNum() == cards.get(2).getNum() && cards.get(0).getNum() == cards.get(3).getNum();
}
private static boolean isStraight(List<PokerCard> cards) {
if (cards.size() < 5) {
return false;
}
for (int i = 1; i < cards.size(); i++) {
if (cards.get(i).getNum() != cards.get(i - 1).getNum() + 1) {
return false;
}
}
return true;
}
private static boolean isDoubleStraight(List<PokerCard> cards) {
if (cards.size() % 2 != 0) {
return false;
}
for (int i = 0; i < cards.size(); i += 2) {
if (cards.get(i).getNum() != cards.get(i + 1).getNum()) {
return false;
}
}
for (int i = 2; i < cards.size(); i += 2) {
if (cards.get(i).getNum() != cards.get(i - 2).getNum() + 1) {
return false;
}
}
return true;
}
}
```
阅读全文