Results 1 to 8 of 8
 09102014, 07:14 PM #1Member
 Join Date
 Sep 2014
 Posts
 16
 Rep Power
 0
need help in battleship bluej program
I am working on a project on bluej called battleship and being new to it I cant figure out how to generate a ship(3 co ordinate long) on a 10X10 multi dimentional array. nothing graphical just plain co ordinates. can you please help me out. ask me questions if you need any more info.
thanks
 09102014, 07:40 PM #2Senior Member
 Join Date
 Oct 2010
 Posts
 393
 Rep Power
 9
Re: need help in battleship bluej program
Hi Haddu, welcome to the forums.
What have you tried so far and which part exactly are you having difficulties with?
Regards.
 09102014, 07:41 PM #3
Re: need help in battleship bluej program
Without seeing the code, it's hard to be too specific.
If the ship is horizontal, set 3 locations in a row (row is constant, columns change)
if the ship is vertical, set 3 locations in a column (column is constant, rows change)If you don't understand my response, don't ignore it, ask a question.
 09112014, 06:52 PM #4Member
 Join Date
 Sep 2014
 Posts
 16
 Rep Power
 0
Re: need help in battleship bluej program
im still a stdent and this is my first time programming so the language is pretty basic. i want to generate a randomized ship in any location at a randomized location and directions around 3 co ordinate long. this program is just a snipet but its to long and doesnt really does the job. the randomized ship should be able to fit in the grid. so i have taken another 2 arrays and did the programing in it and the display will be on the grid which I have fugured out.
Java Code:void program() { int [ ] a = new int[6]; int [ ] b = new int[6]; int rows=9; int x=(int)(Math.random()*9); int y=(int)(Math.random()*9); int direc=(int)(Math.random()*4); for(int i=0;i<3;i++) { if(x+3<=rows && x3>=0 && y+3<=rows && y3>=0) { if(direc==1) {a[i]=x; b[i]=y+i; } if(direc==2) {a[i]=x; b[i]=yi; } if(direc==3) {a[i]=x+i; b[i]=y; } if(direc==4) {a[i]=xi; b[i]=y; } } if(x+3<=rows && x3>=0 && y+3<=rows) { if(direc==1) {a[i]=x; b[i]=y+i; } if(direc==3) {a[i]=x+i; b[i]=y; } if(direc==4) {a[i]=xi; b[i]=y; } } if(x+3<=rows && x3>=0 && y3>=0) { if(direc==2) {a[i]=x; b[i]=yi; } if(direc==3) {a[i]=x+i; b[i]=y; } if(direc==4) {a[i]=xi; b[i]=y+1; } } if(x+3<=rows && y+3<=rows && y3>=0) { if(direc==1) {a[i]=x; b[i]=y+i; } if(direc==2) {a[i]=x; b[i]=yi; } if(direc==3) {a[i]=x+i; b[i]=y; } if(direc==4) {a[i]=xi; b[i]=y+1; } } } } }
 09112014, 07:43 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,423
 Blog Entries
 7
 Rep Power
 27
Re: need help in battleship bluej program
You can think of it this way: the top/left coordinates of your ship has to be in the interval [0,9] x [0,7] if the ship is positioned vertical or in the interval [0,7] x [0,9] if it is positioned horizontally. The rest of the ship lies below (vertical ship) or to the right (horizontal ship) of this first coordinate. Now you only need two directions instead of four ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 09122014, 05:27 PM #6Member
 Join Date
 Sep 2014
 Posts
 16
 Rep Power
 0
 09122014, 06:43 PM #7Senior Member
 Join Date
 Oct 2010
 Posts
 393
 Rep Power
 9
Re: need help in battleship bluej program
The end of the ship closest to the top left corner can be at any coordinate within the array between [0][0] and [7][7]. The remainder of the ship either lie to the right of this point or beneath it.
Regards.Last edited by Ronin; 09122014 at 06:44 PM. Reason: Typo
 09122014, 07:01 PM #8
Re: need help in battleship bluej program
To visualize this, take a piece of paper, draw a 10x10 grid, label all the squares with their indexes: 0,0 to 9,9
The try placing the 3 unit ship at various places on the grid to see where it fits and where is does NOT fit because it runs off the grid.If you don't understand my response, don't ignore it, ask a question.
Similar Threads

GUI for BattleShip program
By xiz1815 in forum New To JavaReplies: 2Last Post: 12112012, 05:46 PM 
Battleship program
By buckbuck2 in forum New To JavaReplies: 2Last Post: 11282012, 02:57 AM 
Battleship Program
By zizou001 in forum New To JavaReplies: 6Last Post: 11152012, 08:21 AM 
Java Battleship Program
By zizou001 in forum New To JavaReplies: 6Last Post: 11152012, 08:21 AM 
Help with Battleship program! 2D arrays
By tylerkung in forum New To JavaReplies: 3Last Post: 09232011, 01:45 PM
Bookmarks