java编写斗地主
时间: 2023-10-17 08:14:20 浏览: 96
斗地主是一款非常经典的扑克牌游戏,下面是一个简单的 Java 实现斗地主的示例代码:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class DouDiZhu {
private static final int NUM_CARDS = 54;
private static final int NUM_PLAYERS = 3;
private static final int NUM_CARDS_EACH = 17;
private static final int CARD_WIDTH = 5;
private static final int CARD_HEIGHT = 7;
private static final int CARD_BLANK = 3;
private static final String[] CARD_VALUES = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "小王", "大王"};
private static final String[] CARD_SUITS = {"♠", "♥", "♣", "♦"};
private ArrayList<Integer> cards;
private ArrayList<Integer>[] players;
private int landlord;
private Scanner scanner;
public DouDiZhu() {
cards = new ArrayList<>();
players = new ArrayList[NUM_PLAYERS];
for (int i = 0; i < NUM_PLAYERS; i++) {
players[i] = new ArrayList<>();
}
scanner = new Scanner(System.in);
}
public void start() {
initCards();
shuffleCards();
dealCards();
chooseLandlord();
playCards();
}
private void initCards() {
for (int i = 0; i < NUM_CARDS; i++) {
cards.add(i);
}
}
private void shuffleCards() {
Collections.shuffle(cards, new Random());
}
private void dealCards() {
for (int i = 0; i < NUM_PLAYERS; i++) {
for (int j = 0; j < NUM_CARDS_EACH; j++) {
players[i].add(cards.remove(0));
}
}
}
private void chooseLandlord() {
System.out.println("请选择地主:");
System.out.println("1.玩家1");
System.out.println("2.玩家2");
System.out.println("3.玩家3");
int choice = scanner.nextInt();
while (choice < 1 || choice > 3) {
System.out.println("选择无效,请重新选择:");
choice = scanner.nextInt();
}
landlord = choice - 1;
System.out.println("玩家" + (landlord + 1) + "成为了地主,手牌为:");
printCards(players[landlord]);
}
private void playCards() {
int currentPlayer = landlord;
boolean gameOver = false;
while (!gameOver) {
if (players[currentPlayer].isEmpty()) {
System.out.println("玩家" + (currentPlayer + 1) + "获胜!");
gameOver = true;
break;
}
System.out.println("轮到玩家" + (currentPlayer + 1) + "出牌,手牌为:");
printCards(players[currentPlayer]);
System.out.print("请输入要出的牌(用逗号分隔,如 1,2,3):");
String input = scanner.next();
String[] cardStrings = input.split(",");
ArrayList<Integer> cards = new ArrayList<>();
for (String cardString : cardStrings) {
try {
int card = Integer.parseInt(cardString) - 1;
if (card >= 0 && card < players[currentPlayer].size()) {
cards.add(players[currentPlayer].get(card));
players[currentPlayer].remove(card);
}
} catch (NumberFormatException e) {
// ignore
}
}
if (cards.isEmpty()) {
System.out.println("出牌无效,请重新输入!");
continue;
}
System.out.println("玩家" + (currentPlayer + 1) + "出的牌为:");
printCards(cards);
currentPlayer = (currentPlayer + 1) % NUM_PLAYERS;
}
}
private void printCards(ArrayList<Integer> cards) {
for (int i = 0; i < cards.size(); i++) {
int card = cards.get(i);
System.out.print(getCardString(card));
if (i < cards.size() - 1) {
System.out.print(",");
}
}
System.out.println();
}
private void printCards(int[] cards) {
for (int i = 0; i < cards.length; i++) {
System.out.print(getCardString(cards[i]));
if (i < cards.length - 1) {
System.out.print(",");
}
}
System.out.println();
}
private String getCardString(int card) {
int value = card / 4;
int suit = card % 4;
String result = CARD_VALUES[value] + CARD_SUITS[suit];
if (value == 13) {
result = CARD_VALUES[value + suit] + CARD_SUITS[suit];
}
return result;
}
public static void main(String[] args) {
DouDiZhu game = new DouDiZhu();
game.start();
}
}
```
这个程序实现了斗地主的基本逻辑,包括初始化牌组、洗牌、发牌、选择地主和出牌等操作。游戏中每个玩家的手牌由一个整数列表表示,每张牌用一个整数表示,整数的值为 0 到 53,分别表示扑克牌的 54 种可能。程序还提供了简单的命令行界面,使玩家可以选择出牌,从而体验斗地主的乐趣。
阅读全文