1
I created a program that generates mazes that can be solved, but now I’m not able to implement a way for the size of the generated maze to be defined by the user.
The code below is ActionListener
of a button that when clicked asks the user the size, and I turned 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 class variable Maze_Generator
, because I need to use the grid array several times within other subclasses of the class Maze_Generator
, and now I can’t find a 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 didn’t put the whole class code here Maze_Generator
for the problem to be specific, but if necessary the remaining code I put.
you refer to the array
grid
?– user28595
yeah, that’s the one
– MDordio
What is this generative endgame??
– user28595
is a sub class that picks up the grid array after created and solves the maze
– MDordio
But why are you calling him before creating a Maze_generator instance? The code isn’t making much sense to me.
– user28595
as I said that I did not put the whole code here, but when I call the subclass geramazefinal it has inside Maze_generator.Gera_maze(); that will fetch the grid array created previously
– MDordio
But you are creating a static array already face, there is no change later. I was going to suggest creating a constructor by taking the captured data, but for that,
grid
cannot be Static.– user28595
I already put all the code of the class Maze_generator in the post inical
– MDordio
The problem is mixing static and instantaneous variables. As I said, you could make
grid
non-static, and pass the values in the class builderMaze_Generator
already creating the array with that size defined by the user. But being statical, there is no way, only creating another static array after capturing the data, since arrays size are immutable.– user28595
something like this? public Static void captur_data(int Row, int col) { char[][] test = new char[Row][col]; }
– MDordio