用Java实现图形化界面的21点游戏
时间: 2023-12-19 11:06:04 浏览: 154
以下是一个简单的21点游戏的Java图形化界面实现,你可以在此基础上进行扩展和优化。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
public class TwentyOneGame extends JFrame implements ActionListener {
private final int WIDTH = 800;
private final int HEIGHT = 600;
private final int CARD_WIDTH = 71;
private final int CARD_HEIGHT = 96;
private JLabel playerLabel;
private JLabel dealerLabel;
private JLabel resultLabel;
private JButton hitButton;
private JButton standButton;
private JButton newGameButton;
private ArrayList<Card> deck;
private ArrayList<Card> playerHand;
private ArrayList<Card> dealerHand;
private int playerScore;
private int dealerScore;
public TwentyOneGame() {
setTitle("Twenty-One Game");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
add(panel);
playerLabel = new JLabel("Player's Hand");
dealerLabel = new JLabel("Dealer's Hand");
resultLabel = new JLabel("");
hitButton = new JButton("Hit");
standButton = new JButton("Stand");
newGameButton = new JButton("New Game");
hitButton.addActionListener(this);
standButton.addActionListener(this);
newGameButton.addActionListener(this);
panel.add(playerLabel);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(dealerLabel);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(hitButton);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(standButton);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(newGameButton);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(resultLabel);
newGame();
}
private void newGame() {
deck = new ArrayList<>();
for (Suit suit : Suit.values()) {
for (Rank rank : Rank.values()) {
deck.add(new Card(rank, suit));
}
}
Collections.shuffle(deck);
playerHand = new ArrayList<>();
dealerHand = new ArrayList<>();
playerHand.add(dealCard());
playerHand.add(dealCard());
dealerHand.add(dealCard());
dealerHand.add(dealCard());
playerScore = calculateScore(playerHand);
dealerScore = calculateScore(dealerHand);
updatePlayerLabel();
updateDealerLabel();
updateResultLabel();
hitButton.setEnabled(true);
standButton.setEnabled(true);
}
private Card dealCard() {
return deck.remove(0);
}
private int calculateScore(ArrayList<Card> hand) {
int score = 0;
for (Card card : hand) {
if (card.getRank().getValue() == 1) {
if (score + 11 <= 21) {
score += 11;
} else {
score += 1;
}
} else {
score += Math.min(card.getRank().getValue(), 10);
}
}
return score;
}
private void updatePlayerLabel() {
playerLabel.setText("Player's Hand (" + playerScore + "):");
JPanel playerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
for (Card card : playerHand) {
playerPanel.add(new JLabel(new ImageIcon(card.getImage().getScaledInstance(CARD_WIDTH, CARD_HEIGHT, Image.SCALE_SMOOTH))));
}
playerLabel.setLabelFor(playerPanel);
playerLabel.add(playerPanel);
}
private void updateDealerLabel() {
dealerLabel.setText("Dealer's Hand (" + dealerHand.get(0).getRank().getValue() + "):");
JPanel dealerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
dealerPanel.add(new JLabel(new ImageIcon(dealerHand.get(0).getImage().getScaledInstance(CARD_WIDTH, CARD_HEIGHT, Image.SCALE_SMOOTH))));
if (dealerHand.size() == 2) {
dealerPanel.add(new JLabel(new ImageIcon(Card.getBackImage().getScaledInstance(CARD_WIDTH, CARD_HEIGHT, Image.SCALE_SMOOTH))));
}
dealerLabel.setLabelFor(dealerPanel);
dealerLabel.add(dealerPanel);
}
private void updateResultLabel() {
if (playerScore > 21) {
resultLabel.setText("Player busts! Dealer wins!");
hitButton.setEnabled(false);
standButton.setEnabled(false);
} else if (dealerScore > 21) {
resultLabel.setText("Dealer busts! Player wins!");
hitButton.setEnabled(false);
standButton.setEnabled(false);
} else if (!hitButton.isEnabled()) {
if (playerScore > dealerScore) {
resultLabel.setText("Player wins!");
} else if (playerScore < dealerScore) {
resultLabel.setText("Dealer wins!");
} else {
resultLabel.setText("Push!");
}
}
}
private void dealerTurn() {
while (dealerScore < 17) {
dealerHand.add(dealCard());
dealerScore = calculateScore(dealerHand);
updateDealerLabel();
}
hitButton.setEnabled(false);
standButton.setEnabled(false);
updateResultLabel();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == hitButton) {
playerHand.add(dealCard());
playerScore = calculateScore(playerHand);
updatePlayerLabel();
if (playerScore > 21) {
dealerTurn();
}
} else if (e.getSource() == standButton) {
dealerTurn();
} else if (e.getSource() == newGameButton) {
for (Component component : playerLabel.getComponents()) {
if (component instanceof JPanel) {
playerLabel.remove(component);
}
}
for (Component component : dealerLabel.getComponents()) {
if (component instanceof JPanel) {
dealerLabel.remove(component);
}
}
resultLabel.setText("");
newGame();
}
}
public static void main(String[] args) {
TwentyOneGame game = new TwentyOneGame();
game.setVisible(true);
}
}
```
此代码中用到了一个 `Card` 类,用于表示扑克牌,其中包括花色(`Suit`)和点数(`Rank`)两个枚举类型,以及获取对应图片的方法。
```java
import javax.swing.*;
import java.awt.*;
public class Card {
private final Rank rank;
private final Suit suit;
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
public Image getImage() {
String filename = "cards/" + rank.getValue() + suit.getName() + ".png";
return new ImageIcon(filename).getImage();
}
public static Image getBackImage() {
return new ImageIcon("cards/back.png").getImage();
}
}
```
枚举类型 `Rank` 和 `Suit` 的定义如下:
```java
public enum Rank {
ACE(1, "A"), TWO(2, "2"), THREE(3, "3"), FOUR(4, "4"), FIVE(5, "5"), SIX(6, "6"),
SEVEN(7, "7"), EIGHT(8, "8"), NINE(9, "9"), TEN(10, "10"), JACK(10, "J"), QUEEN(10, "Q"), KING(10, "K");
private final int value;
private final String name;
Rank(int value, String name) {
this.value = value;
this.name = name;
}
public int getValue() {
return value;
}
public String getName() {
return name;
}
}
public enum Suit {
CLUBS("C"), DIAMONDS("D"), HEARTS("H"), SPADES("S");
private final String name;
Suit(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
```
阅读全文