Requirements :
Use Java
Do Not Use Recursion
Do Not Use Array List Use The Methods Provided
Use What Is Provided Only Make And Use The Helper Methods The Instructions Say You Can
Only Provide The Answer Code
Thank You ! I'm Having The Hardest Time Understanding This
Maze Solving with Stacks
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.
Maze Solving with Stacks
CSC 385
What you need to do:
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 .
Requirements Use JavaDo Not Use RecursionDo Not Use Array Lis.pdf
1. Requirements :
Use Java
Do Not Use Recursion
Do Not Use Array List Use The Methods Provided
Use What Is Provided Only Make And Use The Helper Methods The Instructions Say You Can
Only Provide The Answer Code
Thank You ! I'm Having The Hardest Time Understanding This
Maze Solving with Stacks
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
2. 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.
Maze Solving with Stacks
CSC 385
What you need to do:
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.
You only need to implement the solve method in the MazeSolver.java class. This method
receives
the maze to solve as a parameter and returns an array of Position objects which stores a solution
to
the maze if such solution exists; otherwise, it returns an empty array.
Note: There might be more than one possible solution to a maze, depending on the ordering in
which the neighboring cells are visited at each location. You can do whatever order you want if
the
maze is solved. For instance,
First move left, if the left neighbor is not clear, then move right, if the right neighbor is not clear,
then move up, if it is not clear, then move down.
3. You may create any additional helper methods to aid you. For instance, you may make a method
to convert the stack to a necessary array. Just remember, when you grab the information from a
stack you are getting in the reverse order you push them.
You must solve the problem using a Stack. You may use the built-in stack library but use of any
other data structure is prohibited apart from constructing the final Position array result.
Recursion is also not allowed.
What you need to turn in:
You need to submit the MazeSolver.java file containing your implementation of the solve
method.
Maze Solving with Stacks
CSC 385
Rubric
Stack is utilized 75
Iterative solution 5
Returns correct solutions to solvable mazes 10
Returns correct solutions to unsolvable mazes 10
Additional Notes and Helpful Hints
First hint I have is to not be afraid of creating additional variables to store information. For
instance, you
must move up, down, left, and right (or north, south, west, and east). Creating Position objects to
represent these movements is perfectly acceptable. You will also want to create an object that is
the
walker of the maze. This walker keeps the current position in the maze.
Other than push, pop, and peek on the Stack class, isEmpty is also a useful method.
The maze class contains a bunch of methods that is meant to help you. Read what the methods
do and
decide which ones you want to use for your solution. Read the code given to you FIRST before
starting
to write your code so you can utilize it. It is there to make your life easier, not harder.
Try to translate what you want to do in terms of the stack operations. For instance, if you find
yourself
at a dead end then you want to move back to a previous position. This means you will need to
first pop
the top position, which contains the spot located at the dead end, then peek to see where you
need to
4. be.
Your stack should be declared and initialized as such:
Stack __Give it a name__ = new Stack();
where you can name it whatever you want.
Moving in a two-dimensional array requires the following math:
Move up (North) Row + 1
Move down (South) Row - 1
Move left (West) Column - 1
Move right (East) Column + 1
The Provided Code : Plus The Method I Need Solved !
Position.java
public class Position {
/***************************
* Attributes
****************************/
private int row;
private int column;
/***************************
* Constructor
*
* @param row
* @param column
***************************/
public Position(int row, int column) {
this.row = row;
this.column = column;
}
/**************************
* Checks two positions for equality. Two positions are equal if the have the
* same row and column numbers.
*************************/
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
5. if (!(obj instanceof Position)) {
return false;
}
Position otherPosition = (Position) obj;
return (otherPosition.row == row && otherPosition.column == column);
}
public String toString() {
return "row:" + row + " column:" + column;
}
/**************************
* Getter and Setter methods
*
* @return
*/
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
}
Maze.java
public class Maze {
/**
* Two dimensional array to represent a maze
*/
private final char[][] maze;
private final Position start, end;
/**
* Constructor initializing the maze array
*
* @param maze
*/
public Maze(char[][] maze, Position start, Position end) {
this.maze = maze;
if (validPosition(start)) {
6. 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);
}
}
/**
* Returns the start position object.
*
* @return The start position.
*/
public Position getStart() {
return start;
}
/**
* Returns the stop position object.
*
* @return
*/
public Position getEnd() {
return end;
}
/**
* Returns the number of rows the maze has.
*
* @return The number of rows.
*/
public int numRows() {
return maze.length;
}
/**
* Returns the number of columns the row of the maze has.
7. *
* @param row The row to get the length of.
* @return The length of the row in the maze.
*/
public int numCols(int row) {
return maze[row].length;
}
/**
* Tests if row and column are valid within the two dimensional array. Will
* return true iff row is between 0 and maze.length-1 and column is between 0
* and maze[row].length - 1.
*
* @param row Represents the array to retrieve from maze.
* @param col Represents the character to retrieve from the array at maze[row].
* @return True iff the row and column are valid indices of maze otherwise
* false.
*/
public boolean validPosition(int row, int col) {
if (row >= 0 && row < maze.length) {
if (col >= 0 && col < maze[row].length) {
return true;
}
}
return false;
}
/**
* Tests if the row and column stored in pos are valid. Drops to the
* validPosition(int row, int col) method.
*
* @param pos The position to test.
* @return True iff pos is valid inside the maze otherwise false.
*/
public boolean validPosition(Position pos) {
return validPosition(pos.getRow(), pos.getColumn());
}
/**
8. * Returns the character at pos.
*
* @param pos The position to retrieve the character at.
* @return The character at maze[pos.row][pos.column]
* @throws PositionNotValidException if the position is now within the maze.
*/
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);
}
}
/**
* Retrieves the character at maze[row][column].
*
* @param row The row to retrieve the character from.
* @param column The column to retrieve the character from.
* @return The character at position row, column
* @throws PositionNotValidException if the row or column are not within bounds.
*/
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);
}
}
/**
* Sets a character at a specified position.
*
* @param pos The position to set the character at.
* @param c The character to set at position.
9. * @throws PositionNotValidException if the position passed in is not valid
* within the maze.
*/
public void setAt(Position pos, char c) {
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);
}
}
/**
* Sets a character at a specified row and column.
*
* @param pos The position to set the character at.
* @param c The character to set at position.
* @throws PositionNotValidException if the position passed in is not valid
* within the maze.
*/
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);
}
}
/*** METHODS NOT NEEDED TO HELP SOLVE THE PROBLEM. ****/
/*** YOU DO NOT NEED TO USE THESE METHODS AS THEY ****/
/*** ARE USED IN THE MAIN CLASS. ****/
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++) {
10. 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();
}
}
MazeSolver.java - ONE THAT I NEED A HELP WITH!!
import java.util.Stack;
/**
* Notes:
*
* The start and end position are contained within the maze object. Use the
* getStart() and getEnd() methods.
*
*/
public class MazeSolver {
/**
* You need to implement this method
*
* @param maze: The maze to be solved.
* @return An array of Position which stores a solution to the maze. If a
* solution is not found a null value should be returned.
*/
public static Position[] solve(Maze maze) {
return null;
}
}