java - 尝试从 "main-menu"类调用方法,但它被卡住了,但是当从方法类调用时,它工作得很好

标签 java

为了应对编码挑战,我决定制作游戏战舰。但是,当调用 runGameAgainstAi() 方法时,它拒绝工作,只是绘制网格而不是允许功能,它可能会卡在 while 循环中,但我无法查明确切的问题。任何想法表示赞赏。

runGameAgainstAI 代码:

public void runGameAgainstAI() throws IOException, InterruptedException {

    // create the CPU object and initialize variables for the game
    cpu = new CPU();
    title.setText("Player 1");
    cpuGameTurn = "Picking";
    btnFire.setEnabled(true);

    boolean hasWon = false;
    // game loop
    while (true) {

        sleep(100);

        // this is reserved for when the human player is selecting a cell
        // by firing a valid shot, cpuGameTurn is set to "Picked"
        if (cpuGameTurn.equals("Picking")) {
            servermsg.setText("Select a cell and fire!");
        } 

        // upon shooting, the cpu checks if the shot hit
        // this condition is met when the btnFire is clicked
        // while selecting a valid cell
        else if (cpuGameTurn.equals("Picked")) {
            // check if the shot hit the cpu ships
            boolean hit = cpu.checkHit(eb.getRclick(), eb.getCclick());
            // place a corresponding marker
            if (hit) {
                eb.placeHitMarker(eb.getRclick(), eb.getCclick());
            } else {
                eb.placeMissMarker(eb.getRclick(), eb.getCclick());
            }
            // create the shot record
            sl.insert(new ShotRecord(eb.getRclick(), eb.getCclick(), hit, "You"));
            txaSList.setText(sl.toString());

            // if the cpu lost break, hasWon will be true
            hasWon = cpu.checkLost();
            if (hasWon) {
                break;
            }
            // set the turn to cpu
            cpuGameTurn = "cpu";
        }

        // when it is the cpu turn
        else if (cpuGameTurn.equals("cpu")) {
            // get the cpu shot, and check if it hit
            int shot[] = cpu.shoot();
            boolean hit = pb.checkEnemyShot(shot[0], shot[1]);
            // create the shot record
            sl.insert(new ShotRecord(shot[0], shot[1], hit, "CPU"));
            txaSList.setText(sl.toString());

            // if the cpu won break, hasWon will be false
            if (pb.hasLost()) {
                break;
            }
            // set the turn back to waiting for human to shoot
            cpuGameTurn = "Picking";
        }


    }

    if (hasWon) {
        servermsg.setText("You won!");
    } else {
        servermsg.setText("You lost!");
    }



}

针对ai运行游戏的自测主要方法:

 else {
        try {
            cui = new ClientUI();
            cui.runGameAgainstAI();
        }

单独类中的主菜单代码尝试调用 runGameAgainstAI 方法

        else if (evt.getSource() == btnSinglePlayer) {
        try {
            ClientUI c = new ClientUI();
            c.runGameAgainstAI();


        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

    }

最佳答案

乍一看,代码会无限循环。需要更新 if 内的 cpuGameTurn...

cpuGameTurn = "Picking";
while (true) {

    sleep(100);

    if (cpuGameTurn.equals("Picking")) {
        servermsg.setText("Select a cell and fire!");
    } 
.
.
.

关于java - 尝试从 "main-menu"类调用方法,但它被卡住了,但是当从方法类调用时,它工作得很好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59831967/

相关文章:

java - 未在路径 : DexPathList for Samsung devices only. 上找到类 "LinearLayout"(应用商店构建)

java - Spring Integration + Amazon SQS 队列的工作示例

java - 将 jTable 与 MySQL 数据库连接时,java net beans IDE 8.0 中出现数组越界异常

java - 使用 Selenium WebDriver 按下按钮

java - 没有找到合适的驱动程序调用 getConnection

java - 检查菜单项切换的已更改监听器

java - 使用 J2ME 和蓝牙从手机向笔记本电脑发送命令

java - 开发产品的不同版本

java - JRI 和 ARIMA 集成

java - netty如何确定读取何时完成?