# Help with Battleship Game

Show 40 post(s) from this thread on one page
Page 3 of 3 First 123
• 04-09-2012, 07:42 PM
Norm
Re: Help with Battleship Game
An idea: have an array whose contents would be references to the Ship occupying a square. When there is a hit at a row,col, the row,col in this new array would have a reference to the Ship object.
• 04-09-2012, 09:55 PM
Lucid15
Re: Help with Battleship Game
Not quite sure i am following this.. would this new array be apart of the Ship object? So the ship class would look like
Code:

```private int size;     String name;     private int [][] reference = new int [10][10];         public Ship(int s, String string, int[][]r){         size = s;         name = string;         reference = r;     }```
Just not sure how to track if all of the points on a ship have been hit or not, determining its orientation etc..
• 04-09-2012, 10:00 PM
Norm
Re: Help with Battleship Game
Ship[][] shipLocations = new Ship[#rows][#cols];
then in the placeShip() method, save a reference to this for all the ship's x,y locations:
shipLocations[x][y] = this;
• 04-09-2012, 10:25 PM
Lucid15
Re: Help with Battleship Game
here is what i did, is it correct?
Code:

```private Battleship game;         public void placeShip(int [][]a)     {       Random r = new Random();       int orientation = r.nextInt(2);       if(orientation == 0)//Vertical Ship       {           int x = r.nextInt(10);           int y = r.nextInt(10-size);           for(int z=0;z<size;z++)           {                             a[y+z][x] = 1;             game.shipLocations[y+z][x] = this;           }                    }       else//Horizontal Ship       {           int x = r.nextInt(10-size);           int y = r.nextInt(10);           for(int z=0; z < size; z++)           {             a[y][x+z] = 1;             game.shipLocations[y][x+z] = this;           }       }     }```
Is this right? and what do i do then with the stored shipLocations, i am not quite sure exactly what this did, and the meaning of "this"?
• 04-09-2012, 10:28 PM
Norm
Re: Help with Battleship Game
this is a reference to the current object where the code is executing.

If there is a hit at location: x,y shipLocations[x][y] gives a reference to the Ship object at that location.
• 04-09-2012, 10:41 PM
Lucid15
Re: Help with Battleship Game
so this is storing hit values in their respective ships in which they hit? So if it is a hit it goes back and detects which ship the hit affected? then how do i reference back to check on which ship has been affected and how many times?
• 04-09-2012, 11:02 PM
Norm
Re: Help with Battleship Game
When there is a hit you find the Ship by going into the shipLocations array.
• 04-09-2012, 11:12 PM
Lucid15
Re: Help with Battleship Game
so each ship has a ship locations array? or is there a single array? How do i put this all together, for some reason i am still not getting it? aircraftcarrier.shipLocations, etc? I dont get the whole concept of doing it this way and what use it is, how i would determine if a ships size has been occupied by 3's or hits, or has been a number of times equal to its size.
• 04-09-2012, 11:16 PM
Norm
Re: Help with Battleship Game
The shipLocations array was my answer to that question. If the question has gone away, then there is no need for this array.
• 04-09-2012, 11:22 PM
Lucid15
Re: Help with Battleship Game
i was asking on how to keep track of hits on each Ship, and the locations of each Ship.
• 04-09-2012, 11:27 PM
Norm
Re: Help with Battleship Game
The ship should keep track of its location.

When a ship is hit, use the shipLocations array to find the ship that was hit.
• 04-09-2012, 11:40 PM
Lucid15
Re: Help with Battleship Game
So this is basically just creating an array just like the board, for every ship and placing a 1 inside of it where the ship is on the board? Then how would i use the shipLocations array to find which ship got hit?
• 04-09-2012, 11:43 PM
Norm
Re: Help with Battleship Game
There would be ONE shipLocations array
• 04-09-2012, 11:52 PM
Lucid15
Re: Help with Battleship Game
So whats the point of it then? wouldnt there be overlap of the two fields (compfield, myfield) that are already created?
• 04-09-2012, 11:53 PM
Norm
Re: Help with Battleship Game
What do those arrays contain?
What does the shipLocations array contain?
• 04-09-2012, 11:58 PM
Lucid15
Re: Help with Battleship Game
They contain 0 and 1's where there isnt a ship, and where there is respectively. and i am really still not sure what the shipLocations array contains
• 04-09-2012, 11:59 PM
Norm
Re: Help with Battleship Game
Quote:

what the shipLocations array contains
Go back and read the previous posts. It was defined and values were assigned to it.
• 04-10-2012, 12:04 AM
Lucid15
Re: Help with Battleship Game
a reference to the ship object at each location.
• 04-11-2012, 07:35 PM
Lucid15
Re: Help with Battleship Game
by the way, this gives me a nullpointer exception. Another way i was thinking which may be easier is to have a "life" attribute to each Ship object, but it wouldn't be as convenient... still determined to figure this out
• 04-11-2012, 07:37 PM
Norm
Re: Help with Battleship Game
You should test for null BEFORE using it.
Show 40 post(s) from this thread on one page
Page 3 of 3 First 123