DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Dec 2005
Posts
4

## Adding objects to an array

hello!

I have created a 10x10 array of buttons in a grid formation, however i wondered if anyone can help me to place objects of a set length randomly into the array??

I know this will involve math.RANDOM, but it is items that have 4 different set lengths and i dont know how to keep these objects of one set length together and then store them into the array??

here's my code for the array:

Code:
```	JButton[][] enemyButtons = new JButton[10][10];
for (int i = 0; i < enemyButtons.length; i++)
{
for (int j = 0; j < enemyButtons[i].length; j++)
{

enemyButtons[i][j] = new JButton();
enemyButtons[i][j].setPreferredSize(gridButtonSize);
enemyButtons[i][j].setBackground(Color.yellow);

}
}```
any help would be amazing, thank you!!!

2. Senior Member
Join Date
Mar 2004
Posts
635
That array is holding JButton objects, so it has nothing to do with storing another object in a random spot. I'm assuming you want to randomly place your battleships within the grid map area? (picked it up from your other thread)

1. Grab a random coordinate in the map: X = 0 through 9, Y = 0 through 9
2. Determine the length of the ship (how many grid spaces it takes) and if its horizontal or vertical
3. Make sure that the needed grid spaces are free (not already occupied) Also, check the boundaries of the map. If you have a ship 3 spaces long going horizontally that starts at position 8 in the grid, 8, 9, 10. Well you don't have a 10th spot only 0-9. So you'd have to move the ship over to the left 1 spot to make it fit, but make sure the 7th (and 8 and 9) are clear. Otherwise pick a new random spot to start from.

I'd probably do something like the following:
Make a ship class like this:
The number could represent both an ID for a ship type and the number of spaces it requires.
Code:
```public class Ship
{
public static final int SUBMARINE = 1;
public static final int CRUISER = 2;
public static final int DESTROYER = 3;
public static final int BATTLESHIP = 4;
public static final int CARRIER = 5;

public int damage = 0;
public int shipType = -1;

public Ship(int type)
{
shipType = type;
}

public int shipHit()
{
return ++damage;
}

public int getType()
{
return shipType;
}
}```
Then in your main game class, your map array would initially be filled with "0" for each space. Then place your ships, for example:
map[4][7] = Ship.CRUISER
map[5][7] = Ship.CRUISER

In this case, since I have 5 ships number 1-5, I'll make an array with 5 elements to hold each ship where the first element is the SUBMARINE (1) an so on.

When a space is clicked, determine the coordinates first then see if a ship is present.
Code:
```if (map[x][y] > 0)
{
if (map[x][y] == Ship.CRUISER)
{
//minus 1 cause array starts at 0
if (ships[Ship.CRUISER-1].shipHit() == Ship.CRUISER)
//cruiser now has a total of 2 hits, so its sunk
}
}```
Obviously this method wouldn't work if we had multiple CRUISERS. This was just one, simple idea. But I hope it helped you out some.
Last edited by Phaelax; 12-07-2005 at 07:26 AM.

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise