Reformat code
This commit is contained in:
1 parent
51c12cc0ea
commit
60c654936a
31 files changed
+262
-466
No files matched your search
@@ -5,55 +5,45 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class EightPuzzleNode extends Node<int[][]>
|
||||
{
|
||||
public EightPuzzleNode(final int[][] state)
|
||||
{
|
||||
public class EightPuzzleNode extends Node<int[][]> {
|
||||
public EightPuzzleNode(final int[][] state) {
|
||||
super(state);
|
||||
}
|
||||
|
||||
private EightPuzzleNode(final int[][] value, final Node<int[][]> parent, final int heuristicCosts)
|
||||
{
|
||||
private EightPuzzleNode(final int[][] value, final Node<int[][]> parent, final int heuristicCosts) {
|
||||
super(value, parent, heuristicCosts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTargetReached(final Node<int[][]> target)
|
||||
{
|
||||
public boolean isTargetReached(final Node<int[][]> target) {
|
||||
return valueEquals(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Node<int[][]>> generateSuccessors()
|
||||
{
|
||||
public List<Node<int[][]>> generateSuccessors() {
|
||||
final var successors = new ArrayList<Node<int[][]>>();
|
||||
final var emptyPosition = Objects.requireNonNull(detectEmptyPosition());
|
||||
final var x = emptyPosition.x();
|
||||
final var y = emptyPosition.y();
|
||||
|
||||
for (final var dir : Direction.values())
|
||||
{
|
||||
for (final var dir : Direction.values()) {
|
||||
final var newState = copyOfState();
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
final var posToSwap = switch (dir) {
|
||||
case TOP -> new IntPair(x, y-1);
|
||||
case RIGHT -> new IntPair(x+1, y);
|
||||
case DOWN -> new IntPair(x, y+1);
|
||||
case LEFT -> new IntPair(x-1, y);
|
||||
case TOP -> new IntPair(x, y - 1);
|
||||
case RIGHT -> new IntPair(x + 1, y);
|
||||
case DOWN -> new IntPair(x, y + 1);
|
||||
case LEFT -> new IntPair(x - 1, y);
|
||||
};
|
||||
|
||||
this.swapStateField(newState, emptyPosition, posToSwap);
|
||||
final var successor = new EightPuzzleNode(newState, this, super.heuristicCosts+1);
|
||||
final var successor = new EightPuzzleNode(newState, this, super.heuristicCosts + 1);
|
||||
|
||||
if (!successor.valueEquals(this) && !successor.valueEquals(super.getParent()))
|
||||
{
|
||||
if (!successor.valueEquals(this) && !successor.valueEquals(super.getParent())) {
|
||||
successors.add(successor);
|
||||
}
|
||||
}
|
||||
catch (final ArrayIndexOutOfBoundsException ignored)
|
||||
{
|
||||
} catch (final ArrayIndexOutOfBoundsException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,31 +51,24 @@ public class EightPuzzleNode extends Node<int[][]>
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
final var builder = new StringBuilder();
|
||||
|
||||
for (final var row : super.value)
|
||||
{
|
||||
for (final var row : super.value) {
|
||||
builder.append(Arrays.toString(row)).append("\n");
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private boolean valueEquals(final Node<int[][]> node)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
private boolean valueEquals(final Node<int[][]> node) {
|
||||
if (node == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var row = 0; row < super.value.length; row++)
|
||||
{
|
||||
for (var col = 0; col < super.value[row].length; col++)
|
||||
{
|
||||
if (super.value[row][col] != node.value[row][col])
|
||||
{
|
||||
for (var row = 0; row < super.value.length; row++) {
|
||||
for (var col = 0; col < super.value[row].length; col++) {
|
||||
if (super.value[row][col] != node.value[row][col]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -94,14 +77,10 @@ public class EightPuzzleNode extends Node<int[][]>
|
||||
return true;
|
||||
}
|
||||
|
||||
private IntPair detectEmptyPosition()
|
||||
{
|
||||
for (var row = 0; row < super.value.length; row++)
|
||||
{
|
||||
for (var col = 0; col < super.value[row].length; col++)
|
||||
{
|
||||
if (super.value[row][col] == 0)
|
||||
{
|
||||
private IntPair detectEmptyPosition() {
|
||||
for (var row = 0; row < super.value.length; row++) {
|
||||
for (var col = 0; col < super.value[row].length; col++) {
|
||||
if (super.value[row][col] == 0) {
|
||||
return new IntPair(col, row);
|
||||
}
|
||||
}
|
||||
@@ -110,20 +89,17 @@ public class EightPuzzleNode extends Node<int[][]>
|
||||
return null;
|
||||
}
|
||||
|
||||
private int[][] copyOfState()
|
||||
{
|
||||
private int[][] copyOfState() {
|
||||
final var copy = new int[3][3];
|
||||
|
||||
for (var y = 0; y < copy.length; y++)
|
||||
{
|
||||
for (var y = 0; y < copy.length; y++) {
|
||||
System.arraycopy(super.value[y], 0, copy[y], 0, copy.length);
|
||||
}
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
private void swapStateField(final int[][] newState, final IntPair emptyPos, final IntPair posToSwap)
|
||||
{
|
||||
private void swapStateField(final int[][] newState, final IntPair emptyPos, final IntPair posToSwap) {
|
||||
final var tmp = newState[posToSwap.y()][posToSwap.x()];
|
||||
newState[posToSwap.y()][posToSwap.x()] = newState[emptyPos.y()][emptyPos.x()];
|
||||
newState[emptyPos.y()][emptyPos.x()] = tmp;
|
||||
|
||||
Reference in new issue
Block a user