Skip to content

Commit

Permalink
fixed a bug in Board.isSolution01(). fixed potential race condition i…
Browse files Browse the repository at this point in the history
…n GUI / Solver.
  • Loading branch information
smack42 committed Jun 29, 2014
1 parent 1509919 commit 10ca3e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
28 changes: 25 additions & 3 deletions src/driftingdroids/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public class Board {
private final List<Goal> randomGoals;
private Goal goal; // the current goal

private int[] robots; // index=robot, value=position
private final int[] robots; // index=robot, value=position
private boolean isFreestyleBoard;

public class Goal implements Comparable<Goal> {
Expand Down Expand Up @@ -306,6 +306,7 @@ private Board(int width, int height, int numRobots) {
this.directionIncrement[WEST] = -1;
this.quadrants = new int[4];
this.walls = new boolean[4][width * height]; //filled with "false"
this.robots = new int[numRobots];
this.setRobots(numRobots);
this.goals = new ArrayList<Goal>();
this.randomGoals = new ArrayList<Goal>();
Expand All @@ -314,6 +315,28 @@ private Board(int width, int height, int numRobots) {
}


public static Board createClone(final Board oldBoard) {
// 1. board size, numRobots
final Board newBoard = new Board(oldBoard.width, oldBoard.height, oldBoard.robots.length);
// 2. robots
System.arraycopy(oldBoard.robots, 0, newBoard.robots, 0, newBoard.robots.length);
// 3. quadrants
System.arraycopy(oldBoard.quadrants, 0, newBoard.quadrants, 0, newBoard.quadrants.length);
// 4. walls
for (int i = 0; i < oldBoard.walls.length; ++i) {
System.arraycopy(oldBoard.walls[i], 0, newBoard.walls[i], 0, newBoard.walls[i].length);
}
// 5. list of goals
newBoard.goals.clear();
newBoard.goals.addAll(oldBoard.goals);
// 6. active goal
newBoard.goal = oldBoard.goal;
// 7. isFreestyleBoard
newBoard.isFreestyleBoard = oldBoard.isFreestyleBoard;
return newBoard;
}


public static Board createBoardFreestyle(final Board oldBoard, final int width, final int height, final int numRobots) {
if ((width < WIDTH_MIN) || (height < HEIGHT_MIN) || (width*height > SIZE_MAX)) {
System.out.println("error in createBoardFreestyle(): invalid parameter: width=" + width + " height=" + height + " size=" + width*height);
Expand Down Expand Up @@ -783,7 +806,7 @@ public boolean isSolution01() {
while (false == this.walls[dir][newRoboPos]) {
newRoboPos += dirIncr;
}
if ( (this.goal.position == newRoboPos) &&
if ( ((this.goal.position == oldRoboPos) || (this.goal.position == newRoboPos)) &&
((this.goal.robotNumber == robo) || (this.goal.robotNumber == -1)) ) {
return true;
}
Expand All @@ -794,7 +817,6 @@ public boolean isSolution01() {


public void setRobots(final int numRobots) {
this.robots = new int[numRobots];
if (this.isFreestyleBoard()) {
this.setRobotsRandom();
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/driftingdroids/ui/Starter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
public class Starter {

public static void main(String[] args) throws InterruptedException, InvocationTargetException {
new SwingGUI("DriftingDroids 1.3.3 (2014-06-23) __DEVELOPMENT__");
new SwingGUI("DriftingDroids 1.3.3 (2014-06-29) __DEVELOPMENT__");
//runTestRandom1000();
// runTestKeyDepthMap();
// runTestKey2();
Expand Down
2 changes: 1 addition & 1 deletion src/driftingdroids/ui/SwingGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private void runSolverTask() {
private class SolverTask extends SwingWorker<Solver, Object> {
@Override
protected Solver doInBackground() throws Exception {
final Solver solver = Solver.createInstance(board);
final Solver solver = Solver.createInstance(Board.createClone(board));
solver.setOptionSolutionMode((Solver.SOLUTION_MODE)jcomboOptSolutionMode.getSelectedItem());
solver.setOptionAllowRebounds(jcheckOptAllowRebounds.isSelected());
jtextSolution.setText(null);
Expand Down

0 comments on commit 10ca3e4

Please sign in to comment.