1. 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.

2. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

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
Java 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..

3. 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;

4. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

Re: Help with Battleship Game

here is what i did, is it correct?
Java 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"?

5. 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.

6. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

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?

7. Re: Help with Battleship Game

When there is a hit you find the Ship by going into the shipLocations array.

8. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

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.

9. 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.

10. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

Re: Help with Battleship Game

i was asking on how to keep track of hits on each Ship, and the locations of each Ship.

11. 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.

12. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

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?

13. Re: Help with Battleship Game

There would be ONE shipLocations array

14. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

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?

15. Re: Help with Battleship Game

What do those arrays contain?
What does the shipLocations array contain?

16. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

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

17. Re: Help with Battleship Game

what the shipLocations array contains
Go back and read the previous posts. It was defined and values were assigned to it.

18. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

Re: Help with Battleship Game

a reference to the ship object at each location.

19. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

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

20. Re: Help with Battleship Game

You should test for null BEFORE using it.

Page 3 of 3 First 123

Posting Permissions

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