Results 1 to 8 of 8
 02202013, 02:45 AM #1Member
 Join Date
 Feb 2013
 Posts
 4
 Rep Power
 0
Need to find a logical way to randomly create ships to my Battleshipgame
Hi.
I'm in need of a logical way to create ships to my Battleshipgame. 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; } } }
 02202013, 11:04 AM #2Moderator
 Join Date
 Apr 2009
 Posts
 13,319
 Rep Power
 23
Re: Need to find a logical way to randomly create ships to my Battleshipgame
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.Please do not ask for code as refusal often offends.
** This space for rent **
 02202013, 04:24 PM #3Member
 Join Date
 Feb 2013
 Posts
 4
 Rep Power
 0
Re: Need to find a logical way to randomly create ships to my Battleshipgame
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; }
 02202013, 04:30 PM #4Moderator
 Join Date
 Apr 2009
 Posts
 13,319
 Rep Power
 23
Re: Need to find a logical way to randomly create ships to my Battleshipgame
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.Please do not ask for code as refusal often offends.
** This space for rent **
 02202013, 04:55 PM #5Member
 Join Date
 Feb 2013
 Posts
 4
 Rep Power
 0
Re: Need to find a logical way to randomly create ships to my Battleshipgame
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.
 02202013, 05:16 PM #6Moderator
 Join Date
 Apr 2009
 Posts
 13,319
 Rep Power
 23
Re: Need to find a logical way to randomly create ships to my Battleshipgame
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.Please do not ask for code as refusal often offends.
** This space for rent **
 02202013, 06:01 PM #7Member
 Join Date
 Feb 2013
 Posts
 4
 Rep Power
 0
Re: Need to find a logical way to randomly create ships to my Battleshipgame
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?
 02202013, 06:35 PM #8Moderator
 Join Date
 Apr 2009
 Posts
 13,319
 Rep Power
 23
Re: Need to find a logical way to randomly create ships to my Battleshipgame
Since it'll be one of the bits of the 'elseif' throwing the exception then yes, it will skip anything it hasn't checked yet and go straight to the catch block.
Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads

Logical error on a program using robots to create a board game
By Antonio16 in forum New To JavaReplies: 1Last Post: 01132013, 05:55 AM 
Help with battleship game, please!
By goodvibes in forum New To JavaReplies: 5Last Post: 07252012, 11:04 AM 
Help with Battleship Game
By Lucid15 in forum New To JavaReplies: 59Last Post: 04112012, 07:37 PM 
Battleship Game  really need help
By thrashsynergy in forum Java AppletsReplies: 5Last Post: 04252011, 07:21 PM 
Battleship game
By kathyla18 in forum New To JavaReplies: 2Last Post: 02262009, 10:42 PM
Bookmarks