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!

        recursiveSearch(_currentLocation);

        return numberOfSteps;
    }

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

    public void foo ()
    {
                /*
        if (_theMap.isWall(from) || _theMap.isExplored(from))
            return false;
        
        _currentLocation = from;
        _visited.add(_currentLocation);

        _theMap.addContent(_currentLocation, TileId.TRAVERSE);

        if (_theMap.isOxygenStation(from))
            return true;
            */
    }

    /*
     * 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 recursiveSearch (Coordinate from)
    {
        while (!_theComputer.hasHalted())
        {
            Coordinate[] moves = getNextPositions(from);

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

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

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

                    if (!tryToMove(String.valueOf(DroidMovement.EAST), 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 recursiveSearch(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];

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

        return coords;
    }

    private boolean _debug;
    private Intcode _theComputer;
    private Coordinate _currentLocation;
    private Maze _theMap;
}
@@ -593,31 +593,23 @@
         
-recursiveSearch
+explore
 (_curren
@@ -741,381 +741,8 @@
  %7D%0A%0A
-    public void foo ()%0A    %7B%0A                /*%0A        if (_theMap.isWall(from) %7C%7C _theMap.isExplored(from))%0A            return false;%0A        %0A        _currentLocation = from;%0A        _visited.add(_currentLocation);%0A%0A        _theMap.addContent(_currentLocation, TileId.TRAVERSE);%0A%0A        if (_theMap.isOxygenStation(from))%0A            return true;%0A            */%0A    %7D%0A%0A
     
@@ -946,31 +946,23 @@
 boolean 
-recursiveSearch
+explore
  (Coordi
@@ -1086,16 +1086,57 @@
 s(from);
+  // get all possible moves (Coordinates)
 %0A%0A      
@@ -1168,32 +1168,109 @@
 %22%5Cn%22+_theMap);%0A%0A
+            /*%0A             * We search N, E, S and then W.%0A             */%0A%0A
             if (
@@ -1463,13 +1463,12 @@
 ent.
-SOUTH
+EAST
 ), f
@@ -1612,36 +1612,37 @@
 f(DroidMovement.
-EAST
+SOUTH
 ), from, moves%5B2
@@ -3468,23 +3468,15 @@
 urn 
-recursiveSearch
+explore
 (to)
@@ -3858,16 +3858,39 @@
 te%5B4%5D;%0A%0A
+        // N, E, S, W%0A%0A
         
@@ -3987,32 +3987,35 @@
 ate(coord.getX()
+ -1
 , coord.getY() +
@@ -4012,19 +4012,16 @@
 d.getY()
- +1
 );%0A     
@@ -4054,35 +4054,32 @@
 ate(coord.getX()
- -1
 , coord.getY());
@@ -4068,32 +4068,35 @@
 (), coord.getY()
+ +1
 );%0A        coord

Update RepairDroid.java