Changed implementation

This commit is contained in:
Niklas Birk 2019-03-24 17:25:51 +01:00
parent 237782d518
commit 19fbb91529
2 changed files with 30 additions and 10 deletions

View File

@ -23,7 +23,18 @@ public class EightPuzzleNode implements Node
@Override @Override
public boolean isTargetReached(Node target) public boolean isTargetReached(Node target)
{ {
return Arrays.equals(this.state, ((EightPuzzleNode) target).state); for (int row = 0; row < this.state.length; row++)
{
for (int col = 0; col < this.state[row].length; col++)
{
if (this.state[row][col] != ((EightPuzzleNode) target).state[row][col])
{
return false;
}
}
}
return true;
} }
@Override @Override
@ -127,6 +138,19 @@ public class EightPuzzleNode implements Node
return copy; return copy;
} }
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
for (int[] row : this.state)
{
builder.append(Arrays.toString(row)).append("\n");
}
return builder.toString();
}
private class IntPair private class IntPair
{ {
private final int left; private final int left;

View File

@ -13,9 +13,9 @@ class BreadthFirstSearchTest
void shouldReturnCorrectTarget() void shouldReturnCorrectTarget()
{ {
int[][] state = { int[][] state = {
{1, 2, 3}, {5, 0, 3},
{4, 5, 6}, {2, 1, 6},
{7, 0, 8} {4, 7, 8}
}; };
var root = new EightPuzzleNode(state); var root = new EightPuzzleNode(state);
@ -29,11 +29,7 @@ class BreadthFirstSearchTest
var actual = new BreadthFirstSearch().breadthFirstSearch(List.of(root), expected); var actual = new BreadthFirstSearch().breadthFirstSearch(List.of(root), expected);
var a = ((EightPuzzleNode) actual).getState(); System.out.println("Target: " + Arrays.deepToString(targetState));
System.out.println("Actual:\n" + actual);
for (int[] b : a)
{
System.out.println(Arrays.toString(b));
}
} }
} }