Question

Create a class called MazeSolver: public class MazeSolver {    private char[][] maze;    private int startx,                    &

Create a class called MazeSolver:

public class MazeSolver

{

   private char[][] maze;

   private int startx,

                    starty;

  Public MazeSolver(String fileName) throws IOException

  {

     // create an object to read information from “fileName”

     // read the maze dimension (row col) from the file

     // Allocate the space for maze

     // initialize array maze with contents of the file

     // find startx and starty

     printMaze(); // a method that prints the maze

     // solveMaze() is a recursive method to solve the maze

     if(solveMaze(maze,startx,starty)) {

       System.out.println(“Solution found”);

       printMaze();

    }

    else {

       System.out.println(“No solution found”);

    }


0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANS :)

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

public class MazeSolver {
private static char[][] maze;
private int startX=0;
private int startY=0;
private int endX=0;
private int endY=0;

public MazeSolver (final String filename) throws IOException {
int heightCounter = 0;
try (Scanner sc = new Scanner(new File(filename))) {
int width = sc.nextInt();
int height = sc.nextInt();
maze = new char[width][height];
startX = sc.nextInt();
startY = sc.nextInt();
endX = sc.nextInt();
endY = sc.nextInt();

while (sc.hasNext()) {
String line = sc.nextLine();

int counter = 0;
for (int i = 0; i < line.length(); i++){
if(line.charAt(i) != ' '){
maze[heightCounter][counter] = line.charAt(i);
counter++;
}
}
heightCounter++;
}
maze[startX][startY] = 'S';
maze[endX][endY] = 'E';

for (int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {

if(maze[i][j] == '1') {
maze[i][j] = '#';
}

if(maze[i][j] == '0') {
maze[i][j] = ' ';
}
}
}
//return new MazeSolver(maze, startX, startY, endX, endY);
} catch (InputMismatchException e) {
throw new IOException("Input cannot be parsed", e);
}
}
  
private boolean solve(int i, int j) {

if (maze[i][j] == '#') {
return false;
}

if (maze[i][j] == 'E') {
return true;
}

if (maze[i][j] == 'X') {
return false;
}

maze[i][j] = 'X';

//South
if ((solve(i + 1, j)) == true) {
return true;
}
//West
if ((solve(i, j - 1)) == true) {
return true;
}
//East
if ((solve(i , j + 1)) == true) {
return true;
}
//North
if ((solve(i - 1 , j)) == true) {
return true;
}   

maze[i][j] = ' ';
return false;
}
  
private void printMaze() {

maze[startX][startY] = 'S';
for (int i = 0; i < maze.length; i++) {
System.out.println(maze[i]);
}
}
  
public static void main(String[] args) throws Exception
{

MazeSolver ms = new MazeSolver("D:\\mazeFile.txt");
if(ms.solve(ms.startX, ms.startY)) {
ms.printMaze();
}
else {
System.out.println("The maze could not be solved");
}   
}   
}

INPUT File : mazeFile.txt

-------------------------------

11 11
1 1
8 8
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 1 0 1 1 1 1 1 1
1 0 1 0 0 0 0 0 0 1
1 0 1 1 0 1 0 1 1 1
1 0 1 0 0 1 0 1 0 1
1 0 1 0 0 0 0 0 0 1
1 0 1 1 1 0 1 1 1 1
1 0 1 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1

Output : I hope you will get the answer

Add a comment
Know the answer?
Add Answer to:
Create a class called MazeSolver: public class MazeSolver {    private char[][] maze;    private int startx,                    &
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Problem A robot is asked to navigate a maze. It is placed at a certain...

    The Problem A robot is asked to navigate a maze. It is placed at a certain position (the starting position) in the maze and is asked to try to reach another position (the goal position). Positions in the maze will either be open or blocked with an obstacle. Positions are identified by (x,y) coordinates. At any given moment, the robot can only move 1 step in one of 4 directions. Valid moves are: ● Go North: (x,y) -> (x,y-1) ●...

  • departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE =...

    departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee;    } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...

  • public class File_In_36 {    private String filename;    public int[][] matrix()    {       //...

    public class File_In_36 {    private String filename;    public int[][] matrix()    {       // 1. matrix will call scan_File to determine whether or not the file       // name entered by the user actually exists       // 2. matrix will call size_matrix to determine the number of integers       // in the input file and set the instance variable matrix_size to that       // number       // 3. matrix will call check_square to determine whether or not matrix_size...

  • Examine the following class definition: public class Date private int year; private int month; private int...

    Examine the following class definition: public class Date private int year; private int month; private int day; public Date() { ...) public void set (int x, int y, int z) { ...) public int getYear() { ...) // returns year public int getMonth() { } // returns month public int get Day () { ...) // returns day //more methods here -- 1 Which of the following statements in a client program correctly prints out the day of the object...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private...

    package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private static int cardNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; private static char suitName[] = { 'c', 'd', 'h', 's' }; public static void main(String[] args) throws CardException, DeckException, HandException { Scanner kb = new Scanner(System.in); System.out.println("How many Players? "); int numHands = kb.nextInt(); int cards = 0; if (numHands > 0) { cards = 52 / numHands; System.out.println("Each player gets " + cards + " cards\n"); } else...

  • Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static...

    Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static String solve(char[][] grid) {        // TODO        /*        * 1. Construct a graph using grid        * 2. Use BFS to find shortest path from start to finish        * 3. Return the sequence of moves to get from start to finish        */               // Hardcoded solution to toyTest        return...

  • JAVA PROBLEMS 1. Update the below method to throw an IndexOutOfBoundsException: public E get(int index) {...

    JAVA PROBLEMS 1. Update the below method to throw an IndexOutOfBoundsException: public E get(int index) {         if (index < 0 || index > numItems) {             System.out.println("Get error: Index "                         + index + " is out of bounds.");             return null;         }         return array[index]; } Hint: Update both the method signature and the method body! 2. Update the below method to include a try-catch block rather than throwing the exception to...

  • You’ll create the parser with a 2-part layered approach. You’ll first create a Lexer, which will read characters from a file and output tokens. Then you’ll write the second part, which will process th...

    You’ll create the parser with a 2-part layered approach. You’ll first create a Lexer, which will read characters from a file and output tokens. Then you’ll write the second part, which will process the tokens and determine if there are errors in the program. This program have 3 files Lexer.java, Token.java, Parser.java Part 1 Lexer: The class Lexer should have the following methods: public constructor which takes a file name as a parameter○private getInput which reads the data from the...

  • Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int...

    Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int row) String field(int row, int column) Please use the CSVReader and CSVReaderTester class to complete the code. I have my own CSV files and cannot copy them to here. So if possible, just use a random CSV file. CSVReader.java import java.util.ArrayList; import java.util.Scanner; import java.io.*; /**    Class to read and process the contents of a standard CSV file */ public class CSVReader {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT