Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// maze.java public class Maze { static final int DOWN = 0; static final int RIGHT = 1; static final int UP = 2; static

// maze.java public class Maze { static final int DOWN = 0; static final int RIGHT = 1; static final int UP = 2; static final int LEFT = 3; static final int ROW_START = 2; static final int COLUMN_START = 0; static int move = 0; static char maze[][] = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }, { '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' }, { '.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' }, { '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' }, { '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.' }, { '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' }, { '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' }, { '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' }, { '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' }, { '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' }, { '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' }, { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } }; // method calls mazeTraversal with correct starting point and direction public void traverse() { boolean result = mazeTraversal( ROW_START, COLUMN_START ); if ( !result ) System.out.println( "Maze has no solution." ); } // end method traverse // traverse maze recursively public boolean mazeTraversal( int row, int column ) { // TO BE COMPLETED } // end method mazeTraversal // draw maze public void printMaze() { // for each space in maze for ( int row = 0; row  

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

MazeTest.java

public class MazeTest { public static void main( String args[] ) { Maze maze = new Maze(); maze.traverse(); } // end main } // end class MazeTestimage text in transcribed
(Maze Traversal Using Recursive Backtracking) The grid of #s and dots (.) in Figure l is a two-dimensional array representation of a maze. The #s represent the walls of the maze, and the dots represent locations in the possible paths through the maze. A move can be made only to a location in the array that contains a dot. # # # # # # # # # # # # ## . . . . . . # .. # . # . # # # #-# Write a recursive method (mazeTraversal) to walk through mazes like the one in the # # # # . # . # . # # Figure. The method should receive as arguments a 12-by-12 character array representing the maze and the current location in the maze (the first time this method is called, the current location should be the entry point of the maze). As mazeTraversal attempts to locate the exit, it should place the character x in each square in the path. There's a simple algorithm for walking through a maze that guarantees finding the exit (assuming there's an exit). If there's no exit, you'll arrive at # . . . . . . # . . . the starting location again. The algorithm is as follows: From the current location in the # # # # # # # # # # # # maze, try to move one space in any of the possible directions (down, right, up or left). If Figure 1. Two-dimensional it's possible to move in at least one direction, call mazeTraversal recursively, passing array representation of a the new spot on the maze as the current spot. If it's not possible to go in any direction,maze. "back up" to a previous location in the maze and try a new direction for that location (this is an example of recursive backtracking). Program the method to display the maze after each move so the user can watch as the maze is solved. The final output of the maze should display only the path needed to solve the maze -if going in a particular direction results in a dead end, the x's going in that direction should not be displayed. [Hint: To display only the final path, it may be helpful to mark off spots that result in a dead end with another character (such as '0').] Use the java files provided by the instructor. The java file "Maze.java" contains a skeleton class definition of Maze, use this skeleton to complete the mazeTraversal method

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions