import java.util.*;

public class RepairDroid
{
    public static final String INITIAL_INPUT = Integer.toString(DroidMovement.NORTH);

    public RepairDroid (Vector<String> instructions, boolean debug)
    {
        _debug = debug;
        _theComputer = new Intcode(instructions, INITIAL_INPUT, _debug);
        _currentLocation = new Coordinate(0, 0);  // starting location
        _theMap = new Maze();

        _theMap.addContent(_currentLocation, TileId.TRAVERSE);
    }

    public final int moveToOxygenStation ()
    {
        int numberOfSteps = 0;

        // create a map first!

        explore(_currentLocation);

        return numberOfSteps;
    }

    public void printGrid ()
    {
        System.out.println(_theMap);
    }

    /*
     * If we run into a wall then try a different direction.
     * If we can't move other than backwards then do that.
     * Don't move into areas we've already been.
     */
    
    private boolean explore (Coordinate from)
    {
        while (!_theComputer.hasHalted())
        {
            Coordinate[] moves = getNextPositions(from);  // get all possible moves (Coordinates)

            System.out.println("\n"+_theMap);

            /*
             * We search N, E, S and then W.
             */

            if (!tryToMove(String.valueOf(DroidMovement.NORTH), from, moves[0]))
            {
                System.out.println("\n"+_theMap);

                if (!tryToMove(String.valueOf(DroidMovement.EAST), from, moves[1]))
                {
                    System.out.println("\n"+_theMap);

                    if (!tryToMove(String.valueOf(DroidMovement.SOUTH), from, moves[2]))
                    {
                        System.out.println("\n"+_theMap);

                        return tryToMove(String.valueOf(DroidMovement.WEST), from, moves[3]);
                    }
                }
            }
        }

        return false;
    }

    private boolean tryToMove (String direction, Coordinate from, Coordinate to)
    {                
        System.out.println("**Trying to move from: "+from+" to "+to+" with direction "+DroidMovement.toString(direction));

        if (_theMap.isExplored(to))
        {
            System.out.println("**Been there already.");

            return false;
        }

        _theComputer.setInput(direction);

        _theComputer.executeUntilInput();

        System.out.println("**Waiting for input and output is: "+_theComputer.hasOutput());

        if (_theComputer.hasOutput())
        {
            int response = Integer.parseInt(_theComputer.getOutput());

            System.out.println("**Response is "+DroidStatus.toString(response));

            switch (response)
            {
                case DroidStatus.ARRIVED:  // arrived at the station!!
                {
                    _theMap.addContent(to, TileId.OXYGEN_STATION);
                    _currentLocation = to;

                    return true;
                }
                case DroidStatus.COLLISION:
                {
                    _theMap.addContent(to, TileId.WALL);  // didn't move as we hit a wall

                    return false;
                }
                case DroidStatus.MOVED:
                {
                    /*
                     * Droid moved so let's try to move again.
                     */

                    _theMap.addContent(to, TileId.TRAVERSE);
                    _currentLocation = to;

                    return explore(to);
                }
                default:
                    System.out.println("Unknown response: "+response);
            }
        }
        else
            System.out.println("Error - no output after move instruction!");

        return false;
    }

    private final Coordinate[] getNextPositions (Coordinate coord)
    {
        Coordinate[] coords = new Coordinate[4];

        // N, E, S, W

        coords[0] = new Coordinate(coord.getX(), coord.getY() -1);
        coords[1] = new Coordinate(coord.getX() -1, coord.getY());
        coords[2] = new Coordinate(coord.getX(), coord.getY() +1);
        coords[3] = new Coordinate(coord.getX() +1, coord.getY());

        return coords;
    }

    private boolean _debug;
    private Intcode _theComputer;
    private Coordinate _currentLocation;
    private Maze _theMap;
}
@@ -370,16 +370,50 @@
 ocation%0A
+        _previousLocation = null;%0A
         
@@ -989,31 +989,16 @@
 xplore (
-Coordinate from
 )%0A    %7B%0A
@@ -1331,30 +1331,24 @@
 ment.NORTH),
- from,
  moves%5B0%5D))%0A
@@ -1393,35 +1393,47 @@
 ut.println(%22
-%5Cn%22+_theMap
+**Failed to move NORTH%22
 );%0A%0A        
@@ -1486,30 +1486,24 @@
 ement.EAST),
- from,
  moves%5B1%5D))%0A
@@ -1556,35 +1556,46 @@
 ut.println(%22
-%5Cn%22+_theMap
+**Failed to move EAST%22
 );%0A%0A        
@@ -1657,22 +1657,16 @@
 .SOUTH),
- from,
  moves%5B2
@@ -1731,35 +1731,47 @@
 ut.println(%22
-%5Cn%22+_theMap
+**Failed to move SOUTH%22
 );%0A%0A        
@@ -1786,23 +1786,21 @@
         
-return 
+if (!
 tryToMov
@@ -1836,22 +1836,16 @@
 t.WEST),
- from,
  moves%5B3
@@ -1846,17 +1846,142 @@
 oves%5B3%5D)
-;
+)%0A                        %7B%0A                            System.out.println(%22**Failed to move WEST%22);%0A                        %7D
 %0A       
@@ -2045,37 +2045,73 @@
 %0A        return 
-false
+_theMap.isOxygenStation(_currentLocation)
 ;%0A    %7D%0A%0A    pri
@@ -2155,25 +2155,8 @@
 ion,
- Coordinate from,
  Coo
@@ -2242,20 +2242,32 @@
 from: %22+
-from
+_currentLocation
 +%22 to %22+
@@ -4467,16 +4467,58 @@
 cation;%0A
+    private Coordinate _previousLocation;%0A
     priv

Update RepairDroid.java