0

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("|");
        }

    }
2
  • 2
    When you say you want to inject -5 from another method, would you mind showing an example of what the end goal would look like? In other words, just to clarify what you would like to have happen as an end result. Commented Sep 21, 2020 at 0:23
  • i chose a neg int and it could be any neg int. my thought process and what i think the 2d grid looks like as int is: ` 0,0,1,2,3,4,5 1,0,1,2,3,4,5 2,0,1,2,3,4,5 3,0,1,2,3,4,5 ` so using 'i' and 'k' im printing out a char based on the value. so im a looking for a way i can inject a neg number so when the map prints out it will change to the char i specify for the neg int basically making a game screen int cli where i will be able to move a char around in the map Commented Sep 21, 2020 at 0:59

2 Answers 2

2

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);
}

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

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

|---|
| | |
|-|-|
| | |
|---|
Sign up to request clarification or add additional context in comments.

1 Comment

Very cool. i like this, i can see this being very useful with some other ideas i have for this learning project im doing. Thank you!
0

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("|");
        }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.