1

I am creating a game of Battleships using Java and I am having trouble generating 2 random numbers which randomly choose a place on the battleship board. For example, the computer has to randomly choose a space on the board to place the ships (and later to shoot on).

I have created a 2D array:

int rows = 10;
int cols = 10;
char [][] grid;
grid = new char[rows][cols];

And then tried several different ways to get the two random numbers in the array but I cannot get it to work. Here is an example of what I have tried:

int randomPos = (char) (Math.random() * (grid[rows][cols] + 1));

Please ask me some questions if this doesn't make sense.

Sean

3
  • Is this for AI guessing locations to drop bombs? If so, you don't want duplicate locations, so it may be best to put all possible locations in a list then java.util.Collections.shuffle(List) them. Commented Jul 29, 2011 at 14:19
  • @Steve Taylor Yes, but I assumed I would have to do the same thing for choosing random positions for the computer to put their ships. What will that do? Commented Jul 29, 2011 at 14:43
  • Shuffling just rearranges the items in a list in a random order. Placing ships on the board is an entirely different matter. In that case, the easiest approach is brute force - just keep trying random locations and directions until a ship fits, repeating the same for all ships to be placed. Commented Jul 30, 2011 at 3:26

4 Answers 4

1

Math.random() generates a decimal number. It doesn't make any sense to have square 3.5779789689689... (or whatever you're referring to), so use the Math.floor() method, which rounds the number inside to the nearest integer. And as @Sanjay explained, generate the numbers separately...

int row = (int) Math.floor(Math.random() * rows);
int col = (int) Math.floor(Math.random() * cols);

Now you have actual integers that you can work with.

Sign up to request clarification or add additional context in comments.

3 Comments

Is this instead of the 2D array? If not, how would I get the random numbers in the 2D array?
Yes, but if you need the array for something else don't delete it.
@Sean Just click the little checkmark under the up/down arrows in the upper left hand corner of my answer.
0

Generate two random numbers separately and use them to index your board. To elaborate:

x = random(max_rows)
y = random(max_cols)
(x, y) -> random location (only if it's not already marked)

In your case, the range of the random number should be between 0 and 9 (inclusive both).

Comments

0
int randomRow = Math.random() * grid.length;
int randomColumn = Math.random() * grid[0].length;

I wouldn't use char as my grid type, but the code above works all the same regardless of the types of in the array.

1 Comment

I had to use char so I could put a dash in set out the board. Thanks for your answer.
0

Try this declare this is an ivar:

Random random = new Random();

//In the method do this:

int randomPos = generator.nextInt(11); //This generates a number between 0 and 10.

Comments

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.