0

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");
}
}
vefthym
  • 7,422
  • 6
  • 32
  • 58
  • 1
    you can make the path a runtime argument and get if from the `args` of the main method (like here: https://stackoverflow.com/a/22847894/2516301). Read about command line arguments in java – vefthym Feb 22 '19 at 00:18
  • Can you explain more? – Mike - shde2007 Feb 22 '19 at 00:20
  • 1
    use relative paths, ". /maze. txt" – Kai Aeberli Feb 22 '19 at 00:21
  • Do you always want to run the same txt file, or the user could provide his/her own? My previous comment was for the latter case. If it's the former case, you could use Kai Aeberli's suggestion. – vefthym Feb 22 '19 at 00:24
  • I'd use a packaging tool like grade or maven. The maze.txt would be a resource of the project and packaged within the jar file. The java program could be updated to look for a specific path outside the jar file to load a maze file which is local to the user. I'd remove the static's on the startX() and startY() method, only main() should be static – emeraldjava Feb 22 '19 at 00:27
  • Users can provide their own but Kai's suggestion gave me an error on BufferedReader read = new BufferedReader(new FileReader(file)); – Mike - shde2007 Feb 22 '19 at 00:29
  • @shde2007 that's probably because you didn't copy the maze.txt file to the correct folder (root folder of your project I guess). It's always useful to read the error messages for hints and instructions. – vefthym Feb 22 '19 at 00:31
  • emeraldjava : Thanks but I'm not allow to use the package. – Mike - shde2007 Feb 22 '19 at 00:32
  • @shde2007 I don't really understand what you mean, but then try my first suggestion. – vefthym Feb 22 '19 at 00:34
  • @shde2007 please let me know if my answer solves your problem – vefthym Feb 22 '19 at 00:52

2 Answers2

0

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.

vefthym
  • 7,422
  • 6
  • 32
  • 58
0
import java.util.Scanner;
import java.io.*;

public class MazeSolver {
   private char[][] maze;
   private int startX;
   private int startY;
   private int row;
   private int col;
   private int endX;
   private int endY;

   public MazeSolver (String fileName) {
      try {
         Scanner get = new Scanner(new FileReader(fileName));


         row = get.nextInt(); // Integer.parseInt(sChar[0]);
         col = get.nextInt(); //col = Integer.parseInt(sChar[2]);        
         maze = new char[row][col];      
         String mazeString = "";

         // Read the entire file and store in a String called mazeString
         while(get.hasNextLine()) {
            mazeString += get.nextLine();
         }

         char[] temp = mazeString.toCharArray();

         for(int x = 0; x < row * col; x++) {
            maze[x/row][x%col] = temp[x];
         }
      }

      catch (IOException e) {
       System.out.println("\nFile cannot be found. Please try again: \n");

         System.exit(0);
      }

      char start = 'S';
      for(int i = 0; i < row; i++) {
         for(int x = 0; x < col; x++) {
            char setStart = maze[i][x];
            if(setStart == start) {
               startX = i;
               startY = x;
            }
         }
      }

      char goal = 'G';
      for(int i = 0; i < row; i++) {
         for(int x = 0; x < col; x++) {
            char setEnd = maze[i][x];
            if(setEnd == goal) {
               endX = i;
               endY = x;
            }
         }
      }
      if (solveMaze(startX,startY)){
         System.out.println("Solution Found");
         printMaze();
      }
      else{
         System.out.println("No solution Found");
         printMaze();
      }

   }

   public void printMaze() {
        for(int r = 0; r < row; r++) {
                for(int c = 0; c < col; c++) {
            System.out.print(maze[r][c]);
         }
         System.out.println();

      }
        }

   public boolean solveMaze(int x, int y) {
       // Base case
      if(x == endX && y == endY) {
         maze[x][y] = 'G';
         maze[startX][startY]='S';
         return true;
      }

       // Out of bounds
      if((x >= 0 && x < row && y >= 0 && y < col && maze[x][y] != 'X' && maze[x][y] != '.') == true) {
         maze[x][y] = '.';

       // Right
         if(solveMaze(x + 1, y)) {
            return true;
         }

       // Left
         if(solveMaze(x - 1, y)) {
            return true;
         }

       // Up
         if(solveMaze(x, y - 1)) {
            return true;
         }

       // Down
         if(solveMaze(x, y + 1)) {
            return true;
         }
         else {
            maze[x][y] = '#'; 
            return false;
         }
      }
      else {
         return false;
      }
   }


}
garagol
  • 15
  • 5