1

Every few runs of my program it gives me the error

     at outlab6.Battleship.setShips(Battleship.java:75)
     at outlab6.Battleship.setBoard(Battleship.java:35)

But I thought that I was already setting the code up so that those shouldn't happen. Can someone tell me what I did wrong and why it's not ignoring the possibilities that make that possible?

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)+1;
        int chosenCol = (int)(Math.random()* cols)+1;
        System.out.println("Row:" + chosenRow);
        System.out.println("Col:" + chosenCol);
        //check to see if spot is open

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

      }
    }
  }
}
2
  • I would have a look at these two line int chosenRow = (int)(Math.random()* rows)+1 and int chosenCol = (int)(Math.random()* cols)+1 I haven't run your code but that +1 might be screwing you up there.. Commented Nov 30, 2014 at 19:58
  • 1
    (int)(Math.random()* rows)+1 - imagine rows is 1. then every time you would get 1, arrays start at 0 of course so you would always get and index out of bounds. It should read (int)(Math.random()* rows). Commented Nov 30, 2014 at 19:58

1 Answer 1

1

Your problem is probably here :

    int chosenRow = (int)(Math.random()* rows)+1;
    int chosenCol = (int)(Math.random()* cols)+1;

This will give you a chosenRow between 1 and rows, and a chosenCol between 1 and cols. Having chosenRow == rows will bring you out of the array bounds when you try to access spot[chosenRow][chosenCol + i].

You should change it to :

    int chosenRow = (int)(Math.random()* rows);
    int chosenCol = (int)(Math.random()* cols);
Sign up to request clarification or add additional context in comments.

2 Comments

That's it thank you. I completely forgot for some reason that arrays start at 0. Thank you very very much!
@user3303616 remember that the 0th index is also a row and a column. You do not need to account for Math.random() coming out to 0.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.