I am getting a "Dead Code" warning on my battleship game. I am testing if a ship can be placed and am seeing different direction that the ship will face if it can be placed.
I then get input using In.class and after checking if it can be placed with a boolean (set to true/false when checking directions for placement) I use a 2D array of ints and set all of the locations to 1 (Starting location then + length (given as a parameter) in direction specified)
my .java file is here ShipProperties.java
if possible please keep answers to a beginners skill level (basic i/o and arrays, im pretty good with logic)
I got it fixed and it now works as intended!
Just put the return in a if/else inside the loop
for(int i = length; i > 0; i--)
{
grid[startLocX][startLocY + i] = 1;
if(i == 0)
{
return grid;
}
}
In your code,
for(int i = length; i > 0; i--) //Dead Code
{
grid[startLocX - i][startLocY] = 1;
return grid;
}
decrement in loop is never executed because in first iteration of the loop your method return a value, so never do a second iteration. Actually your code is same as:
if(length > 0)
{
grid[startLocX - length][startLocY] = 1;
return grid;
}
Hope it helps.