0

In the proceeding 2d array, the #'s represent the walls of the maze, and the dots represent squares in the possible paths through the maze. Moves can be made only to a location in the array that contains a dot... I need a recursive method mazeTraverse to "walk" through the maze. It should receive the array and the starting location of the maze as arguments. As it attempts to locate the exit from the maze, it should place the character 'X' in each square in the path. The method should display the maze after each move so the user can watch as the maze is solved.

I'm not really sure how to progress past this. Thank you to anyone who can help.

public class BonusMaze {
    public static void main(String args[]){
        char field[][]={
            {'#','#','#','#','#','#','#','#','#','#','#','#'},
            {'#','•','•','•','#','•','•','•','•','•','•','#',},
            {'•','•','#','•','#','•','#','#','#','#','•','#',},
            {'#','#','#','•','#','•','•','•','•','#','•','#',},
            {'#','•','•','•','•','#','#','#','•','#','•','•',},
            {'#','#','#','#','•','#','•','#','•','#','•','#',},
            {'#','•','•','#','•','#','•','#','•','#','•','#',},
            {'#','#','•','#','•','#','•','#','•','#','•','#',},
            {'#','•','•','•','•','•','•','•','•','#','•','#',},
            {'#','#','#','#','#','#','•','#','#','#','•','#',},
            {'#','•','•','•','•','•','•','#','•','•','•','#',},
            {'#','#','#','#','#','#','#','#','#','#','#','#'},
        };
        printField(field);

        mazeTraverse(field,2,0);

    }
    public static void mazeTraverse(char[][] field, int x, int y){
        field[2][0]='X';
        if(field[x+1][y]=='•'){
            field[x+1][y]='X';
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x-1][y]=='•'){
            field[x-1][y]='X';
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x][y+1]=='•'){
            field[x][y+1]='X';
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x][y-1]=='•'){
            field[x][y-1]='X';
            printField(field);
            mazeTraverse(field,x+1,y);
        }
    }
    public static void printField(char[][] field){
        for(int x=0; x<11; x++){
            for(int y=0; y<11; y++){
                System.out.print(field[x][y]);
            }
            System.out.println();
        }
        System.out.print("\n\n");
    }
}

I've changed it to this, but it's all wacky:

public class BonusMaze {
public static boolean east=true, north=false, south=false, west=false;
/*
 east=false; 
 north=false; 
 south=false;
 west=false;
 */
public static void main(String args[]){
    char field[][]={
        {'#','#','#','#','#','#','#','#','#','#','#','#'},
        {'#','•','•','•','#','•','•','•','•','•','•','#',},
        {'•','•','#','•','#','•','#','#','#','#','•','#',},
        {'#','#','#','•','#','•','•','•','•','#','•','#',},
        {'#','•','•','•','•','#','#','#','•','#','•','•',},
        {'#','#','#','#','•','#','•','#','•','#','•','#',},
        {'#','•','•','#','•','#','•','#','•','#','•','#',},
        {'#','#','•','#','•','#','•','#','•','#','•','#',},
        {'#','•','•','•','•','•','•','•','•','#','•','#',},
        {'#','#','#','#','#','#','•','#','#','#','•','#',},
        {'#','•','•','•','•','•','•','#','•','•','•','#',},
        {'#','#','#','#','#','#','#','#','#','#','#','#'},
    };
    printField(field);
    field[2][0]='X';
    mazeTraverse(field,2,0);

}
public static void mazeTraverse(char[][] field, int x, int y){
    if(x==2&&y==0){
        field[2][1]='X';
        mazeTraverse(field,2,1);
        printField(field);
    }
    if(east){
        if(field[x][y+1]=='•' || field[x+1][y]=='X'){
            field[x][y+1]='X';
        }
        if(field[x+1][y+1]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            south=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x][y+2]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            east=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x-1][y+1]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            north=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x][y+1]=='#'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            west=true;
            printField(field);
            mazeTraverse(field,x-1,y);
        }
    }
    else if(west){
        if(field[x-1][y]=='•' || field[x-1][y]=='X'){
            field[x-1][y]='X';
        }
        if(field[x+1][y+1]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            south=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x][y+2]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            east=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x-1][y+1]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            north=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x][y+1]=='#'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            west=true;
            printField(field);
            mazeTraverse(field,x-1,y);
        }
    }
    else if(north){
        if(field[x][y+1]=='•' || field[x][y+1]=='X'){
            field[x][y+1]='X';
        }
        if(field[x+1][y+1]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            south=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x][y+2]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            east=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x-1][y+1]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            north=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x][y+1]=='#'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            west=true;
            printField(field);
            mazeTraverse(field,x-1,y);
        }
    }
    else if(south){
        if(field[x][y-1]=='•' || field[x][y-1]=='X'){
            field[x][y-1]='X';
        }
        if(field[x+1][y+1]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            south=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x][y+2]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            east=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x-1][y+1]=='•'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            north=true;
            printField(field);
            mazeTraverse(field,x+1,y);
        }
        else if(field[x][y+1]=='#'){
            east=false; 
            north=false; 
            south=false;
            west=false;
            west=true;
            printField(field);
            mazeTraverse(field,x-1,y);
        }
    }


    /*if(field[x+1][y]=='•'){
        field[x+1][y]='X';
        printField(field);
        mazeTraverse(field,x+1,y);
    }
    else if(field[x-1][y]=='•'){
        field[x-1][y]='X';
        printField(field);
        mazeTraverse(field,x+1,y);
    }
    else if(field[x][y+1]=='•'){
        field[x][y+1]='X';
        printField(field);
        mazeTraverse(field,x+1,y);
    }
    else if(field[x][y-1]=='•'){
        field[x][y-1]='X';
        printField(field);
        mazeTraverse(field,x+1,y);
    }*/
}
public static void printField(char[][] field){
    for(int x=0; x<12; x++){
        for(int y=0; y<12; y++){
            System.out.print(field[x][y]);
        }
        System.out.println();
    }
    System.out.print("\n\n");
}

}

user2888050
  • 1
  • 1
  • 2
  • I'd avoid all the "sketchy" nonstandard characters as • in code due to non-portability and other risks of error. – nanofarad Oct 16 '13 at 22:00
  • 2
    If you need to keep walking along the "right" wall, then don't you need to keep track of what direction you're walking? The coordinates to your "right" will depend on your direction. E.g. if you're at (row,column) and you're going North, right is (row,column+1); if you're going West, right is (row-1,column), etc. – ajb Oct 16 '13 at 22:05
  • Going recursion is right, but doing like this won't exactly get you your solution. you need a path finding algorithm like A* to effectively help you solve your maze. Take a look at http://stackoverflow.com/questions/17128965/what-is-a-good-2d-grid-based-path-finding-algorithm – mauris Oct 16 '13 at 22:05
  • Hi friend. Did my answer helped you with your problem? – Jim Blum Jan 24 '14 at 18:07

2 Answers2

0

As you need recursion, you probably need DFS (Depth-First Search). From wikipedia

 procedure DFS(G,v):
2      label v as discovered
3      for all edges e in G.adjacentEdges(v) do
4          if edge e is unexplored then
5              w ← G.adjacentVertex(v,e)
6              if vertex w is unexplored then
7                  label e as a discovered edge
8                  recursively call DFS(G,w)
9              else
10                 label e as a back edge
11      label v as explored

The edges of your programs are the four (at maximum) neighbours of the cell you are examining right now. If you find the exit, you are marking the cell, and return back. This is the general idea. You can now find out the details for yourself.

SOS (watch out the borders of your table) I hope I helped

Jim Blum
  • 2,656
  • 5
  • 28
  • 37
  • If a maze is "simply connected", I don't think you need anything that sophisticated; simply walking along a maze and keeping your right hand on the wall (or the algorithmic equivalent) will work, although you'll be doing a lot of backtracking into dead ends. See [Maze solving algorithms](http://en.wikipedia.org/wiki/Maze_solving_algorithm), Wall follower section. I think the maze in the original question is simply connected, but I can't tell for sure since I don't know whether a wall or a path can be connected diagonally over the corners of cells. – ajb Oct 16 '13 at 23:37
0

I used the following algorithm. If not already at the exit, it tries to go right, then tries to go forward, and then tries to go left. If any of these return a path to the end, then that is returned. If they return null, then the next direction is attempted.

By always trying to go right first, it is simulating the strategy of sticking to the right wall

traverse(Position p, Direction d, String pathSoFar)
   if atEndPosition(p)
     return pathSoFar + p;

   if canTravelRight(p, d)
      result = traverse(poitionToRight, directionToRight, pathSoFar + p)
      if result return result
   if canTravelForward(p, d)
      result = traverse(positionForward, d, pathSoFar + p)
      if result return result
   if canTravelLeft(p, d)
      result = traverse(positionToLeft, directionToLeft, pathSoFar + p)
   return result

I used a really simple logic to find the next direction to the right, if current is north then next is east etc. The logic for moving was equally simple if moving east, new x position is oldx + 1 and new y position is the old y position. I didn't do any bounds checking when calculating the position and instead just tried to see if the new position is on a path. If the new position is a wall or if an ArrayIndexOutOfBoundsException then canTravelxxx returned false

GregA100k
  • 1,385
  • 1
  • 11
  • 16