Adapt the user input to the size of an array

1

I've created a program that generates mazes that can be solved, but now I'm not able to implement a way for the maze size to be defined by the user.

The code below is the ActionListener of a button that when clicked asks the user the size, and I made the input into an int

static class thehandler2 implements ActionListener {

    public void actionPerformed (ActionEvent event) {
        String row = JOptionPane.showInputDialog(null, "Insira o nº de linhas: ", "Question", JOptionPane.QUESTION_MESSAGE);
        String col = JOptionPane.showInputDialog(null, "Insira o nº de colunas: ", "Question", JOptionPane.QUESTION_MESSAGE);
        int foo = Integer.parseInt(row);
        int foo2 = Integer.parseInt(col);
        Maze_Generator.Gera_maze_final();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Maze_Generator view = new Maze_Generator();
                view.setVisible(true);

            }
        });
    }
}
The problem is that I initially created the grid array that contains the labyrinth as a global variable of the Maze_Generator class, since I need to use the array array multiple times within other subclasses of the Maze_Generator class, and now I can not get way to make the variable row = foo and col = foo2 .

public class Maze_Generator extends JFrame {
private static int row = 9;
private static int col = 9;
public static char[][] grid = new char[row][col];
public static List<Integer> l = new LinkedList<Integer>();
public static int pathIndex;

public Maze_Generator() {
    setTitle("Maze");
    setSize(640,480);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void Gera_maze_final() {
    int k = 0;
    while (true && k<100 ) {
        k+=1;
        if (l.size()==0&& k<100) {
            Maze_Generator.Gera_maze();
            char[][] maze = grid;
            Maze_Resolve_Gerada.Buscar(maze);
            int numrow = grid.length;
            int numcol = grid[0].length;
            boolean[][] checked = new boolean[numrow][numcol];
            DepthFirst.searchPath(maze, 1, 0, l, checked);
            pathIndex = l.size() - 2;
            //System.out.println(l.toString());

            }
        }

    }

public static void Gera_maze() {

    String AB = "_W";

    SecureRandom rnd = new SecureRandom();

    for (int i = 0; i < grid.length; i++) {
        for (int j = 0; j < grid[i].length; j++) {
            char c = AB.charAt(rnd.nextInt(AB.length()));
            grid[i][j] = c;
            grid[0][1] = 'S';
            grid[7][7] = 'E';

        }
    }

    for (char[] r : grid) {
        for (char c : r) {
            System.out.print(c);
        }
        System.out.println();
    }


    }

}

I have not put here the whole code of class Maze_Generator for the problem to be specific, but if I need the rest of the code I'll put it in.

    
asked by anonymous 16.06.2017 / 18:25

1 answer

0

Given the above tips and some research, I found the following solution to my previous problem:

public static int getRow() {
    String nrow = JOptionPane.showInputDialog(null, "Insira o nº de linhas: ", "Question", JOptionPane.QUESTION_MESSAGE);
    int  row = Integer.parseInt(nrow);
    return row;
}

public static int getCol() {
    String ncol = JOptionPane.showInputDialog(null, "Insira o nº de colunas: ", "Question", JOptionPane.QUESTION_MESSAGE);
    int  col = Integer.parseInt(ncol);
    return col;
}

private static int frow = getRow();
private static int fcol = getCol();
public static char[][] grid = new char[frow][fcol];

    
17.06.2017 / 02:12