Java 实现 川岛博士--回溯计算游戏

这篇文章介绍了如何使用Java实现一个川岛博士风格的回溯计算游戏,包括游戏界面组件设计、随机题目生成和用户输入验证功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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("");
    }
}

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值