diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..0b9b0fb
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml
new file mode 100644
index 0000000..e96534f
--- /dev/null
+++ b/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+
\ No newline at end of file
diff --git a/src/search/breadthfirstsearch/BreadthFirstSearch.java b/src/search/breadthfirstsearch/BreadthFirstSearch.java
index cb225f3..6438b10 100644
--- a/src/search/breadthfirstsearch/BreadthFirstSearch.java
+++ b/src/search/breadthfirstsearch/BreadthFirstSearch.java
@@ -1,16 +1,15 @@
package search.breadthfirstsearch;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
public class BreadthFirstSearch
{
- public Node breadthFirstSearch(List nodes, Node target)
+ public Node breadthFirstSearch(List> nodes, Node target)
{
- var newNodes = new ArrayList();
+ var newNodes = new ArrayList>();
- for (Node node : nodes)
+ for (Node node : nodes)
{
if (node.isTargetReached(target))
{
diff --git a/src/search/breadthfirstsearch/EightPuzzleNode.java b/src/search/breadthfirstsearch/EightPuzzleNode.java
index 7363060..1b8fd07 100644
--- a/src/search/breadthfirstsearch/EightPuzzleNode.java
+++ b/src/search/breadthfirstsearch/EightPuzzleNode.java
@@ -6,28 +6,26 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;
-public class EightPuzzleNode implements Node
+public class EightPuzzleNode extends Node
{
- private int[][] state;
-
public EightPuzzleNode(int[][] state)
{
- if (!isValidState(state))
- {
- throw new IllegalArgumentException("Allowed numbers are only 0-8 and they must exist uniquely.");
- }
+ super(state);
+ }
- this.state = state;
+ private EightPuzzleNode(int[][] value, Node parent)
+ {
+ super(value, parent);
}
@Override
- public boolean isTargetReached(Node target)
+ public boolean isTargetReached(Node target)
{
- for (int row = 0; row < this.state.length; row++)
+ for (int row = 0; row < super.value.length; row++)
{
- for (int col = 0; col < this.state[row].length; col++)
+ for (int col = 0; col < super.value[row].length; col++)
{
- if (this.state[row][col] != ((EightPuzzleNode) target).state[row][col])
+ if (super.value[row][col] != target.value[row][col])
{
return false;
}
@@ -38,9 +36,9 @@ public class EightPuzzleNode implements Node
}
@Override
- public List generateSuccessors()
+ public List> generateSuccessors()
{
- var successors = new ArrayList();
+ var successors = new ArrayList>();
var emptyPosition = Objects.requireNonNull(detectEmptyPosition());
int x = emptyPosition.getRight();
int y = emptyPosition.getLeft();
@@ -58,25 +56,25 @@ public class EightPuzzleNode implements Node
tmp = newState[y-1][x];
newState[y-1][x] = newState[y][x];
newState[y][x] = tmp;
- successors.add(new EightPuzzleNode(newState));
+ successors.add(new EightPuzzleNode(newState, this));
break;
case RIGHT:
tmp = newState[y][x+1];
newState[y][x+1] = newState[y][x];
newState[y][x] = tmp;
- successors.add(new EightPuzzleNode(newState));
+ successors.add(new EightPuzzleNode(newState, this));
break;
case DOWN:
tmp = newState[y+1][x];
newState[y+1][x] = newState[y][x];
newState[y][x] = tmp;
- successors.add(new EightPuzzleNode(newState));
+ successors.add(new EightPuzzleNode(newState, this));
break;
case LEFT:
tmp = newState[y][x-1];
newState[y][x-1] = newState[y][x];
newState[y][x] = tmp;
- successors.add(new EightPuzzleNode(newState));
+ successors.add(new EightPuzzleNode(newState, this));
break;
}
}
@@ -88,12 +86,8 @@ public class EightPuzzleNode implements Node
return successors;
}
- public int[][] getState()
- {
- return state;
- }
-
- private boolean isValidState(int[][] state)
+ @Override
+ protected boolean isValidValue(int[][] state)
{
var numbers = Arrays.stream(state).flatMapToInt(IntStream::of).toArray();
@@ -112,11 +106,11 @@ public class EightPuzzleNode implements Node
private IntPair detectEmptyPosition()
{
- for (int row = 0; row < this.state.length; row++)
+ for (int row = 0; row < super.value.length; row++)
{
- for (int col = 0; col < this.state[row].length; col++)
+ for (int col = 0; col < super.value[row].length; col++)
{
- if (state[row][col] == 0)
+ if (super.value[row][col] == 0)
{
return new IntPair(row, col);
}
@@ -132,7 +126,7 @@ public class EightPuzzleNode implements Node
for (int y = 0; y < copy.length; y++)
{
- System.arraycopy(this.state[y], 0, copy[y], 0, copy.length);
+ System.arraycopy(super.value[y], 0, copy[y], 0, copy.length);
}
return copy;
@@ -141,9 +135,9 @@ public class EightPuzzleNode implements Node
@Override
public String toString()
{
- StringBuilder builder = new StringBuilder();
+ StringBuilder builder = new StringBuilder("Node:\n");
- for (int[] row : this.state)
+ for (int[] row : super.value)
{
builder.append(Arrays.toString(row)).append("\n");
}
diff --git a/src/search/breadthfirstsearch/Node.java b/src/search/breadthfirstsearch/Node.java
index b2bf3e9..3cc038e 100644
--- a/src/search/breadthfirstsearch/Node.java
+++ b/src/search/breadthfirstsearch/Node.java
@@ -2,8 +2,44 @@ package search.breadthfirstsearch;
import java.util.List;
-public interface Node
+public abstract class Node
{
- boolean isTargetReached(Node target);
- List generateSuccessors();
+ protected T value;
+ private Node parent;
+
+ protected Node(T value)
+ {
+ this(value, null);
+ }
+
+ protected Node(T value, Node parent)
+ {
+ if (!isValidValue(value))
+ {
+ throw new IllegalArgumentException("Illegal node value");
+ }
+
+ this.value = value;
+ this.parent = parent;
+ }
+
+ public T getValue()
+ {
+ return this.value;
+ }
+
+ public Node getParent()
+ {
+ return this.parent;
+ }
+
+ @Override
+ public String toString()
+ {
+ return this.value.toString();
+ }
+
+ protected abstract boolean isValidValue(T value);
+ public abstract boolean isTargetReached(Node target);
+ public abstract List> generateSuccessors();
}
diff --git a/test/search/breadthfirstsearch/BreadthFirstSearchTest.java b/test/search/breadthfirstsearch/BreadthFirstSearchTest.java
index 1a81d9a..ff8f1c9 100644
--- a/test/search/breadthfirstsearch/BreadthFirstSearchTest.java
+++ b/test/search/breadthfirstsearch/BreadthFirstSearchTest.java
@@ -5,8 +5,6 @@ import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
-import static org.junit.jupiter.api.Assertions.*;
-
class BreadthFirstSearchTest
{
@Test
@@ -19,7 +17,6 @@ class BreadthFirstSearchTest
};
var root = new EightPuzzleNode(state);
-
int[][] targetState = {
{1, 2, 3},
{4, 5, 6},
@@ -32,4 +29,9 @@ class BreadthFirstSearchTest
System.out.println("Target: " + Arrays.deepToString(targetState));
System.out.println("Actual:\n" + actual);
}
+
+ private void printSolution(Node targetNode)
+ {
+
+ }
}
\ No newline at end of file
diff --git a/test/search/breadthfirstsearch/EightPuzzleNodeTest.java b/test/search/breadthfirstsearch/EightPuzzleNodeTest.java
index 6e0d3c0..558bbd0 100644
--- a/test/search/breadthfirstsearch/EightPuzzleNodeTest.java
+++ b/test/search/breadthfirstsearch/EightPuzzleNodeTest.java
@@ -3,9 +3,6 @@ package search.breadthfirstsearch;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import java.util.Arrays;
-import java.util.List;
-
class EightPuzzleNodeTest
{
@Test
@@ -120,8 +117,8 @@ class EightPuzzleNodeTest
{0, 5, 8}
};
- Assertions.assertArrayEquals(newState1, ((EightPuzzleNode) successors.get(0)).getState());
- Assertions.assertArrayEquals(newState2, ((EightPuzzleNode) successors.get(1)).getState());
- Assertions.assertArrayEquals(newState3, ((EightPuzzleNode) successors.get(2)).getState());
+ Assertions.assertArrayEquals(newState1, successors.get(0).getValue());
+ Assertions.assertArrayEquals(newState2, successors.get(1).getValue());
+ Assertions.assertArrayEquals(newState3, successors.get(2).getValue());
}
}
\ No newline at end of file