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