fill an already created matrix

0

I do not print the matrix with the data of the config method

public class main {
        static String[][] maze;     //matriz para el cambas
        static String player = "O"; //Icono del personaje 
        static String muro = "#";   //Icono del muro
        static String vacio = " ";  //Espacio en blamco del tablero
        static String Win = "V";    //Icono de mtea
        static String puerta = "="; // Puertas
        static String llave = ";";  //llave
        static int x = 1, y = 1;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        maze =  new String[10][20];
        crear();            //creamos el tablero
        actualizar(" ");    //Mostramos el tablero creado
        //play();               //Empieza el juego
    }

    public static void crear() {
        for (int i = 0; i < maze.length; i++) {
            for (int j = 0; j < maze[i].length; j++) {
                if (i == 0 || i == maze.length-1) {
                    maze[i][j] = muro;
                } else if (j == 0 || j == maze[i].length-1) {
                    maze[i][j] = muro;
                } else {
                    maze[i][j] = vacio;
                }
            }
        }
        config(); //configuramos el tablero en base a la dificultat
    }
    public static void config() {

            String [][] maze = {{"#","P","#"," "," "," "," "," "," "," ","#","#","#","#"," "," "," ",";"," ","#"},  //1
                                {"#"," ","#"," ","#"," ","#","#","#"," "," "," "," "," "," ","#"," ","#"," ","#"},  //2
                                {"#"," "," "," ","#"," ","#","#","#"," ","#"," ","#","#","#","#"," ","#","#","#"},  //3
                                {"#","=","#"," ","#","#"," "," ","#","#"," ","#"," ","#","#","#","#"," "," ","#"},  //4
                                {}}; //Esto entero

    }

    public static void actualizar(String txt) {
        //borrar();
        //Comprobamos si ha ganado
        if (maze[x][y] == Win) {
            //win();
        }else {
        maze[x][y] = player;
        for (int i = 0; i < maze.length; i++) {
            for (int j = 0; j < maze[i].length; j++) {
                System.out.print(maze[i][j]);
            }
            System.out.println("");
        }
        }
        //play(); //Comprobamos la tecla que pulsa el jugador
    }
}
    
asked by Djdadi43 04.12.2017 в 20:01
source

2 answers

1

Look at the following code. I eliminated the static methods (they look horrible) and everything works. Additionally, in the config () method change the initialization to use the global class.

/**
 * @author gmora 
 * */
public class Main {

    private String[][] maze;     //matriz para el cambas
    private final String player = "O"; //Icono del personaje
    private final String muro = "#";   //Icono del muro
    private final String vacio = " ";  //Espacio en blanco del tablero
    private final String Win = "V";    //Icono de meta
    private final String puerta = "="; // Puertas
    private final String llave = ";";  //llave
    private int x = 1, y = 1;

    public void setMaze(String[][] maze) {
        this.maze = maze;
    }


    public static void main(String[] args) {
        Main main = new Main();
        main.setMaze( new String[10][20]);
        main.crear();            //creamos el tablero
        main.actualizar(" ");    //Mostramos el tablero creado
        //play();               //Empieza el juego
    }

    public void crear() {
        for (int i = 0; i < maze.length; i++) {
            for (int j = 0; j < maze[i].length; j++) {
                if (i == 0 || i == maze.length - 1) {
                    maze[i][j] = muro;
                } else if (j == 0 || j == maze[i].length - 1) {
                    maze[i][j] = muro;
                } else {
                    maze[i][j] = vacio;
                }
            }
        }
        config(); //configuramos el tablero en base a la dificultad
    }

    public void config( ) {

        maze = new String[][] {{"#", "P", "#", " ", " ", " ", " ", " ", " ", " ", "#", "#", "#", "#", " ", " ", " ", ";", " ", "#"},  //1
                {"#", " ", "#", " ", "#", " ", "#", "#", "#", " ", " ", " ", " ", " ", " ", "#", " ", "#", " ", "#"},  //2
                {"#", " ", " ", " ", "#", " ", "#", "#", "#", " ", "#", " ", "#", "#", "#", "#", " ", "#", "#", "#"},  //3
                {"#", "=", "#", " ", "#", "#", " ", " ", "#", "#", " ", "#", " ", "#", "#", "#", "#", " ", " ", "#"},  //4
                {}}; //Esto entero

    }

    public void actualizar(String txt) {
        //borrar();
        //Comprobamos si ha ganado
        if (maze[x][y].equals(Win)) { //se usa equals para comparar String
            //win();
        } else {
            maze[x][y] = player;
            for (int i = 0; i < maze.length; i++) {
                for (int j = 0; j < maze[i].length; j++) {
                    System.out.print(maze[i][j]);
                }
                System.out.println("");
            }
        }
        //play(); //Comprobamos la tecla que pulsa el jugador
    }
}
    
answered by 04.12.2017 в 22:03
-1

Ignoring the other problems that I see in the code, it seems that your immediate problem is that you are working with 2 different variables maze .

You have the local variable in the config() method:

String [][] maze = {{...

And then you have the global variable maze :

static String[][] maze;     //matriz para el cambas

Obviously, what the config() method does is lost, because it does not work with the global variable maze .

A tip: try to avoid the use of global variables, just to avoid this type of mistakes and many more. If it already causes you problems with such a small program, imagine the headaches with a more complex program.

Recommended reading: Why is it considered bad practice to use global variables? .

    
answered by 04.12.2017 в 20:58