Search code examples
javabufferedreadermaze

reading the maze file and print it


This program is about maze recursion and I'm struggling finding out how to read the maze file and print the solved maze since I'm new in Java. Could anyone explain it to me how to call the print method from main method? Thank you in advance!

public class Maze {

    private static char[][] maze;
    private static int rows = 0;
    private static int columns = 0;


    public Maze(char[][] mazeIn) {
        maze = mazeIn;
    }

    private static boolean valid (int r, int c) {

          boolean result = false;

          // check if cell is in the bounds of the matrix
          if (r >= 0 && r < maze.length &&
              c >= 0 && c < maze[0].length)

             //  check if cell is not blocked and not previously tried
             if (maze[r][c] == '1')
                result = true;

          return result;

       }  // method valid

    public static boolean solve (int r, int c) {

          boolean done = false;

          if (valid (r, c)) {

             maze[r][c] = '7';  // cell has been tried

             if (r == maze.length-1 && c == maze[0].length-1)
                done = true;  // maze is solved
             else {
                done = solve (r+1, c);  // down
                if (!done)
                   done = solve (r, c+1);  // right
                if (!done)
                   done = solve (r-1, c);  // up
                if (!done)
                   done = solve (r, c-1);  // left
             }
             if (done)  // part of the final path
                maze[r][c] = '8';
          }

          return done;

       }  // method solve

    public static void print () {

          System.out.println();

          for (int r=0; r < maze.length; r++) {
             for (int c=0; c < maze[r].length; c++)
                System.out.print (maze[r][c]);
             System.out.println();
          }

          System.out.println();

       }  // method print_maze


    public static void main(String[] args) throws IOException  {

      String fileName = "Maze.txt";

      try {
            String readline;

            FileReader fileReader = 
                    new FileReader(fileName);

                BufferedReader br = 
                    new BufferedReader(fileReader);

                int line = 0;
                while((readline = br.readLine()) != null) {
                System.out.println(readline); //loads the maze

                char[] charArr = readline.toCharArray();
                maze[line] = charArr;  // error here

                line++;
                }

        br.close();         
    }

    catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");                  

        }
    }
} 

the maze.txt file looks like this

 000100000000000  
 000100001000010  
 000111111111000   
 000100000001000  
 000111110001000  
 000000010001000  
 000011110001000  
 000010010001010  
 000010010000000  
 000010000000000  
 000011111110000  
 000000000010000  
 000000000010000  
 000001000011110  
 000000000010000  

Solution

  • There are a few things I would do differently, but the important part is that you're not populating the char array with any data. While you're reading in the data, you need to populate it into the char array. Something like this would work:

     int line = 0;
     while((readline = br.readLine()) != null) {
         System.out.println(readline); //loads the maze
         char[] charArr = readline.toCharArray();
         maze[line] = charArr;
         line++;
     }
    

    Additionally, the maze array is never actually instantiated, and the main method is never calling "solve()" or "print()". These are things you can handle in your main method.

    Finally, if you're going to call "solve()," you have to decide whether you want to instantiate an instance of the Maze class and call solve on that (which would involve quite a bit of code changes, but is the right approach), or else "solve()" should be a static method as well, which means "valid()" also has to be static.

    P.S.: you have an error in your for-loop

    for (int c=0; columns < maze[r].length; c++)
    

    You should just change "columns" to "c".

    P.P.S., in your solve method you're assigning ints to a char array. Put 7 and 8 in single-quotes to indicate that they're chars, not ints.

     maze[r][c] = '7';  // cell has been tried
    

    This is especially important in your valid() method, because you're testing if the maze[r][c]==1, but you should be testing if maze[r][c]=='1'.

    I made all these changes myself and got this as output

     000700000000000  
     000700007000010  
     000777777777000   
     000700000007000  
     000777770007000  
     000000070007000  
     000077770007000  
     000070070007010  
     000070070000000  
     000070000000000  
     000077777770000  
     000000000070000  
     000000000070000  
     000001000077770  
     000000000070000