title: Java Maze Solver
body: <p>I'm having difficulty trying to turn a algorithm that was hinted to us into usable code.  We are given a Direction enum that has the 8 coordinates (N, NE, NW, S, SE. SW. E, W) along with the exit HERE.  </p>
  
  <p>This is the hinted algorithm:</p>
  
  <pre><code>getPathToExit(row, col):
    if (row, col) is outside of the map:
      return an empty list
    else if (row, col) is an obstacle:
      return an empty list
    else if (row, col) is marked as visited or as deadend:
     return an emtpy list
    else if (row, col) is the exit:
      //optional: mark exit as visited
     return a list containing Direction.HERE
    else:
     //try to find a path from current square to exit:
      mark current square as visited (that is, part of path)
        for each neighbor of current square:
          path = path from neighbor to exit
            if path is not empty:
               add (direction to neighbor) to start of path
                 return path
     //after for loop: no path exists from this square to exit
     mark current square as deadend
       return empty list
  </code></pre>
  
  <p>This is the code I have been working on for a while:</p>
  
  <pre><code>public java.util.ArrayList&lt;Direction&gt; getPathToExit(){
  
  for (int x=0; x&lt;map.length; x++){
      for (int y=0; y&lt;map[x].length; y++){
          if (map[x][y]=='S'){
              this.startRow=x;
              this.startCol=y;
          }
      }
  }
  System.out.println("start "+startRow+", "+startCol);
  return getPathToExit(this.startRow, this.startCol);
  }
  
  private java.util.ArrayList&lt;Direction&gt; getPathToExit(int row, int col){
  
    Direction [] dirs = Direction.values();
    ArrayList&lt;Direction&gt; path = new ArrayList&lt;Direction&gt;();
  
  getPathToExit(row, col);
  
      if (row &lt; 0 || col &lt; 0 || row &gt; map.length || col &gt; map[row].length){
          return null;
      }
      else if (map[row][col] != ' '){
          return null;
      }
      else if (map[row][col] == 'E'){
          path.add(Direction.HERE);
          return path;
      }
      else {
          for (int x=0; x&lt;dirs.length-1; x++){
              int nextRow = row + dirs[x].getRowModifier();
              int nextCol = col + dirs[x].getColModifier();
              path = getPathToExit(nextRow, nextCol);
          }
      }
  return path;
  }
  </code></pre>
  
  <p>This is the enum class:</p>
  
  <pre><code>public enum Direction {
     N, NE, E, SE, S, SW, W, NW, HERE;
  
   /**
   * Returns the X/column change on the screen that is associated with
   * this direction: -1 for W, 0 for N/S, and +1 for E.
   */
    public int getColModifier() {
      int mod;
      switch (this) {
       case NW:
       case W:
       case SW:
       mod = -1;
      break;
    case NE:
    case E:
    case SE:
      mod = +1;
      break;
    default:
      mod = 0;
      break;
    }
  return mod;
  }
  
  /**
   * Returns the Y/row change on the screen that is associated with
   * this direction: -1 for N, 0 for E/W, and +1 for south.
   */
  public int getRowModifier() {
   int mod;
   switch (this) {
     case N:
     case NE:
     case NW:
      mod = -1;
      break;
    case S:
    case SE:
    case SW:
      mod = +1;
      break;
    default:
      mod = 0;
      break;
    }
  return mod;
  }
  
  /** As {@link #getColModifier()} */
  public int getXModifier() {
   return this.getColModifier();
  }
  
   /** As {@link #getRowModifier()} */
  public int getYModifier() {
    return this.getRowModifier();
  }
  
  /**
   * Returns the direction that is the opposite of this one.
   * For example, &lt;code&gt;Direction.NE.reverse() == Direction.SW&lt;/code&gt;.
   * (The opposite of HERE is still HERE though.)
   */
  public Direction reverse() {
    if (this == HERE) {
      return this;
    }else {
      int reversed = (this.ordinal() + 4) % 8;
     Direction[] dirs = Direction.values();
      return dirs[reversed];
    }
  }
  
  }
  </code></pre>
  
  <p>Thanks in advance.</p>