im working on a battleship java program i believe my isSunk
im working on a battleship java program, i believe my isSunk() method is faulty, every time i get a hit it atomatically says the sip is sunk,heres the code i have for that,
public boolean isSunk(){
boolean isSunk = true;
for (int i=0;i<this.getLength();i++){
isSunk = isSunk&&this.hit[i];
}
return isSunk;
there are four types of ships i can hit, and they all sink with one hit, i have battleship which is supposed to take 4 hits before saying its sunk, and the same for a cruiser thats supposed to take 3 hits before sinking, and a destroyer thats supposed to take two hits to sink, all of them are sinking as if they are submarines which occupie one square(1 hit), how do i fix this...
Solution
Actually the isSunk variable you are returning in the function is giving true always thats why you are getting sink with one hit. that variable must return false first untill the desired numberes of hit happens.after that it must return the true for getting sink for the ship.
There is no issue with the function code here. actually the issue is with the hit or you can say wih this.hit[i]
So check for the value of hit.All the best.
