Fix stuff; add new problem "Labyrinthine puzzle" from game Labyrinthine Chapter I

This commit is contained in:
Niklas Birk 2021-04-12 16:11:46 +02:00
parent 6937e28cdd
commit 3fed93055f
5 changed files with 161 additions and 31 deletions

View File

@ -0,0 +1,6 @@
package search;
enum Direction
{
TOP, RIGHT, DOWN, LEFT
}

View File

@ -28,8 +28,8 @@ public class EightPuzzleNode extends Node<int[][]>
{
final var successors = new ArrayList<Node<int[][]>>();
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<int[][]>
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;
}
}

View File

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

View File

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

View File

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