这是我尝试使用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成为具有坐标列表的类:


this.x作为字母或数字的一点
this.y作为一点不像您的示例那样键入this.x
元组(this.x, this.y)在列表中会很好
对护卫舰列表中的任何其他点执行相同的操作。


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

已更改的第一件事是如何检查用户是否在所需范围内调用事物。

必须更改的第二件事是如何确保不会一遍又一遍地调用同一点来“炸毁”一艘船。也就是说,当在护卫舰中的某个点被调用时,应将其从护卫舰中移除。护卫舰中剩余的元组将成为护卫舰上留下的“生命值”的“健康”。回想护卫舰的原始大小,添加Frigate.initialSize()会非常方便,但这可能会在以后出现。

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

10-12 01:52