15 puzzle solver: Difference between revisions
m
Small code improvement.
(New post.) |
m (Small code improvement.) |
||
Line 6,345:
if ( column > 0 ) {
nextPuzzle.makeMove(
}
if ( column < 3 ) {
Puzzle nextPuzzle =
}
if ( row > 0 ) {
Puzzle nextPuzzle =
nextPuzzle.makeMove(Move.UP);
}
if ( row < 3 ) {
Puzzle nextPuzzle =
nextPuzzle.makeMove(Move.DOWN);
}
▲ Puzzle nextPuzzle = aPuzzle.clone();
Integer temp = nextPuzzle.tiles.get(aIndex + aMove.step);▼
nextPuzzle.tiles.set(aIndex + aMove.step, 0);▼
nextPuzzle.tiles.set(aIndex, temp);▼
nextPuzzle.zeroIndex += aMove.step;▼
▲ nextPuzzle.moves.add(aMove.symbol);
if ( ! closedSet.contains(nextPuzzle) ) {▼
openSet.add(nextPuzzle);▼
if ( nextPuzzle.tiles.equals(Puzzle.GOAL) ) {▼
solution = nextPuzzle;▼
}▼
}▼
}
Line 6,385 ⟶ 6,371:
private String symbol;
private
}
Line 6,396 ⟶ 6,382:
searchDepth = aSearchDepth;
}
public void makeMove(Move aMove) {
moves.add(aMove.symbol);
}
▲ }
}
public long heuristic() {
Line 6,449 ⟶ 6,451:
private static Set<Puzzle> closedSet = new HashSet<Puzzle>();
private static Puzzle solution;
}
</syntaxhighlight>
|