title: Java learning maze solver
body: <p>I have been working on some code to guide a 'robot' through a maze with multiple dead ends and 1 correct path to the goal like this:</p>
  
  <p><img src="https://i.stack.imgur.com/kp7QY.png" alt=""></p>
  
  <p>I have used a stack to record the direction the robot is facing the first time it reaches a square with 3 or 4 possible exits and if all adjacent squares have already been visited, used pop() to make the robot return from the direction it first came from (opposite to direction arrived). At the end of the run the stack contains the direction arrived at all squares on the route to the target. Following the opposite directions of the stack would take the robot from the goal back to the start point. I am struggling to find out how to use this stack so that on the next run the robot will take the optimal path to reach the goal.</p>
  
  <p>Some of my code:</p>
  
  <pre><code>private int pollRun = 0; // Incremented after each pass
  private int explorerMode; // 1 = explore, 0 = backtrack
  
  public void exploreControl(IRobot robot) {
  
    byte exits = nonwallExits(robot);
    int direction;
  
    switch (exits) { //passes control to respective method
      case 1: direction = deadEnd(robot);   break;
      case 2: direction = corridor(robot); break;
      case 3: direction = junction(robot); break;
      default: direction = crossroads(robot); break;
    }
  
    if (exits == 1) {explorerMode = 0;}
  
    robot.face(direction); 
  
    pollRun++;
  
  }
  
  public void backtrackControl(IRobot robot) {
  
    byte exits = nonwallExits(robot);
    int direction = IRobot.CENTRE;
  
    switch (exits) { //passes control to respective method
      case 1: direction = deadEnd(robot);   break;
      case 2: direction = corridor(robot); break;
      default: direction = junction(robot); break; // do nothing
    }
  
    if (exits &gt; 2) {
      if (passageExits(robot) &gt; 0){
        exploreControl(robot);
        explorerMode = 1;
        pollRun++;
        return;
      } else {
        robot.setHeading(st.pop());
        robot.face(IRobot.BEHIND);
        pollRun++;
        return;
      }
  
    }
  
      robot.face(direction); 
  
    pollRun++;
  
  }
  
  public void optimal(IRobot robot) {
  
    byte exits = nonwallExits(robot);
    int direction;
    int heading;
  
    for(int i = 0; i &lt; st.size(); i++) {
      stNew.push(st.pop());
    }
  
    if (exits &lt; 3) {
  
      switch (exits) { //passes control to respective method
        case 1: direction = deadEnd(robot);   break;
        default: direction = corridor(robot); break;
      }
  
      robot.face(direction);
  
    } else {
      robot.setHeading(stNew.pop());
    }
  
  }
  
  public void controlRobot(IRobot robot) {
  
    if ((robot.getRuns() == 0) &amp;&amp; (pollRun == 0)) {
      robotData = new RobotData(); //reset the data store
      explorerMode = 1;
    }
  
    if (robot.getRuns() = 1) {
      optimal(robot);
      return;
    }
  
    if (robot.getRuns() &lt;= 0 &amp;&amp; (nonwallExits(robot) &gt;= 3)
        &amp;&amp; (beenbeforeExits(robot) &lt;= 0)) {
      st.push(robot.getHeading());
    }
  
    if (explorerMode == 1) {
      exploreControl(robot);
    } else {backtrackControl(robot);}
  
  }
  </code></pre>
  
  <p>The optimal method shows my attempt at solving it, however all it does is cause the robot to head straight at every junction</p>
  
  <p>For example this maze,</p>
  
  <p><img src="https://i.stack.imgur.com/T4Iro.png" alt="enter image description here"></p>
  
  <p>Would leave me with the stack: EAST, EAST, SOUTH, SOUTH, EAST, SOUTH, SOUTH, EAST, EAST, SOUTH, SOUTH, EAST, EAST, EAST, SOUTH, EAST, SOUTH</p>