Java 实现 川岛博士–回溯计算游戏
想法来源dy刷到的游戏机 花了点时间写了个勉强能玩的
dy–》 玩游戏的泡泡大酱
游戏玩法 : 举例2回溯答题 首先算了你们看那个博主视频吧
/**
* AutumnYe
* 2023/9/5
* */
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
public class GameWin extends JFrame implements ActionListener {
public static int level = 2; // 回溯量
public static int volume = 10; // 做题量
public static final int FRAME_W = 600;
public static final int FRAME_H = 500;
public static final int SCREEN_W = Toolkit.getDefaultToolkit().getScreenSize().width;
public static final int SCREEN_H = Toolkit.getDefaultToolkit().getScreenSize().height;
public static final int FRAME_X = (SCREEN_W - FRAME_W) / 2;
public static final int FRAME_Y = (SCREEN_H - FRAME_H) / 2;
private final JLabel topLabel = new JLabel("", JLabel.CENTER);
private final JLabel centreLabel = new JLabel("", JLabel.CENTER);
private final JLabel bottomLabel = new JLabel("", JLabel.CENTER);
private final JTextField ansTextField = new JTextField("在此处填写答案");
private final JButton confirmBt = new JButton("确定");
void init() {
this.setVisible(true);
this.setSize(FRAME_W, FRAME_H);
this.setLocation(FRAME_X, FRAME_Y);
this.setResizable(false);
// this.setBackground(Color.red);
this.setTitle("川岛博士--回溯计算游戏");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void addComponent() {
setLayout(new FlowLayout());
Dimension ds = new Dimension(500, 50);
Font font = new Font("微软雅黑", Font.BOLD, 40);
String str = String.format("%d 回溯 %d 题", level, volume);
this.topLabel.setPreferredSize(ds);
this.topLabel.setFont(font);
this.topLabel.setText(str);
str = "题目显示区";
this.centreLabel.setPreferredSize(ds);
this.centreLabel.setFont(font);
this.centreLabel.setText(str);
str = "将要回答的题目";
this.bottomLabel.setPreferredSize(ds);
this.bottomLabel.setFont(font);
this.bottomLabel.setText(str);
this.add(topLabel);
this.add(centreLabel);
this.add(bottomLabel);
this.ansTextField.setPreferredSize(new Dimension(300, 50));
this.confirmBt.setPreferredSize(new Dimension(300, 50));
confirmBt.addActionListener(this);
this.add(ansTextField);
this.add(confirmBt);
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
refreshData();
gameWin.init();
gameWin.addComponent();
}
public static int[] answers = new int[50];
public static String[] strings = new String[50];
public static String unDisplayString = "? * ? = ?";
static void refreshData() {
int a, b;
for (int i = 0; i < volume; i++) {
answers[i] = ThreadLocalRandom.current().nextInt(10);
a = ThreadLocalRandom.current().nextInt(10);
b = answers[i] - a;
if (b < 0) {
strings[i] = String.format("%d - %d = ?", a, -b);
} else {
strings[i] = String.format("%d + %d = ?", a, b);
}
System.out.println(strings[i] + answers[i]);
}
}
public boolean state = true;
public int count = 0, countRight = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (count == 0) {
confirmBt.setText("下一题");
}
// 4类情况讨论
if (count < level) {
String disStr = String.format("第 %d 题 ", 1 + count) + strings[count];
centreLabel.setText(disStr);
bottomLabel.setText(" ");
} else if (count < volume) {
String disStr = String.format("第 %2d 题 ", 1 + count) + strings[count];
centreLabel.setText(disStr);
disStr = String.format("第 %2d 题 ", 1 + count - level) + unDisplayString;
bottomLabel.setText(disStr);
} else if (count < level + volume) {
centreLabel.setText(" ");
String disStr = String.format("第 %2d 题 ", 1 + count - level) + unDisplayString;
bottomLabel.setText(disStr);
} else if (count > level + volume + 1) {
refreshData();
count = -1;
countRight = 0;
confirmBt.setText("重新开始");
}
// 要过个回合再结算烦 瑕疵不想改了
if (count > level) {
String ansStr = ansTextField.getText();
System.out.println(ansStr + " " + answers[count - level - 1]);
if (Objects.equals(ansStr, answers[count - level - 1] + "")) {
countRight++;
}
}
count++;
topLabel.setText(String.format("答对 %d 题", countRight));
ansTextField.setText("");
}
}