On the main method of my code on the very last line, I have a directory to run maze.txt from my desktop to run the maze. How can I fix this problem because if I send this code to someone else they have to open the file and change the directory to the directory of the maze.txt which they downloaded with my file.**
Maze.txt
7 7
GOOOOXO
XXOXOOX
OXOOOXX
XXXOOXO
XXXXOXX
SOOOOOX
XXXXXXX
import java.io.*;
public class MazeSolver {
private char [][] maze;
private int startX , startY;
private int counter = 0;
public MazeSolver(String fileName) throws IOException {
maze = fileIterator(fileName);
startX = startX(maze);
startY = startY(maze);
solve(startX,startY);
System.out.println(printMaze());
}
public void solve(int x, int y) {
if (findPath(x,y)) {
maze[x][y] = 'S';
}
}
public boolean findPath(int x , int y){
counter ++;
if (maze[x][y] > 7) {return false;}
if (maze[x][y] == 'G') {return true;}
if (maze[x][y] == 'X' || maze[x][y] == 'O'){return false;}
maze[x][y] ='O';
boolean result;
result = findPath(x , y+1);
if(result){return true;}
result = findPath(x-1 , y);
if(result){return true;}
result = findPath(x , y-1);
if(result){return true;}
result = findPath(x+1 , y);
if(result){return true;}
maze[x][y] = 'O';
return false;
}
public String printMaze() {
String output = "";
for (int x = 0; x < 7; x++) {
for (int y = 0; y < 7; y++) {
output += maze[x][y] + " ";
}
output += "\n";
}
return output;
}
private char[][] fileIterator(String fileName) throws IOException {
File file = new File(fileName);
if(!file.exists()){
System.out.println(fileName+ "does not exists");
}
if(!(file.canRead() && file.isFile())){
System.out.println(fileName + "can not be read");
}
BufferedReader read = new BufferedReader(new FileReader(file));
String rea = read.readLine();
String[] split = rea.split(" ");
int row = Integer.valueOf(split[0]);
int col = Integer.valueOf(split[1]);
String readline;
int num = 0;
char [][] maze = new char[row][col];
while((readline = read.readLine()) != null){
char[] ch = readline.toCharArray();
for(int i = 0;i < ch.length;i++){
maze[i][num] = ch[i];
}
num++;
}
return maze;
}
private static int startX(char[][] charArray){
int startX = 0;
for(int i=0 ; i < charArray.length ; i++){
for(int j=0 ; j < charArray[i].length ; j++){
if(charArray[i][j] == 'S'){
startX = i;
}
}
}
return startX;
}
private static int startY(char[][] charArray){
int startY = 0;
for(int i=0 ; i < charArray.length ; i++){
for(int j=0 ; j < charArray[i].length ; j++){
if(charArray[i][j] == 'S'){
startY = j;
}
}
}
return startY;
}
public static void main(String[] args) throws IOException {
MazeSolver ms = new MazeSolver("C:\\Users\\mypc\\Desktop\\maze.txt");
}
}
There are many solutions for that, depending on what you need.
A simple one, if you want the users to experiment with different maze.txt files that they can provide, is to get the path of the file from the command line arguments (i.e., from the args
parameter of the main method).
You could change your main method body to:
MazeSolver ms = new MazeSolver(args[0]);
Of course, further checks need to be made, but that's irrelevant for this exercise.
Then, the users run your program from the terminal as:
java MazeSolver /path/to/their/maze.txt
/path/to/their/maze.txt
will be captured by args[0]
in your main method.