Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am writing a code for a java project to be able to solve a maze. I need to import a file called maze.dat and

I am writing a code for a java project to be able to solve a maze. I need to import a file called "maze.dat" and my code needs to be able to solve the maze. Can anyone please help me fix my code errors?

import java.util.*;

import java.io.*;

// Class that creates a maze array and attempts to solve.

// Key:

// X: Wall

// ' ': Valid path

// +: Beginning of maze. If a valid path to the end exists, the path will be marked with '+'.

// -: End of maze.

// .: Marker for a path that does not lead to the end of the maze.

public class MazeSolver {

public static void main(String[] args) {

MazeSolver m = new MazeSolver("maze.dat");

// To see the maze before and after the solution is attempted, uncomment the two commented lines in the main.

// m.printMaze();

// System.out.println();

m.solve();

System.out.println();

m.printMaze();

}

private char[][] maze;

private String fileName;

private int rowStart, columnStart, numRows, numColumns;

// Constructor that initializes the maze into an array from a file and finds

the starting position of the array.

public MazeSolver(String file) {

fileName = file;

// Determine the dimensions of the maze in order to create the array.

getMazeSize();

maze = new char[numRows][numColumns];

// Write the maze into the array from the file.

writeArray();

// Find the start and end positions of the maze.

findStart(maze);

}

// Method To print the maze.

public void printMaze() {

for (int i = 0; i < maze.length; i++) {

for (int j = 0; j < maze[i].length; j++) System.out.print(maze[i][j]);

System.out.println();

}

}

// Method that attempts to solve. Returns true if a solution is found, and

false otherwise.

public void solve() {

if (solve(rowStart, columnStart)) System.out.println("The maze has been

solved");

else System.out.println("Could not find the maze exit.");

}

// Method that traverses the maze recursively. The method inserts a '.' to

indicate which locations have been traversed and lead to a dead end. The path that

leads to the end is marked with a '+'.

private boolean solve(int row, int column) {

// Check for boundaries:

if (row < 0 && column < 0 && row >= maze.length && column >

maze[row].length) return false;

// Base case: If we are at the end of the maze, return true.

if (maze[row][column] == '-') return true;

//Check if the location is valid:

if (maze[row][column] != ' ' && maze[row][column] != '+') return false;

// Leave path tracer:

maze[row][column] = '.';

// Check upward:

if (solve(row - 1, column)) {

maze[row][column] = '+';

return true;

}

// Check rightward::

if (solve(row, column + 1)) {

maze[row][column] = '+';

return true;

}

// Check downward:

if (solve(row + 1, column)) {

maze[row][column] = '+';

return true;

}

// Check leftward:

if (solve(row, column - 1)) {

maze[row][column] = '+';

return true;

}

// No valid path available, maze is not solvable.

return false;

}

// Method that writes the maze into a 2d array.

private void writeArray() {

String s = "";

try {

File f = new File(fileName);

Scanner scan = new Scanner(f);

for (int i = 0; i < maze.length; i++) {

s = scan.nextLine();

for (int j = 0; j < maze[i].length; j++) {

maze[i][j] = s.charAt(j);

}

}

scan.close();

} catch (FileNotFoundException err) {

System.out.println("An error occurred. File not found.");

err.printStackTrace();

}

}

// Method to determine the dimensions of the maze in order to create the array.

private void getMazeSize() {

try {

File f = new File(fileName);

Scanner scan = new Scanner(f);

String line = scan.next();

numColumns = line.length();

while (scan.hasNextLine()) {

numRows++;

scan.nextLine();

}

scan.close();

} catch (FileNotFoundException err) {

System.out.println("An error occurred. File not found.");

err.printStackTrace();

}

}

// Method to find the start of the maze.

private void findStart(char[][] maze) {

for (int i = 0; i < maze.length; i++) {

for (int j = 0; j < maze[i].length; j++) {

if (maze[i][j] == '+') {

rowStart = i;

columnStart = j;

return;

}

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions

Question

=+(16.32) 81(w) for w A and 0 Answered: 1 week ago

Answered: 1 week ago

Question

4. Describe cultural differences that influence perception

Answered: 1 week ago