Question: Below is my program to solve a maze. It only solves the one maze which is coded directly into it, but it needs to be
Below is my program to solve a maze. It only solves the one maze which is coded directly into it, but it needs to be able to pull a maze from a .txt file and then solve that. It should pull the maze from the .txt and represent it as a 2d array of Strings. I will include a visual example of maze.txt.(Chegg messes up the formating otherwise I would paste it here). It important to note that the maze in the .txt will not always be of the same dimensions.
The below program solves a maze by representing the finish point as a 9, walls as a 5, and empty space as 0. It then uses a special print method to print the maze in the proper form(that which is used in the .txt). S is the start, F is the finish, X is walls, etc. In your solution you can choose to either fix the logic in my code to match the proper form, or you can simple make sure that once the file in read from maze.txt that it is in the form my maze can solve.
Please include the modified code and an example case of you using it.
The program must be executable in the terminal by typing:
javac Maze.java
java Maze maze.txt
Here is the program:
import java.util.*;
public class Maze {
public static String[][] arr = new String[][] {
{"5","5","5","5","5","5","5","5","5"},
{"5","5","5","0","0","0","0","0","5"},
{"5","0","0","5","0","0","0","0","5"},
{"5","0","0","0","0","0","0","0","5"},
{"5","0","0","0","0","0","0","0","5"},
{"5","0","0","0","0","0","0","0","5"},
{"5","0","0","0","5","5","5","5","5"},
{"5","0","0","0","0","0","0","9","5"},
{"5","5","5","5","5","5","5","5","5"},
};
public static void main(String[] args) {
Vertex originVertex = new Vertex(3,2,null);
arr[originVertex.getX()][originVertex.getY()] = "7";
printInForm();
Vertex p = getShortestPath(originVertex);
if(getShortestPath(originVertex) != null){
System.out.println("Solution Found!");
while(p.getParent() != null) {
if(arr[p.getX()][p.getY()] != "9" && arr[p.getX()][p.getY()] != "7"){
arr[p.getX()][p.getY()] = "S";
}
p = p.getParent();
}
arr[originVertex.getX()][originVertex.getY()] = "7";
printInForm();
}else{
System.out.println("No Solution.");
}
}
public static Queue
public static Vertex getShortestPath(Vertex originVertex) {
//Queue
int x = originVertex.getX();
int y = originVertex.getY();
q.add(new Vertex(x,y, null));
while(!q.isEmpty()) {
Vertex p = q.remove();
if (arr[p.x][p.y] == "9") {
return p;
}
if(isFree(p.x+1,p.y)) {
arr[p.x][p.y] = "-1";
Vertex nextP = new Vertex(p.x+1,p.y, p);
q.add(nextP);
}
if(isFree(p.x-1,p.y)) {
arr[p.x][p.y] = "-1";
Vertex nextP = new Vertex(p.x-1,p.y, p);
q.add(nextP);
}
if(isFree(p.x,p.y+1)) {
arr[p.x][p.y] = "-1";
Vertex nextP = new Vertex(p.x,p.y+1, p);
q.add(nextP);
}
if(isFree(p.x,p.y-1)) {
arr[p.x][p.y] = "-1";
Vertex nextP = new Vertex(p.x,p.y-1, p);
q.add(nextP);
}
}
return null;
}
public static boolean isFree(int x, int y) {
if((x >= 0 && x = 0 && y
return true;
}
return false;
}
public static void printInForm(){
for (int i = 0; i
for (int j = 0; j
if(arr[i][j] == "-1" || arr[i][j] == "0")
System.out.print(" ");
if(arr[i][j] == "9")
System.out.print(" F ");
if(arr[i][j] == "5")
System.out.print(" X ");
if(arr[i][j] == "S")
System.out.print("(.)");
if(arr[i][j] == "7")
System.out.print(" S ");
}
System.out.println();
}
}
private static class Vertex {
int x;
int y;
Vertex parent;
public Vertex(int x, int y, Vertex parent) {
this.x = x;
this.y = y;
this.parent = parent;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public Vertex getParent() {
return this.parent;
}
public String toString() {
return "x = " + x + " y = " + y;
}
}
}

File Edit Format View Help xS XXX XxX FX atl 7:32 PM 411/25/2017
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
