title: How do I solve a maze by sticking to the right wall in an algorithm using recursion?
body: <p>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.</p>
  
  <p>I'm not really sure how to progress past this. Thank you to anyone who can help.</p>
  
  <pre><code>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&lt;11; x++){
              for(int y=0; y&lt;11; y++){
                  System.out.print(field[x][y]);
              }
              System.out.println();
          }
          System.out.print("\n\n");
      }
  }
  </code></pre>
  
  <p>I've changed it to this, but it's all wacky:</p>
  
  <pre><code>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&amp;&amp;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&lt;12; x++){
          for(int y=0; y&lt;12; y++){
              System.out.print(field[x][y]);
          }
          System.out.println();
      }
      System.out.print("\n\n");
  }
  </code></pre>
  
  <p>}</p>