This discussion is archived
1 Reply Latest reply: May 21, 2013 1:24 PM by morgalr RSS

Prevent Random Number Collision Intersection.

1001557 Newbie
Currently Being Moderated
I am currently trying to write a Battleship program for a college course. I have run into an issue with the battleships intersecting. I was wondering how you would go about preventing them from intersecting. I am using the following code.


B stands for battleship.
          
public static void createShip(String[][] board,int size)
{
if(Math.random() < 0.5)
{
int col = (int)(Math.random()*11);
int row = (int)(Math.random()*15);
for(int s = 0; s<size; s++)
{        
                              board[row][col+s] = "B";
}
}
else
{
int col = (int)(Math.random()*15);
int row = (int)(Math.random()*11);
for(int s = 0; s<size; s++)
{
board[row+s][col] = "B";
}
}
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points