Gnerified node

This commit is contained in:
birkn 2019-03-25 19:14:17 +01:00
parent 19fbb91529
commit 481b292d17
7 changed files with 204 additions and 46 deletions

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="OptionalGetWithoutIsPresent" enabled="false" level="WARNING" enabled_by_default="false" />
</profile>
</component>

124
.idea/uiDesigner.xml generated Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View File

@ -1,16 +1,15 @@
package search.breadthfirstsearch; package search.breadthfirstsearch;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class BreadthFirstSearch public class BreadthFirstSearch
{ {
public Node breadthFirstSearch(List<Node> nodes, Node target) public <T> Node<T> breadthFirstSearch(List<Node<T>> nodes, Node<T> target)
{ {
var newNodes = new ArrayList<Node>(); var newNodes = new ArrayList<Node<T>>();
for (Node node : nodes) for (Node<T> node : nodes)
{ {
if (node.isTargetReached(target)) if (node.isTargetReached(target))
{ {

View File

@ -6,28 +6,26 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.IntStream; import java.util.stream.IntStream;
public class EightPuzzleNode implements Node public class EightPuzzleNode extends Node<int[][]>
{ {
private int[][] state;
public EightPuzzleNode(int[][] state) public EightPuzzleNode(int[][] state)
{ {
if (!isValidState(state)) super(state);
{ }
throw new IllegalArgumentException("Allowed numbers are only 0-8 and they must exist uniquely.");
}
this.state = state; private EightPuzzleNode(int[][] value, Node<int[][]> parent)
{
super(value, parent);
} }
@Override @Override
public boolean isTargetReached(Node target) public boolean isTargetReached(Node<int[][]> 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; return false;
} }
@ -38,9 +36,9 @@ public class EightPuzzleNode implements Node
} }
@Override @Override
public List<Node> generateSuccessors() public List<Node<int[][]>> generateSuccessors()
{ {
var successors = new ArrayList<Node>(); var successors = new ArrayList<Node<int[][]>>();
var emptyPosition = Objects.requireNonNull(detectEmptyPosition()); var emptyPosition = Objects.requireNonNull(detectEmptyPosition());
int x = emptyPosition.getRight(); int x = emptyPosition.getRight();
int y = emptyPosition.getLeft(); int y = emptyPosition.getLeft();
@ -58,25 +56,25 @@ public class EightPuzzleNode implements Node
tmp = newState[y-1][x]; tmp = newState[y-1][x];
newState[y-1][x] = newState[y][x]; newState[y-1][x] = newState[y][x];
newState[y][x] = tmp; newState[y][x] = tmp;
successors.add(new EightPuzzleNode(newState)); successors.add(new EightPuzzleNode(newState, this));
break; break;
case RIGHT: case RIGHT:
tmp = newState[y][x+1]; tmp = newState[y][x+1];
newState[y][x+1] = newState[y][x]; newState[y][x+1] = newState[y][x];
newState[y][x] = tmp; newState[y][x] = tmp;
successors.add(new EightPuzzleNode(newState)); successors.add(new EightPuzzleNode(newState, this));
break; break;
case DOWN: case DOWN:
tmp = newState[y+1][x]; tmp = newState[y+1][x];
newState[y+1][x] = newState[y][x]; newState[y+1][x] = newState[y][x];
newState[y][x] = tmp; newState[y][x] = tmp;
successors.add(new EightPuzzleNode(newState)); successors.add(new EightPuzzleNode(newState, this));
break; break;
case LEFT: case LEFT:
tmp = newState[y][x-1]; tmp = newState[y][x-1];
newState[y][x-1] = newState[y][x]; newState[y][x-1] = newState[y][x];
newState[y][x] = tmp; newState[y][x] = tmp;
successors.add(new EightPuzzleNode(newState)); successors.add(new EightPuzzleNode(newState, this));
break; break;
} }
} }
@ -88,12 +86,8 @@ public class EightPuzzleNode implements Node
return successors; return successors;
} }
public int[][] getState() @Override
{ protected boolean isValidValue(int[][] state)
return state;
}
private boolean isValidState(int[][] state)
{ {
var numbers = Arrays.stream(state).flatMapToInt(IntStream::of).toArray(); var numbers = Arrays.stream(state).flatMapToInt(IntStream::of).toArray();
@ -112,11 +106,11 @@ public class EightPuzzleNode implements Node
private IntPair detectEmptyPosition() 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); return new IntPair(row, col);
} }
@ -132,7 +126,7 @@ public class EightPuzzleNode implements Node
for (int y = 0; y < copy.length; y++) 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; return copy;
@ -141,9 +135,9 @@ public class EightPuzzleNode implements Node
@Override @Override
public String toString() 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"); builder.append(Arrays.toString(row)).append("\n");
} }

View File

@ -2,8 +2,44 @@ package search.breadthfirstsearch;
import java.util.List; import java.util.List;
public interface Node public abstract class Node<T>
{ {
boolean isTargetReached(Node target); protected T value;
List<Node> generateSuccessors(); private Node<T> parent;
protected Node(T value)
{
this(value, null);
}
protected Node(T value, Node<T> parent)
{
if (!isValidValue(value))
{
throw new IllegalArgumentException("Illegal node value");
}
this.value = value;
this.parent = parent;
}
public T getValue()
{
return this.value;
}
public Node<T> getParent()
{
return this.parent;
}
@Override
public String toString()
{
return this.value.toString();
}
protected abstract boolean isValidValue(T value);
public abstract boolean isTargetReached(Node<T> target);
public abstract List<Node<T>> generateSuccessors();
} }

View File

@ -5,8 +5,6 @@ import org.junit.jupiter.api.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class BreadthFirstSearchTest class BreadthFirstSearchTest
{ {
@Test @Test
@ -19,7 +17,6 @@ class BreadthFirstSearchTest
}; };
var root = new EightPuzzleNode(state); var root = new EightPuzzleNode(state);
int[][] targetState = { int[][] targetState = {
{1, 2, 3}, {1, 2, 3},
{4, 5, 6}, {4, 5, 6},
@ -32,4 +29,9 @@ class BreadthFirstSearchTest
System.out.println("Target: " + Arrays.deepToString(targetState)); System.out.println("Target: " + Arrays.deepToString(targetState));
System.out.println("Actual:\n" + actual); System.out.println("Actual:\n" + actual);
} }
private void printSolution(Node<int[][]> targetNode)
{
}
} }

View File

@ -3,9 +3,6 @@ package search.breadthfirstsearch;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
class EightPuzzleNodeTest class EightPuzzleNodeTest
{ {
@Test @Test
@ -120,8 +117,8 @@ class EightPuzzleNodeTest
{0, 5, 8} {0, 5, 8}
}; };
Assertions.assertArrayEquals(newState1, ((EightPuzzleNode) successors.get(0)).getState()); Assertions.assertArrayEquals(newState1, successors.get(0).getValue());
Assertions.assertArrayEquals(newState2, ((EightPuzzleNode) successors.get(1)).getState()); Assertions.assertArrayEquals(newState2, successors.get(1).getValue());
Assertions.assertArrayEquals(newState3, ((EightPuzzleNode) successors.get(2)).getState()); Assertions.assertArrayEquals(newState3, successors.get(2).getValue());
} }
} }