First of all, you should be initializing your mazePoints
array with the static ints you declared:
public static char[][] mazePoints = new char[mazeHeight][mazeWidth];
But you’ll need to mark them static first:
private static final int mazeHeight = 12;
private static final int mazeWidth = 58;
If they aren’t meant to be static, then neither should your mazePoints
array.
Next, you should read the file line by line instead of reading the entire thing at once:
final Scanner scanner = new Scanner(f);
for (int row = 0; row < mazeHeight && scanner.hasNext(); row++) {
final String mazeRow = scanner.next(); //Get the next row from the scanner.
mazePoints[row] = mazeRow.toCharArray(); //Convert the row into a char[].
}
You’re done. You have all your points in mazePoints
now.
Note That new Scanner(f).useDelimiter("\\A").next();
that you were using was just changing the default delimiter so that it read the entire file at once. You don’t want that behavior. Use the default behavior which reads a line at a time.
You can blindly traverse the entire 2D array as follows, though, you probably need to implement some type of algorithm rather than iterating the entire 2D array.
for (int y = 0; y < mazeHeight; y++) {
for (int x = 0; x < mazeWidth; x++) {
Point point = new Point(x, y);
//Do whatever with your point...
}
}
8
solved How to traverse java maze using stacks