Nedward
Nedward

Reputation: 17

How to change the first row and column in a 2D array

Hello I'm fairly new to java and am creating a game that involves a 2D array. I'm trying to have a line at the top and side of the grid to display 0-10 and A-J which will be coordinates of the grid. I've made a simple 2D array:

String[][] board = new String [10][10];

    for (int row = 0; row<board.length;row++) {
        for (int column = 0; column<board.length; column++) {
            board[row][column] = ".";
            System.out.print(board[row][column] + "  ");
        }
        System.out.println();
    }

I've done some trial and error but I can't seem to find a solution and there's not much on this I can find on google. Any advice will be appreciated

Upvotes: 0

Views: 694

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39152

Try something more like this:

public static void main(String[] args)
{
    String[][] board = new String [10][10];
    for (int row = 0; row<board.length;row++) {
        for (int column = 0; column<board[0].length; column++) {
            board[row][column] = ".";
        }
    }

    System.out.println();
    displayBoard(board);

    // change something in the board
    board[2][3] = "*";

    System.out.println();
    displayBoard(board);
}

private static void displayBoard(String[][] board)
{   
    String rowLabels = "ABCDEFGHIJ";
    System.out.println("   0  1  2  3  4  5  6  7  8  9");
    for (int row = 0; row<board.length;row++) {
        System.out.print(rowLabels.substring(row, row+1) + "  ");
        for (int column = 0; column<board[row].length; column++) {
            System.out.print(board[row][column] + "  ");
        }
        System.out.println();
    }
}

Output:

   0  1  2  3  4  5  6  7  8  9
A  .  .  .  .  .  .  .  .  .  .  
B  .  .  .  .  .  .  .  .  .  .  
C  .  .  .  .  .  .  .  .  .  .  
D  .  .  .  .  .  .  .  .  .  .  
E  .  .  .  .  .  .  .  .  .  .  
F  .  .  .  .  .  .  .  .  .  .  
G  .  .  .  .  .  .  .  .  .  .  
H  .  .  .  .  .  .  .  .  .  .  
I  .  .  .  .  .  .  .  .  .  .  
J  .  .  .  .  .  .  .  .  .  .  

   0  1  2  3  4  5  6  7  8  9
A  .  .  .  .  .  .  .  .  .  .  
B  .  .  .  .  .  .  .  .  .  .  
C  .  .  .  *  .  .  .  .  .  .  
D  .  .  .  .  .  .  .  .  .  .  
E  .  .  .  .  .  .  .  .  .  .  
F  .  .  .  .  .  .  .  .  .  .  
G  .  .  .  .  .  .  .  .  .  .  
H  .  .  .  .  .  .  .  .  .  .  
I  .  .  .  .  .  .  .  .  .  .  
J  .  .  .  .  .  .  .  .  .  .  

Note that the headers are NOT being stored in the array itself, as then you'd have to do extra processing to determine the "state" of what is in the array...

Upvotes: 2

FazoM
FazoM

Reputation: 4956

Check if you are on the first row and assign number depending on column, and check if you are on the first column and assign symbol depending on row. Try this inside your loops:

if(row == 0) {
  board[row][column] = column;
} else if(column == 0) {
  board[row][column] = "" + ('A' + row);
} else {
  board[row][column] = ".";
}
System.out.print(board[row][column] + "  ");

EDIT There will be problem with the corner - should it be A or 0 ? You may want to start iterating from 1 instead of 0. Additionally, as someone pointed out in comment - for 0-10 you will need 11 elemtent. Additionally if you want a corner it might grow to 12!

Upvotes: 0

Related Questions