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 =...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • The following class class studentType { private: char firstName[25]; char lastName[25]; int testScore[4]; double averageScore; public:...

    The following class class studentType { private: char firstName[25]; char lastName[25]; int testScore[4]; double averageScore; public: }; int main(){ studentType student[30]; } Write c++ code ti create a constructor of studentType to initialize the member variables. Also write function to input values of the member variables and print the values

  • 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...

  • public class CharNode{ private CharNode link; private char info; public CharNode(char info) { this.info = info;...

    public class CharNode{ private CharNode link; private char info; public CharNode(char info) { this.info = info; this.link = null; } public CharNode(char c, CharNode link) { this.info = info; this.link = link; } public CharNode getLink() { return link; } public void setLink(CharNode link) { this.link = link; } public char getInfo() { return info; } public void setInfo(char info) { this.info = info; } } //Remember Queue is a first-in-first-out data structure public class CharQueue { // front is...

  • 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{...

  • how to call maze1 and maze2 object to be use in print method maze2.maze[i][j] maze2 is...

    how to call maze1 and maze2 object to be use in print method maze2.maze[i][j] maze2 is object maze[i][j] is variable from different class i just need help in calling object to be used in the print method and main will call print method to display. import java.io.*; public class MazeSolver {        //=========================================== // object reader //==========================================    public static void read()throws FileNotFoundException,IOException{              reader maze1 = new reader(new FileReader("maze1.txt"));        reader maze2 = new reader(new...

  • 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...

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