DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2005

    Adding objects to an array


    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].addActionListener(new myEvent());
    any help would be amazing, thank you!!!

  2. #2
    Join Date
    Mar 2004
    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.
    Last edited by Phaelax; 12-07-2005 at 07:26 AM.

Similar Threads

  1. Adding objects to a vector
    By cirrus in forum Java
    Replies: 2
    Last Post: 06-19-2005, 10:11 AM
  2. Saving Objects in an Array...?
    By dotnetforever in forum .NET
    Replies: 2
    Last Post: 02-15-2005, 07:16 PM
  3. JTable from array of Objects
    By styles in forum Java
    Replies: 2
    Last Post: 04-17-2001, 04:03 AM
  4. filling array objects
    By Jeanie in forum Java
    Replies: 1
    Last Post: 03-08-2001, 10:50 AM
  5. newbie - Array of objects
    By Mary in forum Java
    Replies: 1
    Last Post: 02-16-2001, 01:13 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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.