// Ryan Kightlinger // November 7, 2002 // This Program will solve a maze // Get the necessary Java library methods import java.awt.*; import java.applet.*; import java.util.*; public class Maze extends Applet { // defines the initial starting position in the maze int mazeXcoord = 50; int mazeYcoord = 50; // the maze ends at the following coordinates int endXcoord = 500; int endYcoord = 500; int wall; // defines where the walls are at (maybe create an array) ???????? // ================================================= // X-Axis // // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // x x ..... x // Enter .......... x . . x // xxxxxxxxxxx . x xxxxxx . . x // x x . x x . x . x // x x x . x x . x . x // x x x . x x . x . x Y-Axis // x x x . xxxx . x . x // x xxxxx ............. x . x // x xxxxxxxx . x // x xxxxxxxXXXxxxxxxxxxxx . x // x x x ............ x // x x x . xxxxxxxxxxxxxx // x x ..............EXIT (500,500) // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcxxx // // ================================================= // PERFORM THESE STEPS TO SOLVE THIS SIMPLE MAZE // ------------------------------------------------------------------------------------- // // 1.) Start out by calling the goEast method (keep moving east until you hit a wall) // // // 2.) If you hit a wall, then try calling the goSouth method (Perform function 1 time) // - If Successful then try Repeating Step 1 again // // // 3.) If you can't go east and can't go south then try moving north (Unless you just // came from the north (by calling the goSouth method) // // - Keep moving north until you can't // - If you can't move north then repeat step 1 again // // // 4.) If you can't go east and can't go south and just came from the north // // - try moving west (keep going west until you can't) // - if you can't move west then try to call the goSouth Method (2 times) // - Repeat Step 1 again // // ------------------------------------------------------------------------------------ // calls the main methods to complete the maze while (mazeXcoord < endXcoord && mazeYCoord < endYcoord) { // start out by calling the goEast method, which will then call the other methods goEast(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall); } x y maze path : 20, 20 wall at : 32, 9 // -------------------------------------------------------------- // THE GO NORTH METHOD // -------------------------------------------------------------- goNorth(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) { // if the square to the north is clear, inside the maze if ( (mazeYCoord - 10) > wall ) { // move to the north mazeYcoord = mazeYcoord - 10; // mark the square as part of the path // if at exit if (mazeXcoord > endXcoord && mazeYCoord > endYcoord) { g.drawString("You Are Out",20,20); } return(mazeXcoord, mazeYcoord); } else { // if you can't go north than try to move west if ( (mazeYCoord - 10) < wall) { goWest(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) // if you can't go west try to move east if ( (mazeXcoord - 10) < wall ) { goEast(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) // if you can't go east than backtrack to the south if ( (mazeXcoord + 10) > wall ) { Mark square visited backtrack south } // end if } // end if } // end if } // end else statement } // try going north again goNorth(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall); } x y maze path : 20, 20 wall at : 20, 39 // -------------------------------------------------------------- // THE GO SOUTH METHOD // -------------------------------------------------------------- goSouth(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) { // if the square to the south is clear, inside the maze if ( (mazeYCoord + 10) < wall ) { // move to the south mazeYcoord = mazeYcoord + 10; // mark the square as part of the path // if at exit if (mazeXcoord > endXcoord && mazeYCoord > endYcoord) { g.drawString("You Are Out",20,20); } return(mazeXcoord, mazeYcoord); } else { // if you can't go south than try to move west if ( (mazeYCoord + 10) > wall) { goWest(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) // if you can't go west try to move east if ( (mazeXcoord - 10) < wall ) { goEast(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) // if you can't go east than backtrack to the north if ( (mazeXcoord + 10) > wall ) { Mark square visited backtrack north } // end if } // end if } // end if } // end else statement } // try going south again goSouth(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall); } // -------------------------------------------------------------- // THE GO WEST METHOD // -------------------------------------------------------------- goWest(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) { // if the square to the west is clear, inside the maze if ( (mazeXCoord - 10) > wall ) { // move to the south mazeXcoord = mazeXcoord - 10; // mark the square as part of the path // if at exit if (mazeXcoord > endXcoord && mazeYCoord > endYcoord) { g.drawString("You Are Out",20,20); } return(mazeXcoord, mazeYcoord); } else { // if you can't go west try to move south if ( (mazeXCoord - 10) < wall) { goSouth(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) // if you can't go south try to move north if ( (mazeYCoord + 10) > wall ) { goEast(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) // if you can't go north than backtrack to the east if ( (mazeYCoord - 10) < wall ) { Mark square visited backtrack east } // end if } // end if } // end if } // end else statement } // try going west again goWest(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall); } // -------------------------------------------------------------- // THE GO EAST METHOD // -------------------------------------------------------------- goEast(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) { // if the square to the east is clear, inside the maze if ( (mazeXCoord + 10) < wall ) { // move to the east mazeXcoord = mazeYcoord + 10; // mark the square as part of the path // if at exit if (mazeXcoord > endXcoord && mazeYCoord > endYcoord) { g.drawString("You Are Out",20,20); } return(mazeXcoord, mazeYcoord); } else { // if you can't go east try to move south if ( (mazeXCoord - 10) < wall) { goSouth(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) // if you can't go south try to move north if ( (mazeYCoord + 10) > wall ) { goEast(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall) // if you can't go north than backtrack to the west if ( (mazeYCoord - 10) < wall ) { Mark square visited backtrack west } // end if } // end if } // end if } // end else statement } // try going east again goEast(mazeXcoord, mazeYcoord, endXcoord, endYcoord, wall); }