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.
Additionally, I have access to these constants:
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;
-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!
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.