diff --git a/src/search/breadthfirstsearch/EightPuzzleNode.java b/src/search/breadthfirstsearch/EightPuzzleNode.java index 1532ef8..7363060 100644 --- a/src/search/breadthfirstsearch/EightPuzzleNode.java +++ b/src/search/breadthfirstsearch/EightPuzzleNode.java @@ -23,7 +23,18 @@ public class EightPuzzleNode implements Node @Override 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 @@ -127,6 +138,19 @@ public class EightPuzzleNode implements Node 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 final int left; diff --git a/test/search/breadthfirstsearch/BreadthFirstSearchTest.java b/test/search/breadthfirstsearch/BreadthFirstSearchTest.java index cb38038..1a81d9a 100644 --- a/test/search/breadthfirstsearch/BreadthFirstSearchTest.java +++ b/test/search/breadthfirstsearch/BreadthFirstSearchTest.java @@ -13,9 +13,9 @@ class BreadthFirstSearchTest void shouldReturnCorrectTarget() { int[][] state = { - {1, 2, 3}, - {4, 5, 6}, - {7, 0, 8} + {5, 0, 3}, + {2, 1, 6}, + {4, 7, 8} }; var root = new EightPuzzleNode(state); @@ -29,11 +29,7 @@ class BreadthFirstSearchTest var actual = new BreadthFirstSearch().breadthFirstSearch(List.of(root), expected); - var a = ((EightPuzzleNode) actual).getState(); - - for (int[] b : a) - { - System.out.println(Arrays.toString(b)); - } + System.out.println("Target: " + Arrays.deepToString(targetState)); + System.out.println("Actual:\n" + actual); } } \ No newline at end of file