I am trying to implement Tremaux Algorithm for a 2D maze. So far the robot works well however I have some problems with retraceStep(). I'm not asking how to implement that, but I just want to make sure that there are no serious logical flaws with my code. @Override    public boolean drive2Exit() throws Exception {        ArrayList<Direction> availableDirections = new ArrayList<Direction>(); //dont' forget to clear!!!        while (!robot.isAtGoal() && robot.getBatteryLevel() >= robot.getEnergyForStepForward()) {            addPositionToVisited();            if (validJunction()) { //Visited cells are treated as walls. Junctions return true if there is no wall to the left or right                availableDirections = getAvailableDirections();                int index = randomGenerator.nextInt(availableDirections.size());                Direction direction = availableDirections.get(index);                turnRobot(direction);                try {                    robot.move(1);                } catch (Exception e) {}                availableDirections.clear();            } else if (canMoveForward()) { //Visited cells are treated as walls                try {                    robot.move(1);                } catch (Exception e) {}            } else {                while (!validJunction()) {                    retraceStep();                }            }        }        if (robot.isAtGoal())            return true;        else if (robot.getBatteryLevel() < robot.getEnergyForStepForward())             throw new Exception();        return false;    }The retraceStep() is simply a left hand wallfollower algorithm:protected void retraceStep() throws Exception {    if (robot.distanceToObstacle(Direction.LEFT) != 0) {        robot.rotate(Turn.LEFT);        try {            robot.move(1);        } catch (Exception e) {}    } else if (robot.distanceToObstacle(Direction.FORWARD) != 0) {        try {            robot.move(1);        } catch (Exception e){}    } else if (robot.isAtJunction()) {        robot.rotate(Turn.RIGHT);        try {            robot.move(1);        } catch (Exception e) {}    } else {        robot.rotate(Turn.AROUND);        try {            robot.move(1);        } catch (Exception e) {}    }}Any tips on restructuring or anything is appreciated.
Tremaux Algorithm
java;algorithm;pathfinding
throws Exception is rarely a good idea.  Consider reusing existing exceptions (IllegalStateException?) or defining exceptions specific to your domain (NoPathToTargetException?).  Otherwise, callers of your methods will be forced to handle the top-level Exception throwable type, which invariably leads to the next paragraph...An empty try-catch usually causes more problems than it solves, especially when catching the Exception base type.  If this is a debugging construct, consider printing the stack trace to the error stream.  You'd be surprised how many can't-happens logging catches. ;)Use braces even for single-statement blocks.  One day, you'll be sorry you didn't; I know I was.  I can see exceptions being made for condensed one-liners, but if you put the following statement on a new line anyway, there's no reason not to add the braces.availableDirections is only used immediately following assignment, so you can pull its declaration down to the block where you need it.  You may want to factor it out and make a getRandomAvailableDirection() method.  (Your comment stresses that calling clear() is important, but I don't see why.)Since you check for low power in your loop anyway, you may want to throw the exception (BatteryLowException?) there and then.  Another option is to make a specific return type:enum DriveToExitResult {    REACHED,    UNREACHABLE,    LOW_BATTERY;}public DriveToExitResult driveToExit() {    // ...Each branch in retraceStep() ultimately leads to robot.move(1); we can pull that out as well, and put it at the end.Grand total@Overridepublic boolean drive2Exit() {    while (!robot.isAtGoal()) {        if (robot.getBatteryLevel() < robot.getEnergyForStepForward()) {            throw new BatteryLowException(Battery level too low to step forward);        }        addPositionToVisited();        if (validJunction()) { //Visited cells are treated as walls. Junctions return true if there is no wall to the left or right            Direction direction = getRandomAvailableDirection();            assert direction != null : valid junction but random direction was null            turnRobot(direction);            robot.move(1);        } else if (canMoveForward()) { //Visited cells are treated as walls            robot.move(1);        } else {            while (!validJunction()) {                retraceStep();            }        }    }    // return robot.isAtGoal();    return true; // loop only breaks through success or exception}protected Direction getRandomAvailableDirection() {    final List<Direction> availableDirections = getAvailableDirections();    if ( availableDirections.isEmpty() ) {        return null; // or throw an exception    }    final int idx = randomGenerator.nextInt(availableDirections.size());    return availableDirections.get(idx);} protected void retraceStep() {    if (robot.distanceToObstacle(Direction.LEFT) != 0) {        robot.rotate(Turn.LEFT);    } else if (robot.isAtJunction()) {        robot.rotate(Turn.RIGHT);    } else if (robot.distanceToObstacle(Direction.FORWARD) == 0) {        robot.rotate(Turn.AROUND);    }       robot.move(1);}If I had to guess from the code, it feels like your issue would be here, as you said:while (!validJunction()) {    retraceStep();}The other code in your method takes care to take only a single step and check power levels each time.  This part, however, does not seem to do so.  There's a different level of abstraction here, and that's a good place to start looking for implicit assumptions and/or incompatibilities.Log those exceptions.  You never know what is hidden there!