diff --git a/src/search/breadthfirstsearch/BreadthFirstSearch.java b/src/search/breadthfirstsearch/BreadthFirstSearch.java index ba2cdf1..cb225f3 100644 --- a/src/search/breadthfirstsearch/BreadthFirstSearch.java +++ b/src/search/breadthfirstsearch/BreadthFirstSearch.java @@ -1,6 +1,7 @@ package search.breadthfirstsearch; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class BreadthFirstSearch @@ -19,7 +20,7 @@ public class BreadthFirstSearch newNodes.addAll(node.generateSuccessors()); } - if (newNodes.isEmpty()) + if (!newNodes.isEmpty()) { return breadthFirstSearch(newNodes, target); } diff --git a/test/search/breadthfirstsearch/BreadthFirstSearchTest.java b/test/search/breadthfirstsearch/BreadthFirstSearchTest.java index 02eeec8..cb38038 100644 --- a/test/search/breadthfirstsearch/BreadthFirstSearchTest.java +++ b/test/search/breadthfirstsearch/BreadthFirstSearchTest.java @@ -2,6 +2,9 @@ package search.breadthfirstsearch; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.List; + import static org.junit.jupiter.api.Assertions.*; class BreadthFirstSearchTest @@ -9,6 +12,28 @@ class BreadthFirstSearchTest @Test void shouldReturnCorrectTarget() { + int[][] state = { + {1, 2, 3}, + {4, 5, 6}, + {7, 0, 8} + }; + var root = new EightPuzzleNode(state); + + int[][] targetState = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 0} + }; + var expected = new EightPuzzleNode(targetState); + + var actual = new BreadthFirstSearch().breadthFirstSearch(List.of(root), expected); + + var a = ((EightPuzzleNode) actual).getState(); + + for (int[] b : a) + { + System.out.println(Arrays.toString(b)); + } } } \ No newline at end of file