0

This is the movement of the random robot, if it hits the wall (*), the line or colune should return to the previous value, right? Since I'm incrementing and decrementing if it happens, basically it checks if it's a wall or not.

It keeps doing the loop till the "S", turns into a "R". R is the robot S is the exit.

        int linha = 1, coluna = 1;
        while (labirinto[7][7] != "R") {
            int aleatorio = randomGenerator.nextInt(4);
            System.out.println(aleatorio);
            switch(aleatorio)
            {
            case 0://cima
                if(labirinto[linha--][coluna] == "*")
                {
                    linha++;
                    break;
                }
                else labirinto[linha][coluna] = "R";
                break;
            case 1:
                if(labirinto[linha++][coluna] == "*")
                {
                    linha--;
                    break;
                }
                else labirinto[linha][coluna] = "R";
                break;
            case 2:
                if(labirinto[linha][coluna++] == "*")
                {
                    coluna--;
                    break;
                }
                else labirinto[linha][coluna] = "R";
                break;
            case 3:
                if(labirinto[linha][coluna--] == "*")
                {
                    coluna++;
                    break;
                }
                else labirinto[linha][coluna] = "R";
                break;
            }
            imprimirLabirinto(labirinto);
        }
    }

This method just prints the updated maze.

    static void imprimirLabirinto(String[][] labirinto) {
        for (int i = 0; i < labirinto.length; i++) {
            for (int j = 0; j < labirinto[i].length; j++) {
                System.out.print(labirinto[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("\n");
    }

The issue I don't understand is why it keeps going out of bounds, with the validations I got in the switch, why does it keep going to the wall. Also if I increment the variables in the else statement, it doubles the movement, which shouldn't happen since it doesn't hit a wall right?

0 Answers0