Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q: In java I need to create a recursive maze solver and it needs to read in a tx

ID: 3692682 • Letter: Q

Question

Q:

In java I need to create a recursive maze solver and it needs to read in a txt file that could look something like this

where the 6 indicates its a 6x6 maze and 1 are open paths, 3's are walls, 0 is the start, and 2 is the finish.

so far my code looks like and im super confused on what to do next.

import java.util.Scanner;
import java.io.File;

public class mazesolver {

   public static void main(String[] args)
   {  
           try
           {
               File f = new File("in.txt");
               Scanner input = new Scanner(f);
              
               int rows = input.nextInt();  
               int col = rows;
               int [][] maze = new int[rows][col];
              
              
String s = "";
               //String start = "0";
               //String end = "2";
              
              
               //System.out.println(rows);
               //System.out.println(col);
               input.nextLine();
               int r = 0;
               while(input.hasNextLine())
               {
                   s = input.nextLine();
              
//System.out.println(s);
                  
                  
                   //System.out.println(s.indexOf(start));
                   //System.out.println(s.indexOf(end));
                  
                       for(int c = 0; c < col; c++)
                       {
                           maze[r][c] = Integer.parseInt(""+s.charAt(c));
System.out.println(maze[r][c]);
                       }
                      
               }
                   r++;
              
              
               //System.out.println(s);
              
  
               /*while(input.hasNextLine())
               {
                   System.out.println(input.nextLine());
               }*/
               }
          
          
          
          
          
          
           catch(Exception e)
           {
               e.printStackTrace();
           }
   }

Explanation / Answer

Please follow the code and the comments for description :

CODE :

import java.util.*;
import java.io.File;
public class MazeGame {

public static void main(String[] args) throws Exception {
    Scanner scan = new Scanner(new File("maze.txt"));
    Scanner user = new Scanner(System.in);
    int rows = scan.nextInt();
    int columns = scan.nextInt();
    int px = 0;
    int py = 0;
    String [][] maze = new String[rows][columns];
    String junk = scan.nextLine();

    for (int i = 0; i < rows; i++){
        String temp = scan.nextLine();
        String[] arrayPasser = temp.split("");
        for (int j = 0; j < columns; j++){
            maze[i][j] = arrayPasser[i];
        }
    }

    boolean gotPath = false;

    while (gotPath == false){
        for (int i = 0; i < rows; i++){
            for (int j = 0; j < columns; j++){
                System.out.print(maze[i][j]);
                System.out.print(" ");
        }
            System.out.print(" ");
      }


        System.out.printf(" ");
        System.out.println("You may:");
        System.out.println("1) Move up");
        System.out.println("2) Move down");
        System.out.println("3) Move left");
        System.out.println("4) Move right");
        System.out.println("0) Quit");
        int choice = user.nextInt();
        int i = 0;

        if (choice == 1 && i >= 0 && i < columns){
            for (int k = 0; k < rows; k++){
                for (int l = 0; l < columns; l++){
                    if (maze[k][l].equals(maze[px][py]) && maze[px][py-1].equals("3") == false){
                        maze[px][py] = "0";
                        maze[k][l-1] = "1";
                        maze[px][py] = maze[k][l-1];
                    }else if (maze[px][py-1] == "3"){
                        System.out.println("Cannot move! Try something else.");
                    }else {
                    continue;}


                    }
                }
            }
        else if (choice == 2 && i >= 0 && i < columns){
            for (int k = 0; k < rows; k++){
                for (int l = 0; l < columns; l++){
                    if (maze[k][l].equals(maze[px][py]) && maze[px][py+1].equals("3") == false){
                        maze[px][py] = "0";
                        maze[k][l+1] = "1";
                        maze[px][py] = maze[k][l+1];
                    }else if (maze[px][py+1] == "3"){
                        System.out.println("Cannot move! Try something else.");
                    }else {
                    continue;}

               }
             }
            }
        else if (choice == 3 && i >= 0 && i < columns){
            for (int k = 0; k < rows; k++){
                for (int l = 0; l < columns; l++){
                    if (maze[k][l].equals(maze[px][py]) && maze[px-1][py].equals("3") == false){
                        maze[px][py] = "0";
                        maze[k-1][l] = "1";
                        maze[px][py] = maze[k-1][l];
                    }else if (maze[px-1][py] == "3"){
                        System.out.println("Cannot move! Try something else.");
                    }else {
                    continue;}
                }
            }
        }
        else if (choice == 4 && i >= 0 && i < columns){
            for (int k = 0; k < rows; k++){
                for (int l = 0; l < columns; l++){
                    if (maze[k][l].equals(maze[px][py]) && maze[px+1][py].equals("3") == false){
                        maze[px][py] = "0";
                        maze[k+1][l] = "1";
                        maze[px][py] = maze[k+1][l];
                    }else if (maze[px+1][py] == "3"){
                        System.out.println("Cannot move! Try something else.");
                    }else {
                    continue;}
                }
            }
        }
        else if (choice == 0){
            System.exit(0);
        }
    }

    System.out.println("Congratulations, you found the path!");

    scan.close();
    user.close();
        }

    }

The text file can be user defined with the given conditions.

Hope this is helpful.