<commit_before>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;
        _robotTrack = new Trail(_theMap);
        _path = new Stack<String>();
        _robotFacing = CellId.ROBOT_FACING_UP;
        _currentMoveDirection = "";
        _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 boolean createPath ()
    {
        System.out.println("createPath from: "+_currentPosition);

        if (_currentPosition != null)
        {
            /*
             * Try L then R.
             * 
             * Robot always starts facing up. Change facing when we
             * start to move but remember initial facing so we can
             * refer movement directions as L or R.
             */

            if (!tryToMove(MOVE_LEFT, leftCoordinate(_currentPosition)))
            {
                return tryToMove(MOVE_RIGHT, rightCoordinate(_currentPosition));
            }
            else
                return true;
        }
        else
        {
            System.out.println("Robot not found!");

            return false;
        }
    }

    private boolean tryToMove (String direction, Coordinate coord)
    {
        System.out.println("tryMove: "+coord+" with direction: "+direction);

        if (_robotTrack.visited(coord))
        {
            System.out.println("Robot already visited this location.");

            return false;
        }

        System.out.println("Location not visited ... yet.");

        if (_theMap.isScaffold(coord))
        {
            System.out.println("Is scaffolding!");

            _currentMoveDirection = direction;
            _path.push(_currentMoveDirection);
            
            _robotTrack.changeElement(coord, _currentMoveDirection);
            _currentPosition = coord;

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

            return createPath();
        }
        else
        {
            System.out.println("Not scaffolding!");

            System.out.println("Robot was facing "+_robotFacing+" and moving "+direction);

            changeFacing();

            System.out.println("Robot now facing "+_robotFacing);

            String nextDirection = getNextDirection();

            System.out.println("Next direction to try with new facing: "+nextDirection);

            if (MOVE_LEFT.equals(nextDirection))
                coord = leftCoordinate(_currentPosition);
            else
                coord = rightCoordinate(_currentPosition);

            return tryToMove(nextDirection, coord);
        }
    }

    private String getNextDirection ()
    {
        Coordinate coord = leftCoordinate(_currentPosition);

        if (_robotTrack.visited(coord))
        {
            coord = rightCoordinate(_currentPosition);

            if (_robotTrack.visited(coord))
                return null;
            else
                return MOVE_RIGHT;
        }
        else
            return MOVE_LEFT;
    }

    private void changeFacing ()
    {
        switch (_robotFacing)
        {
            case CellId.ROBOT_FACING_UP:
            {
                if (_currentMoveDirection.equals(MOVE_LEFT))
                    _robotFacing = CellId.ROBOT_FACING_LEFT;
                else
                    _robotFacing = CellId.ROBOT_FACING_RIGHT;
            }<commit_msg>Update MovementLogic.java<commit_after>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;
        _robotTrack = new Trail(_theMap);
        _path = new Stack<String>();
        _robotFacing = CellId.ROBOT_FACING_UP;
        _currentMoveDirection = "";
        _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 boolean createPath ()
    {
        System.out.println("createPath from: "+_currentPosition);

        if (_currentPosition != null)
        {
            /*
             * Try L then R.
             * 
             * Robot always starts facing up. Change facing when we
             * start to move but remember initial facing so we can
             * refer movement directions as L or R.
             */

            if (!tryToMove(MOVE_LEFT, leftCoordinate(_currentPosition)))
            {
                return tryToMove(MOVE_RIGHT, rightCoordinate(_currentPosition));
            }
            else
                return true;
        }
        else
        {
            System.out.println("Robot not found!");

            return false;
        }
    }

    private boolean tryToMove (String direction, Coordinate coord)
    {
        System.out.println("tryMove: "+coord+" with direction: "+direction);

        if (_robotTrack.visited(coord))
        {
            System.out.println("Robot already visited this location.");

            return false;
        }

        System.out.println("Location not visited ... yet.");

        if (_theMap.isScaffold(coord))
        {
            System.out.println("Is scaffolding!");

            _currentMoveDirection = direction;
            _path.push(_currentMoveDirection);
            
            _robotTrack.changeElement(coord, _currentMoveDirection);
            _currentPosition = coord;

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

            return createPath();
        }
        else
        {
            System.out.println("Not scaffolding!");

            System.out.println("Robot was facing "+_robotFacing+" and moving "+direction);

            changeFacing();

            System.out.println("Robot now facing "+_robotFacing);

            String nextDirection = getNextDirection();

            System.out.println("Next direction to try with new facing: "+nextDirection);

            if (MOVE_LEFT.equals(nextDirection))
                coord = leftCoordinate(_currentPosition);
            else
                coord = rightCoordinate(_currentPosition);

            return tryToMove(nextDirection, coord);
        }
    }

    private String getNextDirection ()
    {
        System.out.println("Getting next direction to move from: "+_currentPosition);

        Coordinate coord = leftCoordinate(_currentPosition);

        System.out.println("Left coordinate would be: "+coord);

        if (_robotTrack.visited(coord))
        {
            System.out.println("Visited so try right ...");

            coord = rightCoordinate(_currentPosition);

            System.out.println("Right coordinate would be: "+coord);

            if (_robotTrack.visited(coord))
                return null;
            else
                return MOVE_RIGHT;
        }
        else
        {
            System.out.println("Not visited.");
            
            return MOVE_LEFT;
        }
    }

    private void changeFacing ()
    {
        switch (_robotFacing)
        {
            case CellId.ROBOT_FACING_UP:
            {
                if (_currentMoveDirection.equals(MOVE_LEFT))
                    _robotFacing = CellId.ROBOT_FACING_LEFT;
                else
                    _robotFacing = CellId.ROBOT_FACING_RIGHT;
            }
{'fraction_non_alphanumeric': 0.08847549909255899, 'fraction_numerical': 0.0009074410163339383, 'mean_word_length': 1.625670041691483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2133367', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2244, 'n_words': 534}