Poehnell
Poehnell

Reputation: 27

How to change or insert value in a 2D Array nested for loop

I'm am very new to programming and working just in the CLI. I may be trying to do something completely wrong or going about it the wrong way. I am using a nested for loop to create a 2d grid array, assigning characters to my (int i) and (int k) to create a border. now in another method am trying to inject a value to the grid so when the grid is printed I can change the char in that position of the grid. I have done this manually building the gird as you can just select grid[1][2] = a. just can't seem to wrap my head around it using the for a loop. reason for using the for a loop as I can adjust the size of the grid and display the border by just adjusting grid size.

public void printMap() {
        map = new int[15][30];

        System.out.println();
        for (int i = 0; i < map.length; i++){
                System.out.print("|");
                for (int k = 0; k < map[i].length; k++){
                    if (i == 0) {
                        System.out.print("-");
                    }else if (i == map.length-1){
                        System.out.print("_");
                    }else if (k == -5){ // i want to inject -5 from another method into the grid so it prints out '^' where ever i inject -5
                        System.out.print("^");
                    }else {
                        System.out.print(" ");

                }
            }
            System.out.println("|");
        }

    }

Upvotes: 0

Views: 83

Answers (2)

Poehnell
Poehnell

Reputation: 27

talking with a friend he gave me this solution. so now at int 'i' (2) and int 'k'(2) it will print '^'

private void displayGame(){
        printMap(2,2);

    }
    public void printMap(int playerPositionX, int playerPositionY) {
        System.out.println();
        boolean foundRow = false;
        boolean foundPosition = false;
        for (int i = 0; i < map.length; i++){
                System.out.print("|");
                if (!foundRow && playerPositionY == i){
                    foundRow = true;
                }
                for (int k = 0; k < map[i].length; k++){
                    if (foundRow && (playerPositionX == k)){
                        foundPosition = true;
                    }
                    if (i == 0) {
                        System.out.print("-");
                    }else if (i == map.length-1){
                        System.out.print("_");
                    }else if (foundPosition){
                        System.out.print("^");
                        foundPosition = false;
                        foundRow = false;
                    }else {
                        System.out.print(" ");

                }
            }
            System.out.println("|");
        }

    }

Upvotes: 0

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

I think you should split two operation on the grid: update and print. It's better to change grid definition to char[][] and print() method just prints this content (it should not care about what different characters could mean). In case you want to hold int instead, so just use if...else to print correct character.

Additionally, you should split definition of the grid and it content. Look at my simple example. There're several operations:

  • add border to the grid
  • print the grid
  • add smile image to the grid
  • add cross image to the grid

private static void addBorder(char[][] grid) {
    for (int row = 0; row < grid.length; row++) {
        for (int col = 0; col < grid[row].length; col++) {
            if (col == 0 || col == grid[row].length - 1)
                grid[row][col] = '|';
            else if (row == 0 || row == grid.length - 1)
                grid[row][col] = '-';
            else
                grid[row][col] = ' ';
        }
    }
}

private static void showSmile(char[][] grid) {
    clearGrid(grid);
    // only for 5x5 grid
    grid[1][1] = '^';
    grid[1][3] = '^';
    grid[2][2] = '|';
    grid[3][2] = '-';
}

private static void showCross(char[][] grid) {
    clearGrid(grid);
    // only for 5x5 grid
    grid[1][2] = '|';
    grid[2][2] = '|';
    grid[3][2] = '|';
    grid[2][1] = '-';
    grid[2][3] = '-';
}

private static void clearGrid(char[][] grid) {
    for (int row = 1; row < grid.length - 1; row++)
        for (int col = 1; col < grid[row].length - 1; col++)
            grid[row][col] = ' ';
}

private static void printGrid(char[][] grid) {
    for (int row = 0; row < grid.length; row++) {
        for (int col = 0; col < grid[row].length; col++)
            System.out.print(grid[row][col]);

        System.out.println();
    }
}

Output:

public static void main(String[] args) {
    char[][] grid = new char[5][5];
    addBorder(grid);
    printGrid(grid);
    System.out.println();
    
    showSmile(grid);
    printGrid(grid);
    System.out.println();
    
    showCross(grid);
    printGrid(grid);
}

|---|
|   |
|   |
|   |
|---|

|---|
|^ ^|
| | |
| - |
|---|

|---|
| | |
|-|-|
| | |
|---|

Upvotes: 2

Related Questions