Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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:

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].addActionListener(new myEvent());
enemyButtons[i][j].setBackground(Color.yellow);

grid2.add(enemyButtons[i][j]);
}
}

any help would be amazing, thank you!!!
[974 byte] By [zoidberg] at [2007-11-11 7:26:42]
# 1 Re: Adding objects to an array
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.

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.

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.
Phaelax at 2007-11-11 22:38:24 >