puzzlesolver: Merge nested if statements

This commit is contained in:
sdburns1998
2019-07-07 17:58:49 +02:00
parent adbe421838
commit 50bac46b35

View File

@@ -84,9 +84,7 @@ public class PuzzleState
int emptyPieceY = emptyPiece / DIMENSION; int emptyPieceY = emptyPiece / DIMENSION;
// Move left if there is space to the left // Move left if there is space to the left
if (emptyPieceX > 0) if (emptyPieceX > 0 && (parent == null || parent.emptyPiece != emptyPiece - 1))
{
if (parent == null || parent.emptyPiece != emptyPiece - 1)
{ {
PuzzleState state = new PuzzleState(this); PuzzleState state = new PuzzleState(this);
state.parent = this; state.parent = this;
@@ -97,12 +95,9 @@ public class PuzzleState
moves.add(state); moves.add(state);
} }
}
// Move right if there is space to the right // Move right if there is space to the right
if (emptyPieceX < DIMENSION - 1) if (emptyPieceX < DIMENSION - 1 && (parent == null || parent.emptyPiece != emptyPiece + 1))
{
if (parent == null || parent.emptyPiece != emptyPiece + 1)
{ {
PuzzleState state = new PuzzleState(this); PuzzleState state = new PuzzleState(this);
state.parent = this; state.parent = this;
@@ -113,12 +108,9 @@ public class PuzzleState
moves.add(state); moves.add(state);
} }
}
// Move up if there is space upwards // Move up if there is space upwards
if (emptyPieceY > 0) if (emptyPieceY > 0 && (parent == null || parent.emptyPiece != emptyPiece - DIMENSION))
{
if (parent == null || parent.emptyPiece != emptyPiece - DIMENSION)
{ {
PuzzleState state = new PuzzleState(this); PuzzleState state = new PuzzleState(this);
state.parent = this; state.parent = this;
@@ -129,12 +121,9 @@ public class PuzzleState
moves.add(state); moves.add(state);
} }
}
// Move down if there is space downwards // Move down if there is space downwards
if (emptyPieceY < DIMENSION - 1) if (emptyPieceY < DIMENSION - 1 && (parent == null || parent.emptyPiece != emptyPiece + DIMENSION))
{
if (parent == null || parent.emptyPiece != emptyPiece + DIMENSION)
{ {
PuzzleState state = new PuzzleState(this); PuzzleState state = new PuzzleState(this);
state.parent = this; state.parent = this;
@@ -145,7 +134,6 @@ public class PuzzleState
moves.add(state); moves.add(state);
} }
}
return moves; return moves;
} }