From d4e54493c7b73168afd1157098c411cc371ae91f Mon Sep 17 00:00:00 2001 From: Niklas Birk Date: Tue, 2 Apr 2019 20:34:15 +0200 Subject: [PATCH] Made all variables and parameter final where available --- src/Main.java | 6 -- src/search/EightPuzzleNode.java | 66 +++++++------------ src/search/Node.java | 14 ++-- src/search/heuristic/AStar.java | 5 ++ .../BreadthFirstSearch.java | 6 +- .../depthfirstsearch/DepthFirstSearch.java | 6 +- .../IterativeDeepening.java | 8 +-- test/search/EightPuzzleNodeTest.java | 41 +++++------- test/search/SearchTestUtils.java | 2 +- test/search/heuristic/AStarTest.java | 8 +++ .../BreadthFirstSearchTest.java | 20 +++--- .../DepthFirstSearchTest.java | 10 +-- .../IterativeDeepeningTest.java | 20 +++--- 13 files changed, 95 insertions(+), 117 deletions(-) delete mode 100644 src/Main.java create mode 100644 src/search/heuristic/AStar.java create mode 100644 test/search/heuristic/AStarTest.java diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index e90a4ee..0000000 --- a/src/Main.java +++ /dev/null @@ -1,6 +0,0 @@ -public class Main -{ - public static void main(String[] args) - { - } -} diff --git a/src/search/EightPuzzleNode.java b/src/search/EightPuzzleNode.java index e2ad6c1..9bac00a 100644 --- a/src/search/EightPuzzleNode.java +++ b/src/search/EightPuzzleNode.java @@ -4,22 +4,21 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.stream.IntStream; public class EightPuzzleNode extends Node { - public EightPuzzleNode(int[][] state) + public EightPuzzleNode(final int[][] state) { super(state); } - private EightPuzzleNode(int[][] value, Node parent) + private EightPuzzleNode(final int[][] value, final Node parent) { super(value, parent); } @Override - public boolean isTargetReached(Node target) + public boolean isTargetReached(final Node target) { return valueEquals(target); } @@ -27,25 +26,25 @@ public class EightPuzzleNode extends Node @Override public List> generateSuccessors() { - var successors = new ArrayList>(); - var emptyPosition = Objects.requireNonNull(detectEmptyPosition()); - int x = emptyPosition.getX(); - int y = emptyPosition.getY(); + final var successors = new ArrayList>(); + final var emptyPosition = Objects.requireNonNull(detectEmptyPosition()); + final int x = emptyPosition.getX(); + final int y = emptyPosition.getY(); - for (Direction dir : Direction.values()) + for (final Direction dir : Direction.values()) { - var newState = copyOfState(); + final var newState = copyOfState(); try { - var posToSwap = switch (dir) { + final var posToSwap = switch (dir) { case TOP -> new IntPair(x, y-1); case RIGHT -> new IntPair(x+1, y); case DOWN -> new IntPair(x, y+1); case LEFT -> new IntPair(x-1, y); }; - var successor = this.swapStateField(newState, emptyPosition, posToSwap); + final var successor = this.swapStateField(newState, emptyPosition, posToSwap); if (!successor.valueEquals(this) && !successor.valueEquals(super.getParent())) { @@ -53,7 +52,7 @@ public class EightPuzzleNode extends Node } } - catch (ArrayIndexOutOfBoundsException ignored) + catch (final ArrayIndexOutOfBoundsException ignored) { } } @@ -61,20 +60,12 @@ public class EightPuzzleNode extends Node return successors; } - @Override - protected boolean isValidParameterValue(int[][] state) - { - var numbers = Arrays.stream(state).flatMapToInt(IntStream::of).toArray(); - - return numbersAreInAllowedRange(numbers) && numbersAreUnique(numbers); - } - @Override public String toString() { - StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); - for (int[] row : super.value) + for (final int[] row : super.value) { builder.append(Arrays.toString(row)).append("\n"); } @@ -82,7 +73,7 @@ public class EightPuzzleNode extends Node return builder.toString(); } - private boolean valueEquals(Node node) + private boolean valueEquals(final Node node) { if (node == null) { @@ -103,16 +94,6 @@ public class EightPuzzleNode extends Node return true; } - private boolean numbersAreInAllowedRange(int[] numbers) - { - return Arrays.stream(numbers).min().getAsInt() == 0 && Arrays.stream(numbers).max().getAsInt() == 8; - } - - private boolean numbersAreUnique(int[] numbers) - { - return Arrays.stream(numbers).count() == Arrays.stream(numbers).distinct().count(); - } - private IntPair detectEmptyPosition() { for (int row = 0; row < super.value.length; row++) @@ -131,7 +112,7 @@ public class EightPuzzleNode extends Node private int[][] copyOfState() { - var copy = new int[3][3]; + final var copy = new int[3][3]; for (int y = 0; y < copy.length; y++) { @@ -141,13 +122,13 @@ public class EightPuzzleNode extends Node return copy; } - private EightPuzzleNode swapStateField(int[][] newState, IntPair emptyPos, IntPair posToSwap) + private EightPuzzleNode swapStateField(final int[][] newState, final IntPair emptyPos, final IntPair posToSwap) { - int posToSwapX = posToSwap.getX(); - int postToSwapY = posToSwap.getY(); - int emptyX = emptyPos.getX(); - int emptyY = emptyPos.getY(); - int tmp; + final int posToSwapX = posToSwap.getX(); + final int postToSwapY = posToSwap.getY(); + final int emptyX = emptyPos.getX(); + final int emptyY = emptyPos.getY(); + final int tmp; tmp = newState[postToSwapY][posToSwapX]; newState[postToSwapY][posToSwapX] = newState[emptyY][emptyX]; @@ -161,7 +142,7 @@ public class EightPuzzleNode extends Node private final int x; private final int y; - public IntPair(int x, int y) + public IntPair(final int x, final int y) { this.x = x; this.y = y; @@ -177,6 +158,7 @@ public class EightPuzzleNode extends Node } } + private enum Direction { TOP, RIGHT, DOWN, LEFT diff --git a/src/search/Node.java b/src/search/Node.java index db163a8..fb97094 100644 --- a/src/search/Node.java +++ b/src/search/Node.java @@ -4,21 +4,16 @@ import java.util.List; public abstract class Node { - protected T value; - private Node parent; + protected final T value; + private final Node parent; - protected Node(T value) + protected Node(final T value) { this(value, null); } - protected Node(T value, Node parent) + protected Node(final T value, final Node parent) { - if (!isValidParameterValue(value)) - { - throw new IllegalArgumentException("Illegal node value"); - } - this.value = value; this.parent = parent; } @@ -33,7 +28,6 @@ public abstract class Node return this.parent; } - protected abstract boolean isValidParameterValue(T value); public abstract boolean isTargetReached(Node target); public abstract List> generateSuccessors(); } diff --git a/src/search/heuristic/AStar.java b/src/search/heuristic/AStar.java new file mode 100644 index 0000000..8ca5914 --- /dev/null +++ b/src/search/heuristic/AStar.java @@ -0,0 +1,5 @@ +package search.heuristic; + +public class AStar +{ +} diff --git a/src/search/uninformed/breadthfirstsearch/BreadthFirstSearch.java b/src/search/uninformed/breadthfirstsearch/BreadthFirstSearch.java index 097aa66..12aaff5 100644 --- a/src/search/uninformed/breadthfirstsearch/BreadthFirstSearch.java +++ b/src/search/uninformed/breadthfirstsearch/BreadthFirstSearch.java @@ -7,11 +7,11 @@ import java.util.List; public class BreadthFirstSearch { - public Node breadthFirstSearch(List> nodes, Node target) + public Node breadthFirstSearch(final List> nodes, final Node target) { - var newNodes = new ArrayList>(); + final var newNodes = new ArrayList>(); - for (Node node : nodes) + for (final Node node : nodes) { if (node.isTargetReached(target)) { diff --git a/src/search/uninformed/depthfirstsearch/DepthFirstSearch.java b/src/search/uninformed/depthfirstsearch/DepthFirstSearch.java index 0e98c42..8a514e7 100644 --- a/src/search/uninformed/depthfirstsearch/DepthFirstSearch.java +++ b/src/search/uninformed/depthfirstsearch/DepthFirstSearch.java @@ -4,18 +4,18 @@ import search.Node; public class DepthFirstSearch { - public Node depthFirstSearch(Node node, Node target) + public Node depthFirstSearch(final Node node, final Node target) { if (node.isTargetReached(target)) { return node; } - var newNodes = node.generateSuccessors(); + final var newNodes = node.generateSuccessors(); while (!newNodes.isEmpty()) { - var resultNode = depthFirstSearch(newNodes.get(0), target); + final var resultNode = depthFirstSearch(newNodes.get(0), target); if (resultNode != null) { diff --git a/src/search/uninformed/iterativedeepening/IterativeDeepening.java b/src/search/uninformed/iterativedeepening/IterativeDeepening.java index ac6089b..70cde89 100644 --- a/src/search/uninformed/iterativedeepening/IterativeDeepening.java +++ b/src/search/uninformed/iterativedeepening/IterativeDeepening.java @@ -4,7 +4,7 @@ import search.Node; public class IterativeDeepening { - public Node iterativeDeepening(Node node, Node target) + public Node iterativeDeepening(final Node node, final Node target) { int lowBarrier = 0; @@ -19,18 +19,18 @@ public class IterativeDeepening return resultNode; } - private Node depthFirstSearch(Node node, Node target, int depth, int barrier) + private Node depthFirstSearch(final Node node, final Node target, int depth, final int barrier) { if (node.isTargetReached(target)) { return node; } - var newNodes = node.generateSuccessors(); + final var newNodes = node.generateSuccessors(); while (!newNodes.isEmpty() && depth < barrier) { - var resultNode = depthFirstSearch(newNodes.get(0), target, ++depth, barrier); + final var resultNode = depthFirstSearch(newNodes.get(0), target, ++depth, barrier); if (resultNode != null) { diff --git a/test/search/EightPuzzleNodeTest.java b/test/search/EightPuzzleNodeTest.java index 98bd855..c9e99fd 100644 --- a/test/search/EightPuzzleNodeTest.java +++ b/test/search/EightPuzzleNodeTest.java @@ -9,7 +9,7 @@ class EightPuzzleNodeTest @Test public void shouldThrowExceptionWhileStateHasDuplicateNumbers() { - int[][] state = { + final int[][] state = { {1, 1, 3}, {4, 5, 6}, {7, 8, 0} @@ -21,7 +21,7 @@ class EightPuzzleNodeTest @Test public void shouldThrowExceptionWhileStateHasNumbersOutOfRange() { - int[][] state = { + final int[][] state = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} @@ -33,14 +33,14 @@ class EightPuzzleNodeTest @Test public void shouldReturnTrueWhenTargetReached() { - int[][] state = { + final int[][] state = { {1, 2, 3}, {4, 5, 6}, {7, 8, 0} }; - var node = new EightPuzzleNode(state); - var target = new EightPuzzleNode(state); + final var node = new EightPuzzleNode(state); + final var target = new EightPuzzleNode(state); Assertions.assertTrue(node.isTargetReached(target)); } @@ -48,20 +48,20 @@ class EightPuzzleNodeTest @Test public void shouldReturnFalseWhenTargetNotReached() { - int[][] actualState = { + final int[][] actualState = { {7, 1, 6}, {0, 4, 2}, {3, 5, 8} }; - int[][] targetState = { + final int[][] targetState = { {1, 2, 3}, {4, 5, 6}, {7, 8, 0} }; - var node = new EightPuzzleNode(actualState); - var target = new EightPuzzleNode(targetState); + final var node = new EightPuzzleNode(actualState); + final var target = new EightPuzzleNode(targetState); Assertions.assertFalse(node.isTargetReached(target)); } @@ -69,50 +69,45 @@ class EightPuzzleNodeTest @Test public void shouldReturnNonEmptyListOfSuccessors() { - int[][] state = { + final int[][] state = { {7, 1, 6}, {0, 4, 2}, {3, 5, 8} }; - var node = new EightPuzzleNode(state); - var successors = node.generateSuccessors(); + final var node = new EightPuzzleNode(state); + final var successors = node.generateSuccessors(); Assertions.assertFalse(successors.isEmpty()); } - @Test - public void shouldReturnEmptyListOfSuccessors() - { - } - @Test public void shouldReturnCorrectSuccessors() { - int[][] state = { + final int[][] state = { {7, 1, 6}, {0, 4, 2}, {3, 5, 8} }; - var node = new EightPuzzleNode(state); - var successors = node.generateSuccessors(); + final var node = new EightPuzzleNode(state); + final var successors = node.generateSuccessors(); Assertions.assertEquals(3, successors.size()); - int[][] newState1 = { + final int[][] newState1 = { {0, 1, 6}, {7, 4, 2}, {3, 5, 8} }; - int[][] newState2 = { + final int[][] newState2 = { {7, 1, 6}, {4, 0, 2}, {3, 5, 8} }; - int[][] newState3 = { + final int[][] newState3 = { {7, 1, 6}, {3, 4, 2}, {0, 5, 8} diff --git a/test/search/SearchTestUtils.java b/test/search/SearchTestUtils.java index 1bedc34..a3c85a4 100644 --- a/test/search/SearchTestUtils.java +++ b/test/search/SearchTestUtils.java @@ -2,7 +2,7 @@ package search; public class SearchTestUtils { - public static void printSolution(Node targetNode) + public static void printSolution(final Node targetNode) { var node = targetNode; diff --git a/test/search/heuristic/AStarTest.java b/test/search/heuristic/AStarTest.java new file mode 100644 index 0000000..071c42e --- /dev/null +++ b/test/search/heuristic/AStarTest.java @@ -0,0 +1,8 @@ +package search.heuristic; + +import static org.junit.jupiter.api.Assertions.*; + +class AStarTest +{ + +} \ No newline at end of file diff --git a/test/search/uninformed/breadthfirstsearch/BreadthFirstSearchTest.java b/test/search/uninformed/breadthfirstsearch/BreadthFirstSearchTest.java index ce5e6f4..06d99dd 100644 --- a/test/search/uninformed/breadthfirstsearch/BreadthFirstSearchTest.java +++ b/test/search/uninformed/breadthfirstsearch/BreadthFirstSearchTest.java @@ -12,21 +12,21 @@ class BreadthFirstSearchTest @Test void shouldReturnCorrectTarget() { - int[][] state = { + final int[][] state = { {5, 0, 3}, {2, 1, 6}, {4, 7, 8} }; - var root = new EightPuzzleNode(state); + final var root = new EightPuzzleNode(state); - int[][] targetState = { + final int[][] targetState = { {1, 2, 3}, {4, 5, 6}, {7, 8, 0} }; - var expected = new EightPuzzleNode(targetState); + final var expected = new EightPuzzleNode(targetState); - var actual = new BreadthFirstSearch().breadthFirstSearch(List.of(root), expected); + final var actual = new BreadthFirstSearch().breadthFirstSearch(List.of(root), expected); printSolution(actual); } @@ -34,21 +34,21 @@ class BreadthFirstSearchTest @Test void shouldReturnCorrectTargetCubekNode() { - int[][] state = { + final int[][] state = { {2, 0, 4}, {6, 7, 1}, {8, 5, 3} }; - var root = new EightPuzzleNode(state); + final var root = new EightPuzzleNode(state); - int[][] targetState = { + final int[][] targetState = { {1, 2, 3}, {4, 5, 6}, {7, 8, 0} }; - var expected = new EightPuzzleNode(targetState); + final var expected = new EightPuzzleNode(targetState); - var actual = new BreadthFirstSearch().breadthFirstSearch(List.of(root), expected); + final var actual = new BreadthFirstSearch().breadthFirstSearch(List.of(root), expected); printSolution(actual); } diff --git a/test/search/uninformed/depthfirstsearch/DepthFirstSearchTest.java b/test/search/uninformed/depthfirstsearch/DepthFirstSearchTest.java index 68751df..4e26528 100644 --- a/test/search/uninformed/depthfirstsearch/DepthFirstSearchTest.java +++ b/test/search/uninformed/depthfirstsearch/DepthFirstSearchTest.java @@ -10,21 +10,21 @@ class DepthFirstSearchTest @Test void shouldReturnCorrectTarget() { - int[][] state = { + final int[][] state = { {1, 2, 3}, {4, 5, 6}, {7, 0, 8} }; - var root = new EightPuzzleNode(state); + final var root = new EightPuzzleNode(state); - int[][] targetState = { + final int[][] targetState = { {1, 2, 3}, {4, 5, 6}, {7, 8, 0} }; - var expected = new EightPuzzleNode(targetState); + final var expected = new EightPuzzleNode(targetState); - var actual = new DepthFirstSearch().depthFirstSearch(root, expected); + final var actual = new DepthFirstSearch().depthFirstSearch(root, expected); printSolution(actual); } diff --git a/test/search/uninformed/iterativedeepening/IterativeDeepeningTest.java b/test/search/uninformed/iterativedeepening/IterativeDeepeningTest.java index b3a5f69..5a39fa9 100644 --- a/test/search/uninformed/iterativedeepening/IterativeDeepeningTest.java +++ b/test/search/uninformed/iterativedeepening/IterativeDeepeningTest.java @@ -10,21 +10,21 @@ class IterativeDeepeningTest @Test void shouldReturnCorrectTarget() { - int[][] state = { + final int[][] state = { {5, 0, 3}, {2, 1, 6}, {4, 7, 8} }; - var root = new EightPuzzleNode(state); + final var root = new EightPuzzleNode(state); - int[][] targetState = { + final int[][] targetState = { {1, 2, 3}, {4, 5, 6}, {7, 8, 0} }; - var expected = new EightPuzzleNode(targetState); + final var expected = new EightPuzzleNode(targetState); - var actual = new IterativeDeepening().iterativeDeepening(root, expected); + final var actual = new IterativeDeepening().iterativeDeepening(root, expected); printSolution(actual); } @@ -32,21 +32,21 @@ class IterativeDeepeningTest @Test void shouldReturnCorrectTargetCubekNode() { - int[][] state = { + final int[][] state = { {2, 0, 4}, {6, 7, 1}, {8, 5, 3} }; - var root = new EightPuzzleNode(state); + final var root = new EightPuzzleNode(state); - int[][] targetState = { + final int[][] targetState = { {1, 2, 3}, {4, 5, 6}, {7, 8, 0} }; - var expected = new EightPuzzleNode(targetState); + final var expected = new EightPuzzleNode(targetState); - var actual = new IterativeDeepening().iterativeDeepening(root, expected); + final var actual = new IterativeDeepening().iterativeDeepening(root, expected); printSolution(actual); }