java实现swing的24点纸牌游戏
时间: 2023-06-24 17:03:58 浏览: 184
以下是一个简单的 Java Swing 24 点纸牌游戏的实现,包括随机生成纸牌、计算表达式、判断胜负等操作。
首先,定义一个 Card 类表示一张纸牌,包括点数和花色属性:
```java
public class Card {
private int point;
private String suit;
public Card(int point, String suit) {
this.point = point;
this.suit = suit;
}
public int getPoint() {
return point;
}
public String getSuit() {
return suit;
}
}
```
接下来,定义一个 CardUtil 工具类,包括生成一副牌、洗牌、随机抽牌等操作:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CardUtil {
private static final String[] SUITS = {"♠", "♥", "♣", "♦"};
private static final int[] POINTS = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
public static List<Card> generateCards() {
List<Card> cards = new ArrayList<>();
for (String suit : SUITS) {
for (int point : POINTS) {
cards.add(new Card(point, suit));
}
}
return cards;
}
public static void shuffleCards(List<Card> cards) {
Collections.shuffle(cards);
}
public static Card drawCard(List<Card> cards) {
if (cards.isEmpty()) {
return null;
}
return cards.remove(0);
}
}
```
然后,定义一个 GamePanel 类表示游戏界面,包括纸牌显示区域、计算表达式、判断胜负等操作:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class GamePanel extends JPanel implements ActionListener {
private List<Card> cards;
private List<JButton> cardButtons;
private JLabel expressionLabel;
private JLabel resultLabel;
private JButton clearButton;
private JButton submitButton;
public GamePanel() {
setPreferredSize(new Dimension(800, 600));
// 生成一副牌并洗牌
cards = CardUtil.generateCards();
CardUtil.shuffleCards(cards);
// 初始化纸牌显示区域
cardButtons = new ArrayList<>();
for (int i = 0; i < 4; i++) {
JButton button = new JButton(cards.get(i).getPoint() + cards.get(i).getSuit());
button.addActionListener(this);
cardButtons.add(button);
add(button);
}
// 初始化表达式和结果标签
expressionLabel = new JLabel("");
expressionLabel.setFont(new Font("Arial", Font.PLAIN, 24));
add(expressionLabel);
resultLabel = new JLabel("");
resultLabel.setFont(new Font("Arial", Font.PLAIN, 24));
add(resultLabel);
// 初始化清除和提交按钮
clearButton = new JButton("清除");
clearButton.addActionListener(this);
add(clearButton);
submitButton = new JButton("提交");
submitButton.addActionListener(this);
add(submitButton);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == clearButton) {
// 清除表达式和结果
expressionLabel.setText("");
resultLabel.setText("");
} else if (source == submitButton) {
// 计算表达式并判断胜负
String expression = expressionLabel.getText();
if (expression.isEmpty()) {
resultLabel.setText("请输入表达式");
} else {
double result = calculateExpression(expression);
if (result == 24) {
resultLabel.setText("胜利!");
} else {
resultLabel.setText("失败!");
}
}
} else if (cardButtons.contains(source)) {
// 添加或删除纸牌到表达式
JButton button = (JButton) source;
String buttonText = button.getText();
if (expressionLabel.getText().contains(buttonText)) {
expressionLabel.setText(expressionLabel.getText().replace(buttonText, ""));
} else {
expressionLabel.setText(expressionLabel.getText() + buttonText);
}
}
}
private double calculateExpression(String expression) {
Stack<Double> stack = new Stack<>();
String[] tokens = expression.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");
for (String token : tokens) {
if (token.matches("\\d+")) {
stack.push(Double.parseDouble(token));
} else if (token.matches("[+\\-*/]")) {
double b = stack.pop();
double a = stack.pop();
switch (token) {
case "+":
stack.push(a + b);
break;
case "-":
stack.push(a - b);
break;
case "*":
stack.push(a * b);
break;
case "/":
stack.push(a / b);
break;
}
}
}
return stack.pop();
}
}
```
最后,定义一个 Main 类作为程序入口,创建一个 JFrame 并添加 GamePanel:
```java
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("24点纸牌游戏");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new GamePanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
运行程序,即可开始游戏。
阅读全文