For this homework you will be making a maze solver. Your program will take in fr
ID: 3815783 • Letter: F
Question
For this homework you will be making a maze solver. Your program will take in from a file 2 things. The size of the square maze, and the maze itself. The maze will consists of numbers between 0 and 3, where 0 is the start of the maze, 1 is an open path, 3 is a wall, and 2 end of the maze. For example a 6 times 6 maze could be represented by the following file, there will be no spaces separating the elements of the maze: Your program must then solve the maze. It will then output the correct path through the marked by 0's to the command line. For example the maze above it may output the following solution (I added spaces to make it more readable necessary): You can assume that the input contains the exact amount of numbers needed and that it is a solvable maze, following the rules outlined above. save your program as a "Homework2 java" file. Your program should read from a file named "in.txt if you have your program read from a different file you may receive no credit for this assignment.Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* @author master
*/
public class MazeSolver {
public int _count = 0;
public int size = 6;
public char[][] mazeArray;
public MazeSolver() {
// reading file
File file = new File("maze.txt");
try {
Scanner sc = new Scanner(file);
int temp = 0;
while (sc.hasNextLine()) {
// parsing each line
String line = sc.nextLine();
if(line.length() == 1){
mazeArray = new char[Integer.parseInt(line)][Integer.parseInt(line)];
System.out.println(Integer.parseInt(line));
this.size = Integer.parseInt(line);
}
else{
if(temp < this.size){
for(int i=0;i<this.size;i++){
//storing maze into array
mazeArray[temp][i] = line.charAt(i);
}
}
temp++;
}
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// Solve the mazeArray
public void SolveMaze(int m, int n) {
if (step(m, n)) {
mazeArray[m][n] = 'S';
}
}
// solving by recursive backtracking
public boolean step(int m, int n) {
_count++;
// if found the exit , then stop
if (mazeArray[m][n] == '2') {
return true;
}
// if hit the wall or hit the already selected path
if (mazeArray[m][n] == '3' || mazeArray[m][n] == '*') {
return false;
}
// marking location
mazeArray[m][n] = '*';
boolean _pathFound;
// go to step right
_pathFound = step(m, n + 1);
if (_pathFound) {
return true;
}
// go to step up
_pathFound = step(m - 1, n);
if (_pathFound) {
return true;
}
// go to step left
_pathFound = step(m, n - 1);
if (_pathFound) {
return true;
}
// go to step down
_pathFound = step(m + 1, n);
if (_pathFound) {
return true;
}
// if it is dead-end then mark
mazeArray[m][n] = ' ';
return false;
}
public String toString() {
String displayStr = "";
for (int i = 0; i < this.size; i++) {
for (int j = 0; j < this.size; j++) {
displayStr += mazeArray[i][j] + " ";
}
displayStr += " ";
}
return displayStr;
}
public static void main(String[] args) {
MazeSolver mazesolver = new MazeSolver();
// start solving
mazesolver.SolveMaze(1,3);
System.out.println(mazesolver);
System.out.println("Total backtracking function calls: " + mazesolver._count);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.