java - 战舰代码不起作用

标签 java

这是我使用 Java 重新创建战舰的尝试。我决定仅使用一艘船来测试游戏的简单版本,并在游戏板上为该船指定一个具体位置。我发现我的代码有问题。无论我输入什么坐标,我最终都会“撞到”这艘船。

这是我迄今为止编写的所有代码:

import java.util.Scanner;

class GameBoard {

    Scanner input = new Scanner(System.in);                                         // scanner object

    String[][] board = {                                                            // game board

    {"_", " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10"},      
    {"A", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
    {"B", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
    {"C", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
    {"D", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},            
    {"E", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
    {"F", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
    {"G", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
    {"H", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
    {"I", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"},
    {"J", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]", "[ ]"}

    };

    boolean frigateIsAlive = true;                                                  // the ship is still alive

    int numOfHitsOnFrigate = 0;                                                     // number of hits the player made on the frigate

    String [] frigate = {board[1][1], board[1][2]};                                 // ship 


    public void createBoard(){                                                      // draws the battleship game board
        for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[row].length; col++) {
                System.out.print(board[row][col] + "\t");

            } // inner loop
            System.out.println();
            System.out.println();
            System.out.println();
        } // outer loop
}

    public String getUserGuess() {                                                  // takes the users guess

        System.out.println("Choose a coordinate on the board to fire at");
        int x = input.nextInt();                                                        
        int y = input.nextInt();

        String userGuess = board[x][y];
        return userGuess;
    }



    public void checkResult(String userGuess) {                                     // checks the user's guess

            if(userGuess.equalsIgnoreCase(frigate[0])){
                System.out.println("hit!");
                numOfHitsOnFrigate++;
                board[1][1] = " *";
                createBoard();
            }
            else if(userGuess.equalsIgnoreCase(frigate[1])) {
                System.out.println("hit!");
                numOfHitsOnFrigate++;
                board[1][2] = " *";
                createBoard();
            }
            else {
                System.out.println("miss!");
            }
            if (numOfHitsOnFrigate == 2) {
                System.out.println("Enemy frigate has been sunk!");
                frigateIsAlive = false;
            }

        } 



} // end class


public class Game {

public static void run() {
    GameBoard newGame = new GameBoard();

    newGame.createBoard();

    while(newGame.frigateIsAlive) {
    newGame.checkResult(newGame.getUserGuess());
    }






    }

}


public class App {

    public static void main(String[] args) {

        Game.run();

    }
}

最佳答案

船总是被击中,因为frigate的声明是:

  frigate = {board[1][1], board[1][2]}

,最终将字符串 '[ ]' 分配给护卫舰的两个值。当您寻找护卫舰并比较值时,会将其与更多的空字符串进行比较。

可以通过在 [1,2,3,4, n] 中制作位置 x 和在 [A,B,C...,Letter_n 中制作 y 的棋盘来解决此问题]。也就是说,护卫舰的坐标为 Frigate.x = 1Frigate.y = A

希望这会有所帮助!

<小时/>

我看到了您关于如何实现这一点的进一步问题。我将使 Frigate 成为一个具有坐标列表的类:

  1. this.x 作为一个字母或数字

  2. this.y 作为一个点 不是您示例中的 this.x 类型

  3. 元组 (this.x, this.y) 在您的列表 Frigate 中效果很好

  4. 对列表护卫舰中的任何其他点执行相同的操作。

列表护卫舰完成后,还需要更改两件事。

改变的第一件事是如何检查用户是否在您想要的范围内调用事物。

必须改变的第二件事是如何确保同一点不会被一遍又一遍地调用来“炸毁”一艘船。也就是说,当 Frigate 中的一个点被调用时,它应该从 Frigate 中删除。 Frigate 中剩余的元组将是 Frigate 上剩余的“生命值”。要记忆 Frigate 的原始大小,添加 Frigate.initialSize() 会非常方便,但这可能会在以后进行。

关于java - 战舰代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26566803/

相关文章:

java - java.util.function.Supplier 的 C++ 等价物是什么?

java - Camel onException - 具有相同异常类但不同操作的路由

javascript - 三元运算符未按预期工作

java - 如何在读取第一行后中断 POI 流式读取器?

java - 如何在java中打开和替换apache PDFBox lib中PDF流中的数据?

java - Hadoop 中失败文件的重试处理

java - 如何用Java构建金字塔

Java - 类加载导致父类的 ClassNotFoundException

java - Selenium 浏览器用户名和密码弹出窗口 - 无法处理(Java)

java - log4j2什么时候加载配置文件?