有 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();

    }
}

共 (1) 个答案

  1. # 1 楼答案

    因为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

    我希望这有帮助


    我看到了你关于如何实施这一点的进一步问题。我会让护卫舰成为一个有坐标列表的等级:

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

    2. this.y作为一个非此类型的点。x如您的示例所示

    3. 元组(this.x, this.y)在你的列表护卫舰上会很好用

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

    在护卫舰名单完成后,还有两件事需要改变

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

    第二件必须改变的事情是如何确保同一点不会被反复调用来“炸毁”一艘船。也就是说,当护卫舰上的一个点被呼叫时,它应该从护卫舰上移除。护卫舰上剩余的元组将是护卫舰上剩余的生命值。为了回忆护卫舰的原始尺寸,增加Frigate.initialSize()是非常方便的,但这可能是以后的事情