I need to replace values from an array (Java)

0

I'm doing a maze and I need to replace the values ("") with ("or") to be able to create the walls of the levels, but I do not know how to do it.

The standard level is as follows, it starts at ("*") and ends at ("F")

String[][] matriz = {
        {"|", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "|"},
        {"|", "*", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "|"},
        {"|", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "F", "|"},
        {"|", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "|"}
    };

I tried to do it in the following way but it did not help me

for (String[] matriz1 : matriz) {
        for (int j = 0; j < matriz[0].length; j++) {
            if (matriz1.equals("|")) {
                System.out.println("o");
                System.out.print(matriz1[j] + "  ");
            }
        }
        System.out.println();
    }
    for (String[] matriz1 : matriz) {
        for (int j = 0; j < matriz[0].length; j++) {
            System.out.print(matriz1[j] + "  ");
        }
        System.out.println();
    }

It generates a space with nothing and the empty matrix.

I am also thinking about a random generation algorithm, but for the moment if you could help me with that I would be very grateful.

    
asked by Gabriel Bastias 07.06.2018 в 16:37
source

1 answer

1

The first for that you have in your question is really unnecessary, in fact it does not do anything since you are making an incorrect comparison, comparing an array of Strings with the character "|", they will never be the same:

  if (matriz1.equals("|")) {

To do what you want this is an option, detecting spaces and replacing with "o", otherwise you add the value defined in the matrix:

for (String[] matriz1 : matriz) {
    for (int j = 0; j < matriz[0].length; j++) {
        if(matriz1[j].equals(" ")){ //Reemplaza
          System.out.print(matriz1[j].replace(" ","o") + " ");           
        }else{
            System.out.print(matriz1[j] + " "); //Agrega valor y espacio.
        }
    }
    System.out.println();
}

to get:

    
answered by 07.06.2018 / 18:12
source