Skip to content

Commit

Permalink
refactored should recompute solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sidnair committed Dec 7, 2011
1 parent 90fc844 commit f1bb1af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 75 deletions.
6 changes: 3 additions & 3 deletions game/GameConfig.txt
@@ -1,9 +1,9 @@
1, 30, 2, -1, maps/g7maps/map_50_50_illusion_1.txt
1, 30, 2, -1, maps/g7maps/map_50_50_illusion_2.txt

-1, -1, 5, -1, maps/Board1_L.txt
-1, -1, 5, -1, maps/Board1_R.txt

1, 30, 2, -1, maps/g7maps/map_50_50_illusion_1.txt
1, 30, 2, -1, maps/g7maps/map_50_50_illusion_2.txt

1, 30, 1, -1, maps/g2maps/PipeMaze_A_51_12.txt
1, 30, 1, -1, maps/g2maps/PipeMaze_B_51_12.txt

Expand Down
96 changes: 25 additions & 71 deletions src/mirroruniverse/g6/G6Player.java
Expand Up @@ -84,10 +84,9 @@ public class G6Player implements Player {
private Solver solver;

private boolean didExhaustiveCheck;
private boolean computedSolutionWhenFullyExplored;
private boolean leftCompletelyExplored, rightCompletelyExplored;
private boolean oldLeftExitFound, oldRightExitFound;
private int leftUnknownAroundExit, rightUnknownAroundExit;
private boolean computedSolution;
private boolean computedSolutionWhenRightFullyExplored;
private boolean computedSolutionWhenLeftFullyExplored;

public G6Player() {
// Set all points to be unknown.
Expand All @@ -107,85 +106,40 @@ public G6Player() {
}

private boolean shouldRecomputeSolution() {
// recompute only:
// recompute when:
// we finish examining a fake solution
// when we first see the exits,
// when we uncover squares around the exit,
// and after we've explored all of the board.
// after we've explored all of either board.
// when we're adjacent to an exit

if (solution != null && solution.isFake() && solution.isCompleted()) {
return true;
}
if (computedSolutionWhenFullyExplored) {
if (computedSolutionWhenLeftFullyExplored &&
computedSolutionWhenRightFullyExplored) {
return false;
}
if (solution == null) {
if (isFullyExplored()) {
computedSolutionWhenFullyExplored = true;
}
return true;
}
// This means we don't recompute a good solution if we see a better
// path as we explore.
if (solution != null && solution.getDiff() == 0) {
return false;
}

boolean nextToLeftExit = isNextToExit(left, x1, y1);
boolean nextToRightExit = isNextToExit(right, x2, y2);
boolean nextToEitherExit = nextToLeftExit || nextToRightExit;

boolean newLeftCompletelyExplored = numSquaresUnknown(left) == 0;
boolean newRightCompletelyExplored = numSquaresUnknown(right) == 0;
boolean eitherNewlyCompletelyExplored =
(!leftCompletelyExplored && newLeftCompletelyExplored)
|| (!rightCompletelyExplored && newRightCompletelyExplored);
leftCompletelyExplored = newLeftCompletelyExplored;
rightCompletelyExplored = newRightCompletelyExplored;

boolean eitherExitNewlyFound = (!oldLeftExitFound && leftExitFound) || (!oldRightExitFound && rightExitFound);
oldLeftExitFound = leftExitFound;
oldRightExitFound = rightExitFound;

int newLeftUnknownAroundExit = numSquaresUnknownAroundExit(left, r1);
int newRightUnknownAroundExit = numSquaresUnknownAroundExit(right, r2);
boolean eitherUnknownAroundExitNewlyUncovered = (newLeftUnknownAroundExit < leftUnknownAroundExit) || (newRightUnknownAroundExit < rightUnknownAroundExit);
leftUnknownAroundExit = newLeftUnknownAroundExit;
rightUnknownAroundExit = newRightUnknownAroundExit;

return nextToEitherExit || eitherNewlyCompletelyExplored || eitherExitNewlyFound || eitherUnknownAroundExitNewlyUncovered;
}

private int numSquaresUnknown(int[][] knowledge) {
int count = 0;
for(int i = 0; i < knowledge.length; i++) {
for(int j = 0; j < knowledge[0].length; j++) {
if(knowledge[j][i] == Utils.entitiesToShen(Entity.UNKNOWN)) {
count++;
if (solution == null || solution.getDiff() > 0) {
if (isLeftFullyExplored() && !computedSolutionWhenLeftFullyExplored) {
if (isRightFullyExplored()) {
computedSolutionWhenRightFullyExplored = true;
}
computedSolutionWhenLeftFullyExplored = true;
return true;
}
}
return count;
}

private int numSquaresUnknownAroundExit(int[][] knowledge, int r) {
int x = 0;
int y = 0;
for(int i = 0; i < knowledge.length; i++)
for(int j = 0; j < knowledge[0].length; j++)
if(knowledge[i][j] == Utils.entitiesToShen(Entity.EXIT)) {
y = i;
x = j;
}
int count = 0;
for(int i = Math.max(x - r, 0); i < Math.min(x + r + 1, knowledge[0].length); i++) {
for(int j = Math.max(y - r, 0); j < Math.min(y + r + 1, knowledge.length); j++) {
if(knowledge[j][i] == Utils.entitiesToShen(Entity.UNKNOWN)) {
count++;
}
if (isRightFullyExplored() && !computedSolutionWhenRightFullyExplored) {
computedSolutionWhenRightFullyExplored = true;
return true;
}
if (!computedSolution) {
computedSolution = true;
return true;
}
if (isNextToExit(left, x1, y1) || isNextToExit(right, x2, y2)) {
return true;
}
}
return count;
return false;
}

private boolean isNextToExit(int[][] knowledge, int x, int y) {
Expand Down
2 changes: 1 addition & 1 deletion src/mirroruniverse/g6/dfa/DFA.java
Expand Up @@ -239,7 +239,7 @@ private static boolean fabricate(DFA first, DFA other) {
ArrayList<Move> firstSol = first.findShortestPath();
ArrayList<Move> otherSol = other.findShortestPath();

if (G6Player.DEBUG) {
if (G6Player.SID_DEBUG) {
if (firstSol == null || otherSol == null) {
System.err.println("This should not be called after solving.");
System.exit(1);
Expand Down

0 comments on commit f1bb1af

Please sign in to comment.