用JAVA gui界面 写一个扑克牌游戏开火车 代码
时间: 2023-07-01 10:16:34 浏览: 167
用Java编写的扑克牌游戏
4星 · 用户满意度95%
好的,以下是一个简单的Java GUI界面扑克牌游戏开火车的代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PokerGame extends JFrame {
private JPanel playerPanel, centerPanel, buttonPanel;
private JLabel[] playerLabels;
private JButton startButton, addButton, passButton;
private JTextArea infoArea;
private JScrollPane infoScroll;
private Deck deck;
private Hand[] hands;
private Hand train;
private int currentPlayer;
private boolean isStarted, isEnded;
public PokerGame() {
super("Poker Game");
deck = new Deck();
hands = new Hand[4];
for (int i = 0; i < 4; i++) {
hands[i] = new Hand();
}
train = new Hand();
playerPanel = new JPanel(new GridLayout(1, 4));
playerLabels = new JLabel[4];
for (int i = 0; i < 4; i++) {
playerLabels[i] = new JLabel("Player " + (i + 1));
playerPanel.add(playerLabels[i]);
}
centerPanel = new JPanel(new GridLayout(2, 1));
centerPanel.add(new JLabel("Train:"));
centerPanel.add(train);
startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startGame();
}
});
addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addCard();
}
});
addButton.setEnabled(false);
passButton = new JButton("Pass");
passButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pass();
}
});
passButton.setEnabled(false);
buttonPanel = new JPanel();
buttonPanel.add(startButton);
buttonPanel.add(addButton);
buttonPanel.add(passButton);
infoArea = new JTextArea(10, 30);
infoArea.setEditable(false);
infoScroll = new JScrollPane(infoArea);
Container c = getContentPane();
c.add(playerPanel, BorderLayout.NORTH);
c.add(centerPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
c.add(infoScroll, BorderLayout.EAST);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setVisible(true);
}
private void startGame() {
if (!isStarted) {
deck.shuffle();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 10; j++) {
hands[i].addCard(deck.dealCard());
}
}
currentPlayer = 0;
isStarted = true;
addButton.setEnabled(true);
passButton.setEnabled(true);
startButton.setEnabled(false);
updateInfo("Game started.\n");
updateHands();
updateTrain();
updatePlayer();
}
}
private void addCard() {
if (!isEnded) {
Card card = hands[currentPlayer].playCard();
if (card != null) {
train.addCard(card);
updateInfo("Player " + (currentPlayer + 1) + " added " + card + " to the train.\n");
updateHands();
updateTrain();
if (hands[currentPlayer].getCardCount() == 0) {
isEnded = true;
updateInfo("Player " + (currentPlayer + 1) + " wins!\n");
addButton.setEnabled(false);
passButton.setEnabled(false);
}
else {
currentPlayer = (currentPlayer + 1) % 4;
updatePlayer();
}
}
}
}
private void pass() {
if (!isEnded) {
updateInfo("Player " + (currentPlayer + 1) + " passed.\n");
currentPlayer = (currentPlayer + 1) % 4;
updatePlayer();
}
}
private void updateHands() {
for (int i = 0; i < 4; i++) {
playerLabels[i].setText("Player " + (i + 1) + " (" + hands[i].getCardCount() + ")");
}
}
private void updateTrain() {
centerPanel.remove(train);
centerPanel.add(train = new Hand());
centerPanel.revalidate();
centerPanel.repaint();
}
private void updatePlayer() {
updateInfo("Player " + (currentPlayer + 1) + "'s turn.\n");
}
private void updateInfo(String message) {
infoArea.append(message);
infoArea.setCaretPosition(infoArea.getDocument().getLength());
}
public static void main(String[] args) {
new PokerGame();
}
}
class Deck {
private Card[] cards;
private int topCard;
public Deck() {
cards = new Card[52];
topCard = 0;
for (int i = 0; i < 52; i++) {
cards[i] = new Card(i);
}
}
public void shuffle() {
for (int i = 51; i > 0; i--) {
int j = (int)(Math.random() * (i + 1));
Card temp = cards[i];
cards[i] = cards[j];
cards[j] = temp;
}
topCard = 0;
}
public Card dealCard() {
if (topCard < 52) {
return cards[topCard++];
}
else {
return null;
}
}
}
class Card {
private int rank, suit;
public Card(int index) {
rank = index % 13;
suit = index / 13;
}
public int getRank() {
return rank;
}
public int getSuit() {
return suit;
}
public String toString() {
String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
String[] suits = {"♠", "♥", "♦", "♣"};
return ranks[rank] + suits[suit];
}
}
class Hand extends JPanel {
private Card[] cards;
private int cardCount;
public Hand() {
cards = new Card[10];
cardCount = 0;
}
public int getCardCount() {
return cardCount;
}
public Card getCard(int index) {
if (index >= 0 && index < cardCount) {
return cards[index];
}
else {
return null;
}
}
public void addCard(Card card) {
if (cardCount < 10) {
cards[cardCount++] = card;
add(new JLabel(card.toString()));
}
}
public Card playCard() {
if (cardCount > 0) {
Card card = cards[0];
for (int i = 1; i < cardCount; i++) {
cards[i - 1] = cards[i];
}
cards[--cardCount] = null;
remove(0);
revalidate();
repaint();
return card;
}
else {
return null;
}
}
}
```
这个程序使用了Java的AWT和Swing库来实现GUI界面,同时使用了面向对象的编程思想来设计游戏逻辑。在游戏开始前,程序会生成一副扑克牌,并将其洗牌;然后程序会给四个玩家发牌,每个玩家有10张牌;接着玩家轮流出牌,每出一张牌就会添加到火车上,如果一名玩家没有牌了,他就赢了游戏。程序还提供了添加牌和放弃出牌的功能,同时会在右侧的信息区域显示当前游戏状态。
阅读全文