Access over 35 million academic & study documents

3Write a Maze class with a search method that uses recursion to tr

Content type
User Generated
Rating
Showing Page:
1/12
3
Write a Maze class with a search method that uses
recursion to traverses a maze by following
space-character passageways through a maze whose walls
are X characters. The program driver
below specifies a layout with an array of strings one
string for each row in the two-dimensional
maze.
/*************************************************************
* MazeDriver.java
* Dean & Dean
*
* This drives a maze traverse.
*************************************************************/
public class MazeDriver
{
public static void main(String[] args)
{
String[] mazeStrings = new String[]
{"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"XXXXXXXXXXXX XX",
"XXXX XXXXXXXXXXXX XXXX",
"XXXX XXXXX XXXXXXX XXX",
"XXXX XXXXX XXXXXXX XXXXXXXXXXX",
"XXXX XXXXXXXXXXXXXXXXXXX",
"XXXX XXXXX XXXXX",
" XXXXX XXXXXXXXXXXXX XXXXX",
"X XXXXXXXX XXXXXXXXXXXXX XXXXX",
"X XXXXXXXXXXXXX XXX XXX",
"X XXXXXXXXXXXXXX XXXXX XXX",
"XXX XXXXXXXXXXXXXX XXXXX XXX",
"XXX XXXXX XXXXXXX",
"XXX XXXXXXXX XXXXXXXXX XXXXXXX",
"XXX XXXXXXXX XXXXXXXXX XXXXXXX",
"XXX XXXX XXXXXXXXX ",

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/12
"XXX XXX XXXX XXXXXXXXX XXXXXXX",
"XXX XXXX XXXXXXXXX XXXXXXX",
"XXXXXXXXXXXX XXXXXXX",
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"};
Maze maze = new Maze(mazeStrings);
maze.search(7, 0, 0.0); // start row, start col, start angle
} // end main
} // end MazeDriver class
This particular maze is 30 columns wide and 20 rows high.
The Xs are boundaries and the clear
spaces are passages. There is an opening on the left side
at row 7 and column 0, and there is another
opening on the right at row 15 and column 29. There are
two islands created by cross linking
passageways, and there is one open area that is three
columns wide.
To traverse the maze, specify a starting row and column
and an initial heading angle that is not
into a wall. For example, you might start with row 7,
column 0, and angle 0 to indicate entrance at the
left-side opening. Or you might start with row 15, column
29, and angle 180 to indicate entrance at
the right-side opening. An angel of +90 is up and an angle
of 90 is down. You could make the
traverse by always following the wall on the left or always
following the wall on the right. Our
solution always follows the wall on the left.
Your search method should contain a for loop that steps
through four angles. To follow left
walls, those four angles are: 90 degrees to the left of
straight ahead, straight ahead, 90 degrees to the
right of straight ahead, and straight behind in that order. If
the next cell in the currently selected
direction is outside the array limits, youve found an exit,
so print the exit location and return true.
Otherwise, if the next cell in the selected direction is not

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/12

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 12 pages?
Access Now
Unformatted Attachment Preview
3 Write a Maze class with a search method that uses recursion to traverses a maze by following space-character passageways through a maze whose walls are ‘X’ characters. The program driver below specifies a layout with an array of strings – one string for each row in the two-dimensional maze. /************************************************************* * MazeDriver.java * Dean & Dean * * This drives a maze traverse. *************************************************************/ public class MazeDriver { public static void main(String[] args) { String[] mazeStrings = new String[] {"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXX XX", "XXXX XXXXXXXXXXXX XXXX", "XXXX XXXXX XXXXXXX XXX", "XXXX XXXXX XXXXXXX XXXXXXXXXXX", "XXXX XXXXXXXXXXXXXXXXXXX", "XXXX XXXXX XXXXX", " XXXXX XXXXXXXXXXXXX XXXXX", "X XXXXXXXX XXXXXXXXXXXXX XXXXX", "X XXXXXXXXXXXXX XXX XXX", "X XXXXXXXXXXXXXX XXXXX XXX", "XXX XXXXXXXXXXXXXX XXXXX XXX", "XXX XXXXX XXXXXXX", "XXX XXXXXXXX XXXXXXXXX XXXXXXX", "XXX XXXXXXXX XXXXXXXXX XXXXXXX", "XXX XXXX XXXXXXXXX ", "XXX XXX XXXX XXXXXXXXX XXXXXXX", "XXX XXXX XXXXXXXXX XXXXXXX", "XXXXXXXXXXXX XXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}; Maze maze = new Maze(mazeStrings); maze.search(7, 0, 0.0); // start row, start col, start angle } // end main } // end MazeD ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Just the thing I needed, saved me a lot of time.

Studypool
4.7
Indeed
4.5
Sitejabber
4.4