import java.util.*;

public class MovementLogic
{
    public static final int ROUTINE_A = 0;
    public static final int ROUTINE_B = 1;
    public static final int ROUTINE_C = 2;
    public static final int ROUTINE_D = 3;

    public static final String MOVE_LEFT = "L";
    public static final String MOVE_RIGHT = "R";

    public MovementLogic (Map theMap, boolean debug)
    {
        _theMap = theMap;
        _path = new Stack<String>();
        _robotFacing = CellId.ROBOT_FACING_UP;
        _currentPosition = new Coordinate(_theMap.findStartingPoint());
        _debug = debug;
    }

    public void createMovementFunctions ()
    {
        createPath();

        String pathElement = _path.pop();

        System.out.println("Path:");

        while (pathElement != null)
        {
            System.out.println(pathElement);

            try
            {
                _path.pop();
            }
            catch (Exception ex)
            {
                pathElement = null;
            }
        }
    }

    public void createMovementRoutine ()
    {
        
    }

    /*
     * Create path using only L and R.
     */

    private void createPath ()
    {
        if (_currentPosition != null)
        {
            /*
             * Try L then R.
             * 
             * Robot always starts facing up. Don't change facing until
             * we run into a "wall" (open space, i.e., scaffolding).
             */

            if (!tryMoveLeft())
                tryMoveRight();
        }
        else
            System.out.println("Robot not found!");
    }

    private boolean tryMoveRight ()
    {
        Coordinate coord = rightPosition(_currentPosition);

        System.out.println("tryMoveRight: "+coord);

        if (_theMap.isScaffold(coord))
        {
            _path.push(MOVE_RIGHT);
            _currentPosition = coord;

            return tryMoveRight();
        }
        {
            System.out.println("Right change facing!");
            
            return false;
        }
    }

    private boolean tryMoveLeft ()
    {
        Coordinate coord = leftPosition(_currentPosition);

        System.out.println("tryMoveLeft: "+coord);

        if (_theMap.isScaffold(coord))
        {
            _path.push(MOVE_LEFT);
            _currentPosition = coord;

            return tryMoveLeft();
        }
        else
        {
            System.out.println("Left change facing!");

            return false;
        }
    }

    private final Coordinate rightPosition (Coordinate coord)
    {
        int x = coord.getX();
        int y = coord.getY();

        switch (_robotFacing)
        {
            case CellId.ROBOT_FACING_DOWN:
            {
                x--;
            }
            break;
            case CellId.ROBOT_FACING_UP:
            {
                x++;
            }
            break;
            case CellId.ROBOT_FACING_LEFT:
            {
                y++;
            }
            break;
            case CellId.ROBOT_FACING_RIGHT:
            default:
            {
                y--;
            }
            break;
        }

        return new Coordinate(x, y);
    }

    private final Coordinate leftPosition (Coordinate coord)
    {
        int x = coord.getX();
        int y = coord.getY();

        switch (_robotFacing)
        {
            case CellId.ROBOT_FACING_DOWN:
            {
                x++;
            }
            break;
            case CellId.ROBOT_FACING_UP:
            {
                x--;
            }
            break;
            case CellId.ROBOT_FACING_LEFT:
            {
                y--;
            }
            break;
            case CellId.ROBOT_FACING_RIGHT:
            default:
            {
                y++;
            }
            break;
        }

        return new Coordinate(x, y);
    }

    private Map _theMap;
    private Stack<String> _path;
    private String _robotFacing;
    private Coordinate _currentPosition;
    private boolean _debug;
}
@@ -483,16 +483,52 @@
 ING_UP;%0A
+        _currentMoveDirection = %22%22;%0A
         
@@ -1843,28 +1843,87 @@
    _
-path.push(MOVE_RIGHT
+currentMoveDirection = MOVE_RIGHT;%0A            _path.push(_currentMoveDirection
 );%0A 
@@ -2346,27 +2346,86 @@
    _
-path.push(MOVE_LEFT
+currentMoveDirection = MOVE_LEFT;%0A            _path.push(_currentMoveDirection
 );%0A 
@@ -3995,32 +3995,96 @@
 e(x, y);%0A    %7D%0A%0A
+    private final void changeDirection ()%0A    %7B%0A        %0A    %7D%0A%0A
     private Map 
@@ -4158,16 +4158,58 @@
 Facing;%0A
+    private String _currentMoveDirection;%0A
     priv

Update MovementLogic.java