简体   繁体   中英

Trouble with creating Battleship using Java

The following block of code is supposed to check if the coordinates that the user entered are the coordinates of the ship. The ship is located on a two dimensional array at (1,1) and (1,2).

The problem started when I surrounded the getUserGuess method implementation with a while loop. The loop checks if the ship is still alive and will keep asking for the user to enter coordinates until the ship is sunk. However, as soon as the user enters either pair of the correct coordinates, the entire ship is sunk.

I have no idea why this keeps happening. As soon as I comment out the loop, the problem stops, but the loop is necessary.

Here is the method:

public void checkResult(String userGuess) {
    while (frigateIsAlive == true) {
        if (userGuess.equalsIgnoreCase(board[1][1])){
            System.out.println("hit!");
            numOfHitsOnFrigate++;
            board[1][1] = " *";
            createBoard();
        }
        if (userGuess.equalsIgnoreCase(board[1][2])) {
            System.out.println("hit!");
            numOfHitsOnFrigate++;
            board[1][2] = " *";
            createBoard();
        }
        else if (numOfHitsOnFrigate == 2) {
            System.out.println("Enemy frigate has been sunk!");
            frigateIsAlive = false;
            break;
        }
        else {
            System.out.println("miss!");
            // try again
        }
    }
} 

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;
}

Let me know if you need to see any other part of the code in order to better assist me.

This method is flawed :

  • If userGuess matches board[1][1], the loop will make you increment numOfHitsOnFrigate twice, and then you'll change frigateIsAlive to false and exit.

  • If userGuess matches board[1][2], the loop will make you increment numOfHitsOnFrigate infinite times and you'll never exit.

  • If userGuess doesn't match, the loop will never terminate, and keep printing miss! without getting new input.

You need to remove the loop, since this method checks a single userGuess , and change the conditions :

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

Based on what you wrote - I surrounded the getUserGuess method implementation with a while loop. - you have another loop which keeps getting input from the user. That other loop, whose code you haven't shown us, is necessary, since without it the game won't progress.

What you probably want (pseudo-code):

  • start (of a loop)
  • ask user for a guess
  • check result for the guess
  • sunk => stop / not sunk => continue to start

(ie you misplaced your while loop)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM