1
votes

Alright, so I'm trying to make a very rudimentary version of battleship in order to test out my one week's worth of programming knowledge. Basically, all the program does is have the user enter two numbers and those numbers are then used to locate a slot on a multidimensional boolean array. If the array's slot is true, it should say hit. If it is false, it should say miss. However, for some reason, no matter what I enter, I keep getting "Hit! Way to go!" Fortunately, the program is displaying an error for the "else" command. It says, "The following branch is never used." Unfortunately, I don't know why the branch is never used and I don't know how to fix it. Can I have some pointers, help, and/or advice? Thanks!

import java.util.Scanner;

class Battleship {        
    public static void main(String[] arguments) {
       System.out.println("                                  Welcome to Battleship Solo Edition!");
       System.out.println("                        There are seven boats scattered across this 10 by 10 grid.");
       System.out.println("                        When you get a \'Hit!\', mark it on your accompanying paper.");
       System.out.println("                 Once you have completely shot down all seven boats (18 Hits), you win!");
       System.out.println("                              But play carefully! You only have 50 missiles.");
        System.out.println();                                                                 
       boolean[][] field = new boolean [10][10];
       //Ship one
               field[8][2] = true;
               field[8][3] = true;
               field[8][4] = true;
               field[8][5] = true;
               field[8][6] = true;
       //Ship two
               field[1][9] = true;
               field[2][9] = true;
       //Ship three
               field[2][4] = true;
       //Ship four
               field[2][2] = true;
               field[3][2] = true;
               field[4][2] = true;
       //Ship five
               field[4][8] = true;
               field[5][8] = true;
               field[6][8] = true;
               field[7][8] = true;
        //Ship six
               field[6][3] = true;
               field[6][4] = true;
       //Ship seven        
               field[6][6] = true;

for (int missileattempts=0; missileattempts<=50; missileattempts++){
    System.out.println("What is the X-Coordinate of your guess?");
    Scanner scan = new Scanner(System.in);
    int AnswerX = scan.nextInt();
    System.out.println("What is the Y-Coordinate of your guess?");
    Scanner scan2 = new Scanner(System.in);
    int AnswerY = scan.nextInt();
    if (field[AnswerX][AnswerY]=true){
        System.out.println("Hit! Way to go!");}
    else {System.out.println("Miss! Try again!");}
    }
}}
2
Since I'm still new to Java (and all of programming for that matter). I'd greatly appreciate it, if you could dumb down your responses. Thanks!e4a

2 Answers

5
votes
a = b // assignment

a == b // comparison

You need to use the correct operator in your if condition.

Assignment results in the value from the right-hand side of the operator. Because you are assigning the value true to field[x][y], your condition will always evaluate to true.

3
votes

In the if condition, you are assigning the value true to the variable, and then that gets checked. (It's always true at that point because that's what you assigned.

Two things here: First, the Boolean test operator is == rather than =. Second, if the variable is Boolean anyway, then you should just check it rather than doing an equality check at all, like this: if (field[AnswerX][AnswerY]) {. Doing equality checks on Booleans is bad style - in part because it lends itself to exactly the error that you have here.