This commit is contained in:
Niklas Birk 2019-03-24 16:58:10 +01:00
parent d2c1cb698c
commit 237782d518
2 changed files with 27 additions and 1 deletions

View File

@ -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);
}

View File

@ -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));
}
}
}