# Thread: Need to find a logical way to randomly create ships to my Battleship-game

1. Member
Join Date
Feb 2013
Posts
4
Rep Power
0

## Need to find a logical way to randomly create ships to my Battleship-game

Hi.
I'm in need of a logical way to create ships to my Battleship-game. I'm using a 10*10 table and I need the ships to spawn randomly to the table without overlapping each other.
1 ship that takes 5 spaces.
1 ship that takes 4 spaces.
2 ships that takes 3 spaces.
And 1 ship that takes 2 spaces.

Now, I've already managed to do all that, but I would still need a way to make the ships not to spawn next to each other. So that the ships aren't on adjacent spaces. It's ok if they touch each other diagonally.

Here is the code that creates the ships randomly. If you need more information, just ask. Hope someone figures out a way.

Java Code:
```    public static void createComputerShips(String[][] computerBoard) {
Random numbers = new Random();
int size = 5;

here:
for (int j = 0; j < 5;) {
int spawnCoordinateY = numbers.nextInt(10);
int spawnCoordinateX = numbers.nextInt(10);
int direction = numbers.nextInt(5);

if (direction == 1 && spawnCoordinateY > size - 2) {

for (int m = 0; m < size; m++) {
String coordinateYX = computerBoard[spawnCoordinateY - m][spawnCoordinateX];

if (coordinateYX.equals("S")) {
continue here;
}
}

for (int i = 0; i < size; i++) {
computerBoard[spawnCoordinateY][spawnCoordinateX] = "S";
spawnCoordinateY--;
}

if (j == 2) {
j++;
continue here;
}

j++;
size--;
}

if (direction == 2 && spawnCoordinateX < size + 1) {

for (int m = 0; m < size; m++) {
String coordinateYX = computerBoard[spawnCoordinateY][spawnCoordinateX + m];

if (coordinateYX.equals("S")) {
continue here;
}
}

for (int i = 0; i < size; i++) {
computerBoard[spawnCoordinateY][spawnCoordinateX] = "S";
spawnCoordinateX++;
}

if (j == 2) {
j++;
continue here;
}

j++;
size--;
}

if (direction == 3 && spawnCoordinateY < size + 1) {

for (int m = 0; m < size; m++) {
String coordinateYX = computerBoard[spawnCoordinateY + m][spawnCoordinateX];

if (coordinateYX.equals("S")) {
continue here;
}
}

for (int i = 0; i < size; i++) {
computerBoard[spawnCoordinateY][spawnCoordinateX] = "S";
spawnCoordinateY++;
}

if (j == 2) {
j++;
continue here;
}

j++;
size--;
}

if (direction == 4 && spawnCoordinateX > size - 2) {

for (int m = 0; m < size; m++) {
String coordinateYX = computerBoard[spawnCoordinateY][spawnCoordinateX - m];

if (coordinateYX.equals("S")) {
continue here;
}
}

for (int i = 0; i < size; i++) {
computerBoard[spawnCoordinateY][spawnCoordinateX] = "S";
spawnCoordinateX--;
}

if (j == 2) {
j++;
continue here;
}

j++;
size--;
}

else {
continue here;
}
}
}```

2. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Need to find a logical way to randomly create ships to my Battleship-game

Write a method that takes the proposed location of the ship and returns a boolean.
This method will check if the location is valid, based on the current map.

Inside this method simply check each square the ship would occupy and validate them.
I would write a validate method that takes a single set of coordinates and sees if there is a ship in the square itself, or in one of the 4 neighbouring squares.

3. Member
Join Date
Feb 2013
Posts
4
Rep Power
0

## Re: Need to find a logical way to randomly create ships to my Battleship-game

I have added this one part here, that checks the surrounding squares. Ofc when i try to run the game, it says ArrayIndexOutOfBoundsException. And i know why and where the problem is.

Im wondering if there is a way to, in the event of such error, disregard it and continue the program?

Java Code:
```    public static void createComputerShips(String[][] computerBoard) {
Random numbers = new Random();
int size = 5;

here:
for (int j = 0; j < 5;) {
int spawnCoordinateY = numbers.nextInt(10);
int spawnCoordinateX = numbers.nextInt(10);
int direction = numbers.nextInt(5);

if (direction == 1 && spawnCoordinateY > size - 2) {

for (int m = 0; m < size; m++) {
String coordinateYX = computerBoard[spawnCoordinateY - m][spawnCoordinateX];

if (coordinateYX.equals("S")) {
continue here;
}

else if (computerBoard[spawnCoordinateY - m - 1][spawnCoordinateX].equals("S")          //Here i check if the surrounding squares have the "S" already.
|| computerBoard[spawnCoordinateY - m + 1][spawnCoordinateX].equals("S")        //Ofc when the spawnCoordinateY or spawnCoordinateX is over 9 or below 0,
|| computerBoard[spawnCoordinateY - m][spawnCoordinateX - 1].equals("S")        // the ArrayIndexOutOfBoundsException occurs
|| computerBoard[spawnCoordinateY - m][spawnCoordinateX + 1].equals("S")) {    //How to fix it?
continue here;
}
}

for (int i = 0; i < size; i++) {
computerBoard[spawnCoordinateY][spawnCoordinateX] = "S";
spawnCoordinateY--;
}

if (j == 2) {
j++;
continue here;
}

j++;
size--;
}```

4. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Need to find a logical way to randomly create ships to my Battleship-game

There's a reason I suggested sticking these thigns into methods.
Nice small methods are easy to test.

Besides, for your immediate problem, you could simply not check if the index < 0 or >= arrayLength.

5. Member
Join Date
Feb 2013
Posts
4
Rep Power
0

## Re: Need to find a logical way to randomly create ships to my Battleship-game

Yeah, probably small methods would be easier to test and so on, but honestly I'm not that quick at writing code yet. This is my first real attempt at doing something as big as this (i know, its small ;)). And if I had limitless time to do that, i probably would. But I'm running out of time. This is a project I'm doing for school and I'm supposed to present this to my teacher tomorrow. I've got all the rest of the code done, but just need to figure out this one last thing.

But, anyways thanks for the tips, I'll see if I can come up with something.

6. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Need to find a logical way to randomly create ships to my Battleship-game

That's all the more reason to keep each part small.
The smaller the method the less you have to keep in your head as to what the code you are looking at needs to do.

Anyway, you can surround that bit of code with a try/catch block (it would be your if/else statement), but you do run the risk of continuing to run when there is a real error in the code.

7. Member
Join Date
Feb 2013
Posts
4
Rep Power
0

## Re: Need to find a logical way to randomly create ships to my Battleship-game

Originally Posted by Tolls
That's all the more reason to keep each part small.
The smaller the method the less you have to keep in your head as to what the code you are looking at needs to do.

Anyway, you can surround that bit of code with a try/catch block (it would be your if/else statement), but you do run the risk of continuing to run when there is a real error in the code.
Actually i did try the try/catch block thing. It seems to be working. Although like 1 out of 10 times there are some ships next to each other. Don't know why.
Unless... does the try/catch block interrupt the current if/if else if it catches an exception? And continue after the catch block?
Or does it continue from where it catches the exception?

8. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Need to find a logical way to randomly create ships to my Battleship-game

Since it'll be one of the bits of the 'else-if' throwing the exception then yes, it will skip anything it hasn't checked yet and go straight to the catch block.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•