hello the code given is mostly complete but I need help with the Solver portion please
Consider a maze made up of rectangular array of squares, such as the following one:
X X X X X X X X X X X X X
X X X X X X
X X X X X
X X X X X X X X
X X X X X
X X X X X X X
X X X X X X X X X X X X X
Figure 1--- A sample maze
The X blocks represent a blocked square and form the walls of the maze. Let us consider mazes
that
have only one entrance and one exit, as in our example. Beginning at the entrance at the top left
side of
the maze, find a path to the exit at the bottom right side. You can move only up, down, left, or
right.
Square is either clear or blocked by an X character.
Let a two-dimensional array represent the maze. Use a stack data structure to find a path through
the
maze. Some mazes might have more than one successful path, while others might have no path.
Hints:
The primary consideration is that if you reach a dead end, you need to be able to backtrack
along the path to the point where you can try a different path. The stack data structure makes it
possible to store the current path and backtrack if necessary. Every clear position visited in the
current path is pushed to the stack and if a dead end is reached, the previous position is popped
from the stack to backtrack. You need to have a loop that begins with the start position and
pushes it into the stack then moves to a neighboring cell until either the goal position is reached,
or the stack becomes empty.
Make sure to mark each clear array cell you move to as visited to avoid checking it again and
getting stuck in an infinite loop. For example, each array cell can have three different values: X
for blocked, V for visited, and for clear.
A dead end is an array cell whose all neighbors are either blocked or already visited.
There are four files you should pay attention to:
Position.java: a class to store the position of an array cell. It has two attributes: row and
column along with their accessor and mutator methods. It also has an equals method that
checks for equality of two Position objects.
Maze.java: a class to store a maze. It has 3 attributes: a two-dimensional character array which
represents the maze, a Position object to represent the start location, and another Position
object to represent the stop location.
MazeSolver: This class contains one static method, solve. It accepts a maze and returns an
array of Positions that is used to traverse the maze if the maze is solvable. If the maze is not
solvable then an empty array is returned.
o Keep in mind that stacks store objects in reverse order so be sure to return the position
array in the correct order.
Tester: a sample driver class which calls the traverse method on a maze in either the
SolvableMaze, UnsolvableMaze, or HugeSolvableMaze files. These should be at the root of the
project folder
SOLVER
import java.util.Stack;
public class MazeSolver {
public static Position[] solve(Maze maze) {
return null;
}
}
POSITION
public class P.
A Critique of the Proposed National Education Policy Reform
hello the code given is mostly complete but I need help with the Sol.pdf
1. hello the code given is mostly complete but I need help with the Solver portion please
Consider a maze made up of rectangular array of squares, such as the following one:
X X X X X X X X X X X X X
X X X X X X
X X X X X
X X X X X X X X
X X X X X
X X X X X X X
X X X X X X X X X X X X X
Figure 1--- A sample maze
The X blocks represent a blocked square and form the walls of the maze. Let us consider mazes
that
have only one entrance and one exit, as in our example. Beginning at the entrance at the top left
side of
the maze, find a path to the exit at the bottom right side. You can move only up, down, left, or
right.
Square is either clear or blocked by an X character.
Let a two-dimensional array represent the maze. Use a stack data structure to find a path through
the
maze. Some mazes might have more than one successful path, while others might have no path.
Hints:
The primary consideration is that if you reach a dead end, you need to be able to backtrack
along the path to the point where you can try a different path. The stack data structure makes it
possible to store the current path and backtrack if necessary. Every clear position visited in the
current path is pushed to the stack and if a dead end is reached, the previous position is popped
from the stack to backtrack. You need to have a loop that begins with the start position and
pushes it into the stack then moves to a neighboring cell until either the goal position is reached,
or the stack becomes empty.
Make sure to mark each clear array cell you move to as visited to avoid checking it again and
getting stuck in an infinite loop. For example, each array cell can have three different values: X
for blocked, V for visited, and for clear.
A dead end is an array cell whose all neighbors are either blocked or already visited.
There are four files you should pay attention to:
Position.java: a class to store the position of an array cell. It has two attributes: row and
column along with their accessor and mutator methods. It also has an equals method that
2. checks for equality of two Position objects.
Maze.java: a class to store a maze. It has 3 attributes: a two-dimensional character array which
represents the maze, a Position object to represent the start location, and another Position
object to represent the stop location.
MazeSolver: This class contains one static method, solve. It accepts a maze and returns an
array of Positions that is used to traverse the maze if the maze is solvable. If the maze is not
solvable then an empty array is returned.
o Keep in mind that stacks store objects in reverse order so be sure to return the position
array in the correct order.
Tester: a sample driver class which calls the traverse method on a maze in either the
SolvableMaze, UnsolvableMaze, or HugeSolvableMaze files. These should be at the root of the
project folder
SOLVER
import java.util.Stack;
public class MazeSolver {
public static Position[] solve(Maze maze) {
return null;
}
}
POSITION
public class Position {
private int row;
private int column;
public Position(int row, int column) {
this.row = row;
this.column = column;
}
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof Position)) {
return false;
3. }
Position otherPosition = (Position) obj;
return (otherPosition.row == row && otherPosition.column == column);
}
public String toString() {
return "row:" + row + " column:" + column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
}
MAZE
public class Maze {ate final char[][] maze;
private final Position start, end;
public Maze(char[][] maze, Position start, Position end) {
this.maze = maze;
if (validPosition(start)) {
this.start = start;
} else {
throw new PositionNotValidException("The start position is valid for the maze: " +
start);
}
if (validPosition(end)) {
this.end = end;
} else {
throw new PositionNotValidException("The stop position is valid for the maze: " + end);
}
}
public Position getStart() {
return start;
}
public Position getEnd() {
return end;
4. }
public int numRows() {
return maze.length;
}
public int numCols(int row) {
return maze[row].length;
}
public boolean validPosition(int row, int col) {
if (row >= 0 && row < maze.length) {
if (col >= 0 && col < maze[row].length) {
return true;
}
}
return false;
}
public boolean validPosition(Position pos) {
return validPosition(pos.getRow(), pos.getColumn());
}
public char getAt(Position pos) {
if (validPosition(pos)) {
return maze[pos.getRow()][pos.getColumn()];
} else {
String msg = String.format("The position given is not valid: %s", pos.toString());
throw new PositionNotValidException(msg);
}
}
public char getAt(int row, int column) {
if (validPosition(row, column)) {
return maze[row][column];
} else {
String msg = String.format("The row and column given is not valid: row %d col %d",
row, column);
throw new PositionNotValidException(msg);
}
}
public void setAt(Position pos, char c) {
5. if (validPosition(pos)) {
setAt(pos.getRow(), pos.getColumn(), c);
} else {
String msg = String.format("The position given is not valid: %s", pos.toString());
throw new PositionNotValidException(msg);
}
}
public void setAt(int row, int column, char c) {
if (validPosition(row, column)) {
maze[row][column] = c;
} else {
String msg = String.format("The row and column given is not valid: row %d col %d",
row, column);
throw new PositionNotValidException(msg);
}
}
public Maze clone() {
char clone[][] = new char[maze.length][];
for (int r = 0; r < maze.length; r++) {
clone[r] = new char[maze[r].length];
for (int c = 0; c < maze[r].length; c++) {
clone[r][c] = maze[r][c];
}
}
return new Maze(clone, start, end);
}
public String toString() {
StringBuffer sb = new StringBuffer();
for (int r = 0; r < maze.length; r++) {
sb.append(maze[r]);
sb.append("n");
}
return sb.toString();
}
}
POSITIONEXCEPTION
6. public class PositionNotValidException extends RuntimeException {
public PositionNotValidException() {
super();
}
public PositionNotValidException(String msg) {
super(msg);
}
private static final long serialVersionUID = 1L;
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
import maze.Maze;
import maze.MazeSolver;
import maze.Position;
/**
* Driver class to test students solution. The printed result should be a path
* from the start to the finish where the path is represented by asterisks.
*
* The Solvable and Unsolvable mazes were generated using
* https://www.dcode.fr/maze-generator
*
*
*/
public class Tester {
private static Maze loadMaze(String file) {
char maze[][] = null;
Position start = null, stop = null;
try {
Scanner filein = new Scanner(new File(file));
// Load Header
// First number is how many rows.
// Second and third number are the row and column to start at.
// Fourth and fifth number are the row and column to stop at.
7. int row = filein.nextInt();
start = new Position(filein.nextInt(), filein.nextInt());
stop = new Position(filein.nextInt(), filein.nextInt());
filein.nextLine();
// End Header
maze = new char[row][];
int currRow = 0;
while (filein.hasNextLine()) {
maze[currRow++] = filein.nextLine().replace("n", "").toCharArray();
}
filein.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
} catch (Exception e) {
System.out.println("Something bad happened. See exception message");
e.printStackTrace();
System.exit(2);
}
return new Maze(maze, start, stop);
}
private static void walkPath(Maze maze, Position traversal[]) {
Position previous = null;
for (Position p : traversal) {
if (maze.getAt(p) == ' ') {
maze.setAt(p, 'O');
} else if (maze.getAt(p) == 'O') {
System.out.println("MISTAKE");
System.out.println("There is a repeated position " + p);
System.exit(3);
} else {
System.out.println("MISTAKE");
System.out.println("There is an obstacle at position " + p);
System.out.println(maze);
System.exit(3);
}
8. // Check to make sure path is connected.
if (previous != null) {
int prevRow = previous.getRow();
int prevCol = previous.getColumn();
int curRow = p.getRow();
int curCol = p.getColumn();
boolean isPathConnected = false;
if (curRow - 1 == prevRow || curRow + 1 == prevRow) {
isPathConnected = curCol == prevCol;
} else if (curCol - 1 == prevCol || curCol + 1 == prevCol) {
isPathConnected = curRow == prevRow;
}
if (!isPathConnected) {
System.out.println("Path not connected");
System.out.println("From: " + previous);
System.out.println("To: " + p);
maze.setAt(previous, '?');
maze.setAt(p, '?');
System.out.println(maze);
System.exit(3);
}
}
previous = p;
}
}
private static void printDialog() {
System.out.println("Pick an option");
System.out.println("1: Solvable Maze");
System.out.println("2: Unsolvable Maze");
System.out.println("3: Huge Unsolvable Maze");
System.out.println("4: EXIT");
}
private static int getChoice(Scanner in) {
boolean exit = false;
int choice = 0;
do {
9. try {
System.out.print(">> ");
choice = Integer.parseInt(in.nextLine());
if (choice > 0 && choice <= 4) {
exit = true;
} else {
System.out.println("Choice must be either 1, 2, 3, or 4.");
}
} catch (InputMismatchException e) {
System.out.println("Choice must be either 1, 2, 3, or 4.");
}
} while (!exit);
return choice;
}
public static void main(String[] args) {
Maze clone;
Position solution[];
Scanner userIn = new Scanner(System.in);
boolean exit = false;
while (!exit) {
printDialog();
int choice = getChoice(userIn);
switch (choice) {
case 1:
/** Solvable Maze ***/
Maze solvableMaze = loadMaze("SolvableMaze");
clone = solvableMaze.clone();
System.out.println("Testing Solvable Maze");
System.out.println("Starts at " + clone.getStart());
System.out.println("Ends at " + clone.getEnd());
System.out.println(clone);
solution = MazeSolver.solve(clone);
if (solution.length != 0) {
walkPath(solvableMaze, solution);
System.out.println("nSOLUTION");
System.out.println(solvableMaze);
10. } else {
System.out.println("Null returned but maze is solvable.n");
}
break;
/*****************************************************************************
**/
case 2:
/** Unsolvable Maze **/
Maze unsolvableMaze = loadMaze("UnsolvableMaze");
clone = unsolvableMaze.clone();
System.out.println("Testing Unsolvable Maze");
System.out.println(clone);
solution = MazeSolver.solve(clone);
if (solution.length == 0) {
System.out.println("CORRECT");
System.out.println("This maze is unsolvable so an empty array should be
returned.");
} else {
System.out.println("INCORRECT");
System.out.println("This maze is unsolvable. An empty array should be returned.");
}
break;
/*****************************************************************************
***/
/** Huge Solvable Maze **/
case 3:
Maze hugeSolvableMaze = loadMaze("HugeSolvableMaze");
clone = hugeSolvableMaze.clone();
System.out.println("Testing Huge Solvable Maze");
System.out.println("Starts at " + clone.getStart());
System.out.println("Ends at " + clone.getEnd());
System.out.println(clone);
solution = MazeSolver.solve(clone);
if (solution.length != 0) {
walkPath(hugeSolvableMaze, solution);
System.out.println("nSOLUTION");