Results 1 to 2 of 2
  1. #1
    goodvibes is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Converting int(x, y) to Array

    Hi! I am working on a Battleship program for my class and have run into some issues. Right now I am trying to figure out how to convert the users mouse click (mouseX, mouseY) to an array's indices, grid[row][col]. Any advice? I have posted the method below and the grid that it refers to.

    Java Code:
    private void attackGrid(int mouseX, mouseY)
    {
    }
    
    private void initializeGrid()
    {
     grid = new int[NUM_ROWS][NUM_COLS];
            
            for (int row = 0; row < NUM_ROWS; row++)
                for (int col = 0; col < NUM_COLS; col++)
                    grid[row][col] = EMPTY;
    }
    Additionally, I have access to these constants:
    -PANEL_WIDTH, PANEL_HEIGHT: width and height in pixels of the board (these are both 601)
    -grid.NUM_ROWS, grid.NUM_COLS: number of rows and columns in the grid (these are both 10)

    Thanks in advance for any help!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Converting int(x, y) to Array

    Think about how you would do this without a computer; given the [x,y] what steps would you take to find the [column,row] from that?

    Once you have that sorted out, it's trivial to convert it to Java code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. converting array to arraylist
    By syle_q in forum New To Java
    Replies: 4
    Last Post: 04-19-2011, 08:07 PM
  2. Converting value of array to log
    By monika in forum New To Java
    Replies: 4
    Last Post: 05-14-2010, 09:10 AM
  3. Need help converting int to a 4 byte array
    By kook04 in forum Advanced Java
    Replies: 5
    Last Post: 02-26-2010, 09:59 PM
  4. Converting ArrayList to Array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 11:41 AM

Tags for this Thread

Posting Permissions

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