For my homework assignment we are to traverse a virtual maze using recursion. I am wanting to make sure I am reading in the file properly. The goal of my code at this point is to read in a text file containing the maze and place an 'X' at the starting point of the maze.
Additionally, I read on another SO post about using BufferedReader with FileReader but the post was kind of vague - what is advantage of that?
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class ReadInMaze
{
private static char[][] maze = null;
private static int rows = 0;
private static int cols = 0;
private static int xStart = 0;
private static int yStart = 0;
public static void Maze(File mazeFile) throws IOException
{
File mazeFile = new File ("C:/Users/Mark/workspace/18-20_MazeTraversal_Hard/src/MazeForMazeTraversalHW.txt");
BufferedReader reader = new BufferedReader(new FileReader(mazeFile));
Scanner lineOfFile = new Scanner(reader.readLine());
rows = lineOfFile.nextInt(); //get the number of rows of the maze
cols = lineOfFile.nextInt(); // get the number of columns of the maze
maze = new char[rows][cols]; //create a char array of the proper size
//For loops to iterate the rows and col to find the start/enterance of the maze as it pertains to the first char in the row
for (int y = 0; y < cols; y ++)
{
lineOfFile = new Scanner(reader.readLine());
for(int x = 0; x < rows; x++)
{
char start = lineOfFile.next().charAt(0);
maze[x][y] = start;
//statement to set the starting coorinates for the maze
if (start == '.')
{
xStart = x;
yStart = y;
}
}
}
}
The maze from my text file looks like this:
# # # # # # # # # # # #
# . . . # . . . . . . #
. . # . # . # # # # . #
# # # . # . . . . # . #
# . . . . # # # . # . .
# # # # . # . # . # . #
# . . # . # . # . # . #
# # . # . # . # . # . #
# . . . . . . . . # . #
# # # # # # . # # # . #
# . . . . . . # . . . #
# # # # # # # # # # # #
I did something like this for one of my classes here is how I read in the maze, I used a filechooser so you should replace that with your file name, then once you have the maze you can manipulate it as you please
BufferedReader read = new BufferedReader(new FileReader(chooser.getSelectedFile()));
String rea = read.readLine();
String[] split = rea.split(" ");
width = Integer.valueOf(split[0]);
height = Integer.valueOf(split[1]);
String readline;
int num = 0;
maze1 = new char[width][height];
while((readline = read.readLine()) != null){
char[] ch = readline.toCharArray();
for(int i = 0;i < ch.length;i++){
maze1[i][num] = ch[i];
}
num++;
}
that is assuming your maze is formatted like this with the width and the height at the top of the maze file separated with a space
12 12
# # # # # # # # # # # #
# . . . # . . . . . . #
. . # . # . # # # # . #
# # # . # . . . . # . #
# . . . . # # # . # . .
# # # # . # . # . # . #
# . . # . # . # . # . #
# # . # . # . # . # . #
# . . . . . . . . # . #
# # # # # # . # # # . #
# . . . . . . # . . . #
# # # # # # # # # # # #