Skip to content

Commit

Permalink
changed Board.isSolution01() - issue #13
Browse files Browse the repository at this point in the history
For custom-made boards we don't apply the rule "active robot must ricochet before reaching the goal" as strictly as for the standard Ricochet Robots boards.
This is achieved by detecting the "solution in 0 or 1 move" situation only if the active robot stops at the goal in a single move, because there is a wall or another robot behind the goal space. On all standard Ricochet Robots boards there are two walls next to each goal, so this condition is always met and we have to enforce this rule to avoid the trivial single-move solution. But on custom boards the goals can have no walls next to them and then some helper robot(s) are required to reach the goal. In these cases we allow the active robot to move straight to the goal, because moving the helper robot(s) to the required is not a trivial single-move solution.
  • Loading branch information
smack42 committed May 30, 2019
1 parent 17a4f44 commit 67057cf
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/driftingdroids/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -794,29 +794,51 @@ private Board addQuadrant(final int qNum, final int qPos) {

public boolean isSolution01() {
for (int robo = 0; robo < this.robots.length; ++robo) {
if ((this.goal.robotNumber != robo) && (this.goal.robotNumber != -1)) {
continue; // skip because it's not the goal robot
}
final int oldRoboPos = this.robots[robo];
if ((this.goal.position == oldRoboPos) && ((this.goal.robotNumber == robo) || (this.goal.robotNumber == -1))) {
if (this.goal.position == oldRoboPos) {
return true; // already on goal
}
int dir = -1;
for (final int dirIncr : this.directionIncrement) {
++dir;
int newRoboPos = oldRoboPos;
// move the robot until it reaches a wall. (_NOT_ another robot!)
int prevRoboPos = oldRoboPos;
// move the robot until it reaches a wall or another robot.
// NOTE: we rely on the fact that all boards are surrounded
// by outer walls. without the outer walls we would need
// some additional boundary checking here.
while (false == this.walls[dir][newRoboPos]) {
newRoboPos += dirIncr;
if ((this.goal.position == newRoboPos) && ((this.goal.robotNumber == robo) || (this.goal.robotNumber == -1))) {
return true; // one move to goal
while (true) {
if (true == this.walls[dir][newRoboPos]) { // stopped by wall
if (this.goal.position == newRoboPos) {
return true; // one move to goal
}
break; // can't go on
}
if (true == this.isRobotPos(newRoboPos)) { // stopped by robot
if (this.goal.position == prevRoboPos) {
return true; // one move to goal
}
// go on in this direction
}
prevRoboPos = newRoboPos;
newRoboPos += dirIncr;
}
}
}
return false;
}

private boolean isRobotPos(final int position) {
for (final int roboPos : this.robots) {
if (position == roboPos) {
return true;
}
}
return false;
}

public void setRobots(final int numRobots) {
this.robots = new int[numRobots];
Expand Down

0 comments on commit 67057cf

Please sign in to comment.