diff --git a/src/search/Direction.java b/src/search/Direction.java new file mode 100644 index 0000000..19fb73a --- /dev/null +++ b/src/search/Direction.java @@ -0,0 +1,6 @@ +package search; + +enum Direction +{ + TOP, RIGHT, DOWN, LEFT +} diff --git a/src/search/EightPuzzleNode.java b/src/search/EightPuzzleNode.java index 47712b5..d6beffd 100644 --- a/src/search/EightPuzzleNode.java +++ b/src/search/EightPuzzleNode.java @@ -28,8 +28,8 @@ public class EightPuzzleNode extends Node { final var successors = new ArrayList>(); final var emptyPosition = Objects.requireNonNull(detectEmptyPosition()); - final var x = emptyPosition.getX(); - final var y = emptyPosition.getY(); + final var x = emptyPosition.x(); + final var y = emptyPosition.y(); for (final var dir : Direction.values()) { @@ -124,13 +124,8 @@ public class EightPuzzleNode extends Node private void swapStateField(final int[][] newState, final IntPair emptyPos, final IntPair posToSwap) { - final var tmp = newState[posToSwap.getY()][posToSwap.getX()]; - newState[posToSwap.getY()][posToSwap.getX()] = newState[emptyPos.getY()][emptyPos.getX()]; - newState[emptyPos.getY()][emptyPos.getX()] = tmp; - } - - private enum Direction - { - TOP, RIGHT, DOWN, LEFT + final var tmp = newState[posToSwap.y()][posToSwap.x()]; + newState[posToSwap.y()][posToSwap.x()] = newState[emptyPos.y()][emptyPos.x()]; + newState[emptyPos.y()][emptyPos.x()] = tmp; } } diff --git a/src/search/IntPair.java b/src/search/IntPair.java index 1317336..7ed8b8c 100644 --- a/src/search/IntPair.java +++ b/src/search/IntPair.java @@ -1,23 +1,5 @@ package search; -public class IntPair +public record IntPair(int x, int y) { - private final int x; - private final int y; - - public IntPair(final int x, final int y) - { - this.x = x; - this.y = y; - } - - public int getX() - { - return x; - } - public int getY() - { - return y; - } - } diff --git a/test/search/LabyrinthineNodeTest.java b/test/search/LabyrinthineNodeTest.java new file mode 100644 index 0000000..67ce6cb --- /dev/null +++ b/test/search/LabyrinthineNodeTest.java @@ -0,0 +1,147 @@ +package search; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import search.uninformed.iterativedeepening.IterativeDeepening; + +import static search.SearchTestUtils.printSolution; + +class LabyrinthineNodeTest +{ + @Test + public void shouldReturnTrueWhenTargetReached() + { + final boolean[][] state = { + {true, true, true}, + {true, true, true}, + {true, true, true}, + }; + + final var node = new LabyrinthineNode(state); + final var target = new LabyrinthineNode(state); + + Assertions.assertTrue(node.isTargetReached(target)); + } + + @Test + public void shouldReturnFalseWhenTargetNotReached() + { + final boolean[][] actualState = { + {true, false, true}, + {false, true, false}, + {true, false, true}, + }; + + final boolean[][] targetState = { + {true, true, true}, + {true, true, true}, + {true, true, true}, + }; + + final var node = new LabyrinthineNode(actualState); + final var target = new LabyrinthineNode(targetState); + + Assertions.assertFalse(node.isTargetReached(target)); + } + + @Test + public void shouldReturnNonEmptyListOfSuccessors() + { + final boolean[][] state = { + {true, false, true}, + {false, true, false}, + {true, false, true}, + }; + + final var node = new LabyrinthineNode(state); + final var successors = node.generateSuccessors(); + + Assertions.assertFalse(successors.isEmpty()); + } + + @Test + public void shouldReturnCorrectSuccessor() + { + final boolean[][] state = { + {true, false, true}, + {true, true, true}, + {true, true, true}, + }; + + final var node = new LabyrinthineNode(state); + final var successors = node.generateSuccessors(); + + Assertions.assertEquals(1, successors.size()); + + final boolean[][] targetSuccessor = { + {false, true, false}, + {true, false, true}, + {true, true, true}, + }; + + System.out.println(successors.get(0)); + + Assertions.assertArrayEquals(targetSuccessor, successors.get(0).getValue()); + } + + @Test + public void shouldReturnCorrectSuccessors() + { + final boolean[][] state = { + {true, false, true}, + {true, true, false}, + {false, true, true} + }; + + final var node = new LabyrinthineNode(state); + final var successors = node.generateSuccessors(); + + Assertions.assertEquals(3, successors.size()); + + final boolean[][] targetSuccessor1 = { + {false, true, false}, + {true, false, false}, + {false, true, true} + }; + + final boolean[][] targetSuccessor2 = { + {true, false, false}, + {true, false, true}, + {false, true, false} + }; + + final boolean[][] targetSuccessor3 = { + {true, false, true}, + {false, true, false}, + {true, false, true} + }; + + Assertions.assertArrayEquals(targetSuccessor1, successors.get(0).getValue()); + Assertions.assertArrayEquals(targetSuccessor2, successors.get(1).getValue()); + Assertions.assertArrayEquals(targetSuccessor3, successors.get(2).getValue()); + } + + @Test + void shouldReturnCorrectTarget() + { + final boolean[][] state = { + {true, false, true}, + {false, true, false}, + {true, false, true} + }; + + final var root = new LabyrinthineNode(state); + + final boolean[][] targetState = { + {true, true, true}, + {true, true, true}, + {true, true, true}, + }; + + final var expected = new LabyrinthineNode(targetState); + + final var actual = new IterativeDeepening().iterativeDeepening(root, expected); + + printSolution(actual); + } +} \ No newline at end of file diff --git a/test/search/heuristic/AStarTest.java b/test/search/heuristic/AStarTest.java index 1e96bb9..b6ad36f 100644 --- a/test/search/heuristic/AStarTest.java +++ b/test/search/heuristic/AStarTest.java @@ -86,8 +86,8 @@ class AStarTest if (targetPos != null && actualPos != null) { - final var xDistance = Math.abs(targetPos.getX() - actualPos.getX()); - final var yDistance = Math.abs(targetPos.getY() - actualPos.getY()); + final var xDistance = Math.abs(targetPos.x() - actualPos.x()); + final var yDistance = Math.abs(targetPos.y() - actualPos.y()); manhattanDistance += xDistance + yDistance; }