KevinRandall
KevinRandall

Reputation: 51

Strange (to me) looping error

I just got some help here for index out of bounds and you guys were wonderful. I added to account for the direction change in my battleship game by adding the following lines of code

 if(direction == 1){    

and }else{

in the following code. It went from working perfectly to looping constantly though can you guys give me an idea as to why this is happening?

 package outlab6;

 import java.util.Scanner;

 public class Battleship {
private int rows;
private int cols;
private Spot spot[][];
Scanner input = new Scanner(System.in);

public  Battleship(int rows, int cols){
    this.rows = rows;
    this.cols = cols;
}
public void setBoard(){
    spot = new Spot[rows][cols];
    for(int i = 0; i < rows; i++){
        for( int j = 0; j < cols; j++){
            spot[i][j] = new Spot();
        }
    }
    //setup board to be completely empty
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < cols; j++){
            spot[i][j].setShip(0);
        }
    }
 //     //test code
 //     for(int i = 0; i < rows; i++){
 //         for(int j = 0; j < cols; j++){
 //             System.out.print(spot[i][j].getShip());
 //         }
 //         System.out.println();
 //     }
        setShips();
}
public void printBoard(boolean active){


}
public boolean over() {

    return false;
}
public void makeGuess() {
    input.nextInt();

}
public void printStatistics() {


}
public void setShips(){
    //this method creates and places the ships
    //start with carrier and move on down
    for(int i = 5; i > 1; i--){
        int col;
        int row;
        boolean valid = false;
        //set a direction
        int direction = (int)(Math.random()*2)+1;
        //System.out.println(direction);
        //get a valid spot
        while(!valid){
        //generate a location
        int chosenRow = (int)(Math.random()* rows);
        int chosenCol = (int)(Math.random()* cols);
        System.out.println("Row:" + chosenRow);
        System.out.println("Col:" + chosenCol);
        //check to see if spot is open
        if(direction == 1){ 
            //for horizontal ships
            if(chosenCol + i < cols){
            for(int j = 0; j < i; j++){
                if(spot[chosenRow][chosenCol + i].getShip() == 0){
                    valid = true;
                }else{
                    valid = false;
                }
            }
        }else{
         //go through again
        }
        }else{

        }
    }
}
 }
 }

Upvotes: 0

Views: 47

Answers (2)

Friso van Dijk
Friso van Dijk

Reputation: 669

If you remove the *2 from direction you'll be fine. Right now it's set to 2 (try pasting System.out.println("Dir:" + direction);

If you remove the *2, you get the following output (when printing direction):

Row:2
Col:3
Dir:1
etc...

Also 1 more tip: In your setBoard() you do the 2 loops: create spots and make them empty. Why not either create them empty in the constructor or make them empty in the same loop?

public void setBoard(){
spot = new Spot[rows][cols];
for(int i = 0; i < rows; i++){
    for( int j = 0; j < cols; j++){
      spot[i][j] = new Spot();
      spot[i][j].setShip(0);
    }
  }
}

Upvotes: 1

markus
markus

Reputation: 1651

Think about when your while loop exits? It will exit as soon as valid will become true. And now think about when it will become true. This will only happen if your direction equals 1. And then look at where your direction is set and what possible values direction might take. And voila there is your infinte loop.

Upvotes: 1

Related Questions