puzzlesolver: remove bounds check from PuzzleSolver#getStep

This commit is contained in:
Lotto
2018-02-25 20:48:16 +01:00
parent 3396c7ac45
commit f210bbc4fb
2 changed files with 19 additions and 10 deletions

View File

@@ -216,9 +216,16 @@ public class PuzzleSolverOverlay extends Overlay
graphics.setColor(Color.YELLOW);
// Display the next 4 steps
for (int j = 1; j < 5; j++)
for (int i = 1; i < 5; i++)
{
PuzzleState futureMove = solver.getStep(solver.getPosition() + j);
int j = solver.getPosition() + i;
if (j >= solver.getStepCount())
{
break;
}
PuzzleState futureMove = solver.getStep(j);
if (futureMove == null)
{
@@ -228,7 +235,7 @@ public class PuzzleSolverOverlay extends Overlay
int blankX = futureMove.getEmptyPiece() % DIMENSION;
int blankY = futureMove.getEmptyPiece() / DIMENSION;
int markerSize = DOT_MARKER_SIZE - j * 3;
int markerSize = DOT_MARKER_SIZE - i * 3;
int x = puzzleBoxLocation.getX() + blankX * PUZZLE_TILE_SIZE
+ PUZZLE_TILE_SIZE / 2 - markerSize / 2;
@@ -248,9 +255,16 @@ public class PuzzleSolverOverlay extends Overlay
int lastBlankY = currentMove.getEmptyPiece() / DIMENSION;
// Display the next 3 steps
for (int j = 1; j < 4; j++)
for (int i = 1; i < 4; i++)
{
PuzzleState futureMove = solver.getStep(solver.getPosition() + j);
int j = solver.getPosition() + i;
if (j >= solver.getStepCount())
{
break;
}
PuzzleState futureMove = solver.getStep(j);
if (futureMove == null)
{

View File

@@ -47,11 +47,6 @@ public class PuzzleSolver implements Runnable
public PuzzleState getStep(int stepIdx)
{
if (stepIdx < 0 || stepIdx >= solution.size())
{
return null;
}
return solution.get(stepIdx);
}