Reformat code

This commit is contained in:
niklas committed 2026-11-12 13:52:04 +01:00
1 parent 51c12cc0ea
commit 60c654936a
31 files changed
+262 -466

No files matched your search

+6 -12
View File
@@ -3,11 +3,9 @@ package search;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class ChessKnightNodeTest
{
public class ChessKnightNodeTest {
@Test
public void shouldReturnTrueWhenTargetReached()
{
public void shouldReturnTrueWhenTargetReached() {
final var state = new ChessKnightNode.KnightPair('a', '1');
final var node = new ChessKnightNode(state);
@@ -17,8 +15,7 @@ public class ChessKnightNodeTest
}
@Test
public void shouldReturnFalseWhenTargetNotReached()
{
public void shouldReturnFalseWhenTargetNotReached() {
final var actualState = new ChessKnightNode.KnightPair('a', '1');
final var targetState = new ChessKnightNode.KnightPair('h', '8');
@@ -30,8 +27,7 @@ public class ChessKnightNodeTest
}
@Test
public void shouldReturnNonEmptyListOfSuccessors()
{
public void shouldReturnNonEmptyListOfSuccessors() {
final var state = new ChessKnightNode.KnightPair('a', '1');
final var node = new ChessKnightNode(state);
@@ -41,8 +37,7 @@ public class ChessKnightNodeTest
}
@Test
public void shouldReturnCorrectSuccessorsWithMaxPossibleSuccessors()
{
public void shouldReturnCorrectSuccessorsWithMaxPossibleSuccessors() {
final var state = new ChessKnightNode.KnightPair('f', '3');
final var node = new ChessKnightNode(state);
@@ -71,8 +66,7 @@ public class ChessKnightNodeTest
}
@Test
public void shouldReturnCorrectSuccessorsOnEdge()
{
public void shouldReturnCorrectSuccessorsOnEdge() {
final var state = new ChessKnightNode.KnightPair('a', '1');
final var node = new ChessKnightNode(state);
+3 -6
View File
@@ -9,11 +9,9 @@ import java.util.List;
import static search.SearchTestUtils.countNodes;
import static search.SearchTestUtils.printSolution;
public class ChessKnightTest
{
public class ChessKnightTest {
@Test
public void shouldReturnOne()
{
public void shouldReturnOne() {
final var state = new ChessKnightNode.KnightPair('a', '3');
final var root = new ChessKnightNode(state);
@@ -31,8 +29,7 @@ public class ChessKnightTest
}
@Test
public void shouldReturnThree()
{
public void shouldReturnThree() {
final var state = new ChessKnightNode.KnightPair('a', '1');
final var root = new ChessKnightNode(state);
+5 -10
View File
@@ -3,11 +3,9 @@ package search;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class EightPuzzleNodeTest
{
class EightPuzzleNodeTest {
@Test
public void shouldReturnTrueWhenTargetReached()
{
public void shouldReturnTrueWhenTargetReached() {
final int[][] state = {
{1, 2, 3},
{4, 5, 6},
@@ -21,8 +19,7 @@ class EightPuzzleNodeTest
}
@Test
public void shouldReturnFalseWhenTargetNotReached()
{
public void shouldReturnFalseWhenTargetNotReached() {
final int[][] actualState = {
{7, 1, 6},
{0, 4, 2},
@@ -42,8 +39,7 @@ class EightPuzzleNodeTest
}
@Test
public void shouldReturnNonEmptyListOfSuccessors()
{
public void shouldReturnNonEmptyListOfSuccessors() {
final int[][] state = {
{7, 1, 6},
{0, 4, 2},
@@ -57,8 +53,7 @@ class EightPuzzleNodeTest
}
@Test
public void shouldReturnCorrectSuccessors()
{
public void shouldReturnCorrectSuccessors() {
final int[][] state = {
{7, 1, 6},
{0, 4, 2},
+5 -9
View File
@@ -5,13 +5,11 @@ import search.uninformed.iterativedeepening.IterativeDeepening;
import static search.SearchTestUtils.printSolution;
public class GameHelperTest
{
public class GameHelperTest {
@Test
void shouldReturnCorrectTargetDemonologist()
{
void shouldReturnCorrectTargetDemonologist() {
final int[][] state = {
{8, 4 ,0},
{8, 4, 0},
{6, 3, 1},
{2, 5, 7}
};
@@ -30,8 +28,7 @@ public class GameHelperTest
}
@Test
void shouldReturnCorrectTargetWartales()
{
void shouldReturnCorrectTargetWartales() {
final int[][] state = {
{1, 8, 6},
{3, 2, 7},
@@ -52,8 +49,7 @@ public class GameHelperTest
}
@Test
void shouldReturnCorrectTargetLabyrinthine()
{
void shouldReturnCorrectTargetLabyrinthine() {
final boolean[][] state = {
{true, false, true},
{false, true, false},
+6 -12
View File
@@ -3,11 +3,9 @@ package search;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class LabyrinthineNodeTest
{
class LabyrinthineNodeTest {
@Test
public void shouldReturnTrueWhenTargetReached()
{
public void shouldReturnTrueWhenTargetReached() {
final boolean[][] state = {
{true, true, true},
{true, true, true},
@@ -21,8 +19,7 @@ class LabyrinthineNodeTest
}
@Test
public void shouldReturnFalseWhenTargetNotReached()
{
public void shouldReturnFalseWhenTargetNotReached() {
final boolean[][] actualState = {
{true, false, true},
{false, true, false},
@@ -42,8 +39,7 @@ class LabyrinthineNodeTest
}
@Test
public void shouldReturnNonEmptyListOfSuccessors()
{
public void shouldReturnNonEmptyListOfSuccessors() {
final boolean[][] state = {
{true, false, true},
{false, true, false},
@@ -57,8 +53,7 @@ class LabyrinthineNodeTest
}
@Test
public void shouldReturnCorrectSuccessor()
{
public void shouldReturnCorrectSuccessor() {
final boolean[][] state = {
{true, false, true},
{true, true, true},
@@ -82,8 +77,7 @@ class LabyrinthineNodeTest
}
@Test
public void shouldReturnCorrectSuccessors()
{
public void shouldReturnCorrectSuccessors() {
final boolean[][] state = {
{true, false, true},
{true, true, false},
+5 -10
View File
@@ -1,16 +1,13 @@
package search;
public class SearchTestUtils
{
public static <T> void printSolution(final Node<T> targetNode)
{
public class SearchTestUtils {
public static <T> void printSolution(final Node<T> targetNode) {
var node = targetNode;
System.out.println("Read from down to top!");
System.out.println("END");
while (node != null)
{
while (node != null) {
System.out.println(node);
node = node.getParent();
}
@@ -18,13 +15,11 @@ public class SearchTestUtils
System.out.println("START");
}
public static <T> int countNodes(final Node<T> targetNode)
{
public static <T> int countNodes(final Node<T> targetNode) {
var node = targetNode;
int nodeCount = 0;
while (node != null)
{
while (node != null) {
nodeCount++;
node = node.getParent();
}
+14 -28
View File
@@ -7,11 +7,9 @@ import search.Node;
import static search.SearchTestUtils.printSolution;
class AStarTest
{
class AStarTest {
@Test
void shouldReturnCorrectTargetCubekNodeHeuristic1()
{
void shouldReturnCorrectTargetCubekNodeHeuristic1() {
final int[][] state = {
{2, 0, 4},
{6, 7, 1},
@@ -32,8 +30,7 @@ class AStarTest
}
@Test
void shouldReturnCorrectTargetCubekNodeHeuristic2()
{
void shouldReturnCorrectTargetCubekNodeHeuristic2() {
final int[][] state = {
{2, 0, 4},
{6, 7, 1},
@@ -53,18 +50,14 @@ class AStarTest
printSolution(actual);
}
private int h1(final Node<int[][]> node, final Node<int[][]> target)
{
private int h1(final Node<int[][]> node, final Node<int[][]> target) {
final var value = node.getValue();
final var targetValue = target.getValue();
var counter = 0;
for (var row = 0; row < value.length; row++)
{
for (var col = 0; col < value[row].length; col++)
{
if (value[row][col] != 0 && value[row][col] != targetValue[row][col])
{
for (var row = 0; row < value.length; row++) {
for (var col = 0; col < value[row].length; col++) {
if (value[row][col] != 0 && value[row][col] != targetValue[row][col]) {
counter++;
}
}
@@ -73,19 +66,16 @@ class AStarTest
return counter;
}
private int h2(final Node<int[][]> node, final Node<int[][]> target)
{
private int h2(final Node<int[][]> node, final Node<int[][]> target) {
final var value = node.getValue();
final var targetValue = target.getValue();
var manhattanDistance = 0;
for (int i = 1; i <= 8; i++)
{
for (int i = 1; i <= 8; i++) {
final var targetPos = detectPositionOf(i, targetValue);
final var actualPos = detectPositionOf(i, value);
if (targetPos != null && actualPos != null)
{
if (targetPos != null && actualPos != null) {
final var xDistance = Math.abs(targetPos.x() - actualPos.x());
final var yDistance = Math.abs(targetPos.y() - actualPos.y());
@@ -96,14 +86,10 @@ class AStarTest
return manhattanDistance;
}
private IntPair detectPositionOf(final int i, final int[][] value)
{
for (var row = 0; row < value.length; row++)
{
for (var col = 0; col < value[row].length; col++)
{
if (value[row][col] == i)
{
private IntPair detectPositionOf(final int i, final int[][] value) {
for (var row = 0; row < value.length; row++) {
for (var col = 0; col < value[row].length; col++) {
if (value[row][col] == i) {
return new IntPair(col, row);
}
}
@@ -7,11 +7,9 @@ import java.util.List;
import static search.SearchTestUtils.printSolution;
class BreadthFirstSearchTest
{
class BreadthFirstSearchTest {
@Test
void shouldReturnCorrectTarget()
{
void shouldReturnCorrectTarget() {
final int[][] state = {
{5, 0, 3},
{2, 1, 6},
@@ -32,8 +30,7 @@ class BreadthFirstSearchTest
}
@Test
void shouldReturnCorrectTargetCubekNode()
{
void shouldReturnCorrectTargetCubekNode() {
final int[][] state = {
{2, 0, 4},
{6, 7, 1},
@@ -5,11 +5,9 @@ import search.EightPuzzleNode;
import static search.SearchTestUtils.printSolution;
class DepthFirstSearchTest
{
class DepthFirstSearchTest {
@Test
void shouldReturnCorrectTarget()
{
void shouldReturnCorrectTarget() {
final int[][] state = {
{1, 2, 3},
{4, 5, 6},
@@ -5,11 +5,9 @@ import search.EightPuzzleNode;
import static search.SearchTestUtils.printSolution;
class IterativeDeepeningTest
{
class IterativeDeepeningTest {
@Test
void shouldReturnCorrectTargetEightPuzzle()
{
void shouldReturnCorrectTargetEightPuzzle() {
final int[][] state = {
{5, 0, 3},
{2, 1, 6},
@@ -30,8 +28,7 @@ class IterativeDeepeningTest
}
@Test
void shouldReturnCorrectTargetCubekNodeEightPuzzle()
{
void shouldReturnCorrectTargetCubekNodeEightPuzzle() {
final int[][] state = {
{2, 0, 4},
{6, 7, 1},