patternjavaMinor
Tremaux Algorithm
Viewed 0 times
algorithmtremauxstackoverflow
Problem
I am trying to implement Tremaux Algorithm for a 2D maze. So far the robot works well however I have some problems with
The
```
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
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 availableDirections = new ArrayList(); //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
Solution
You haven't shared enough to give better advice on the "big picture", but this code looks very strange.
This is a bad sign:
First of all, you should not catch general
Secondly, exception handling is for handling anomalies, things that should not happen under normal circumstances. Judging by the many occurrences of this in the posted code, it looks like you're using it too casually, for things that are not really anomalies but part of the normal behavior of your algorithm. You should use conditionals instead.
Many problems here:
-
Declare variables with interface types instead of implementations. In this case
-
Comments are hard to see appended at the right. Put comments on the line right before the line. Do this for all comments in your code, not only here.
-
The "don't forget to clear" comment suggests a confusion. Why not forget to clear? Be careful with semantic rules like this that the compiler cannot enforce. If something needs to be cleared, try to refactor in a way that it will be guaranteed to be cleared. In this particular case, it seems this list is only used in the
I would rename some of your methods and variables, for example:
This is a bad sign:
try {
robot.move(1);
} catch (Exception e) {
}First of all, you should not catch general
Exception instances, but the most specific possible instances.Secondly, exception handling is for handling anomalies, things that should not happen under normal circumstances. Judging by the many occurrences of this in the posted code, it looks like you're using it too casually, for things that are not really anomalies but part of the normal behavior of your algorithm. You should use conditionals instead.
ArrayList availableDirections = new ArrayList(); //dont' forget to clear!!!Many problems here:
-
Declare variables with interface types instead of implementations. In this case
List instead of ArrayList-
Comments are hard to see appended at the right. Put comments on the line right before the line. Do this for all comments in your code, not only here.
-
The "don't forget to clear" comment suggests a confusion. Why not forget to clear? Be careful with semantic rules like this that the compiler cannot enforce. If something needs to be cleared, try to refactor in a way that it will be guaranteed to be cleared. In this particular case, it seems this list is only used in the
if (validJunction()) { ... } block and cleared at the end. In fact, you could declare it inside that block, and not worry about clearing it, like this:if (validJunction()) {
List availableDirections = getAvailableDirections();
int index = randomGenerator.nextInt(availableDirections.size());
Direction direction = availableDirections.get(index);
turnRobot(direction);
try {
robot.move(1);
} catch (Exception e) {
}
} else if (canMoveForward()) {I would rename some of your methods and variables, for example:
isValidJunctionfromvalidJunction
randomfromrandomGenerator
Code Snippets
try {
robot.move(1);
} catch (Exception e) {
}ArrayList<Direction> availableDirections = new ArrayList<Direction>(); //dont' forget to clear!!!if (validJunction()) {
List<Direction> availableDirections = getAvailableDirections();
int index = randomGenerator.nextInt(availableDirections.size());
Direction direction = availableDirections.get(index);
turnRobot(direction);
try {
robot.move(1);
} catch (Exception e) {
}
} else if (canMoveForward()) {Context
StackExchange Code Review Q#68021, answer score: 3
Revisions (0)
No revisions yet.