Skip to content

Commit ff4fe26

Browse files
committed
Added gradle; added tests
1 parent 90dd4ce commit ff4fe26

18 files changed

+509
-77
lines changed

.gitattributes

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
*.idea
22
out/**
3-
*.iml
3+
*.iml
4+
# Ignore Gradle project-specific cache directory
5+
.gradle
6+
7+
# Ignore Gradle build output directory
8+
build

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,33 @@ The implementation includes 3 files:
77
- AStar.java : Main algorithm class.
88
- Node.java : Class for the nodes used by the algorithm.
99
- AStarTest.java : Class with a main method and a simple test for the algorithm implementation
10+
11+
## Search Area
12+
13+
0 1 2 3 4 5 6
14+
0 - - - - - - -
15+
1 - - - B - - -
16+
2 - I - B - F -
17+
3 - - - B - - -
18+
4 - - - - - - -
19+
5 - - - - - - -
20+
21+
## Search Path with diagonals
22+
23+
0 1 2 3 4 5 6
24+
0 - - - * - - -
25+
1 - - * B * - -
26+
2 - I* - B - *F -
27+
3 - - - B - - -
28+
4 - - - - - - -
29+
5 - - - - - - -
30+
31+
## Search Path without diagonals
32+
33+
0 1 2 3 4 5 6
34+
0 - - * * * - -
35+
1 - - * B * - -
36+
2 - I* * B * *F -
37+
3 - - - B - - -
38+
4 - - - - - - -
39+
5 - - - - - - -

app/build.gradle.kts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
6+
* User Manual available at https://docs.gradle.org/7.6/userguide/building_java_projects.html
7+
*/
8+
9+
plugins {
10+
// Apply the application plugin to add support for building a CLI application in Java.
11+
application
12+
}
13+
14+
repositories {
15+
// Use Maven Central for resolving dependencies.
16+
mavenCentral()
17+
}
18+
19+
dependencies {
20+
// Use JUnit Jupiter for testing.
21+
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
22+
23+
// This dependency is used by the application.
24+
implementation("com.google.guava:guava:31.1-jre")
25+
26+
testImplementation("org.hamcrest:hamcrest:2.2")
27+
}
28+
29+
application {
30+
// Define the main class for the application.
31+
mainClass.set("com.ai.astar.App")
32+
}
33+
34+
tasks.named<Test>("test") {
35+
// Use JUnit Platform for unit tests.
36+
useJUnitPlatform()
37+
}

src/com/ai/astar/AStar.java renamed to app/src/main/java/com/ai/astar/domain/AStar.java

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package com.ai.astar;
1+
package com.ai.astar.domain;
22

3-
import com.ai.astar.domain.Node;
43
import com.ai.astar.domain.searchstrategy.DiagonalMapChecker;
54
import com.ai.astar.domain.searchstrategy.HorizontalVerticalChecker;
65
import com.ai.astar.domain.searchstrategy.MapChecker;
@@ -19,7 +18,15 @@ public class AStar {
1918
private final MapChecker diagonalsChecker;
2019
private final MapChecker hvChecker;
2120

22-
public AStar(int rows, int cols, Node initialNode, Node finalNode, int[][] blocksArray, boolean searchDiagonals) {
21+
public AStar(
22+
int rows,
23+
int cols,
24+
Node initialNode,
25+
Node finalNode,
26+
int[][] blocksArray,
27+
boolean searchDiagonals,
28+
int diagonalCost,
29+
int hvCost) {
2330
this.initialNode = initialNode;
2431
this.finalNode = finalNode;
2532
this.searchArea = new Node[rows][cols];
@@ -28,27 +35,31 @@ public AStar(int rows, int cols, Node initialNode, Node finalNode, int[][] block
2835
initBlocks(blocksArray);
2936
this.closedSet = new HashSet<>();
3037
if (searchDiagonals) {
31-
this.diagonalsChecker = new DiagonalMapChecker(searchArea, openList, closedSet, DEFAULT_DIAGONAL_COST);
38+
this.diagonalsChecker = new DiagonalMapChecker(searchArea, openList, closedSet, diagonalCost);
3239
} else {
3340
this.diagonalsChecker = new NoOpChecker(null, null, null);
3441
}
35-
this.hvChecker = new HorizontalVerticalChecker(searchArea, openList, closedSet, DEFAULT_HV_COST);
42+
this.hvChecker = new HorizontalVerticalChecker(searchArea, openList, closedSet, hvCost);
43+
}
44+
45+
public AStar(int rows, int cols, Node initialNode, Node finalNode, int[][] blocksArray, boolean searchDiagonals) {
46+
this(rows, cols, initialNode, finalNode, blocksArray, searchDiagonals, DEFAULT_DIAGONAL_COST, DEFAULT_HV_COST);
3647
}
3748

3849
private void initNodes() {
3950
for (int i = 0; i < searchArea.length; i++) {
4051
for (int j = 0; j < searchArea[0].length; j++) {
41-
Node node = new Node(i, j);
52+
Node node = Node.of(i, j);
4253
node.calculateHeuristic(finalNode);
4354
this.searchArea[i][j] = node;
4455
}
4556
}
4657
}
4758

4859
private void initBlocks(int[][] blocksArray) {
49-
for (int[] ints : blocksArray) {
50-
int row = ints[0];
51-
int col = ints[1];
60+
for (int[] block : blocksArray) {
61+
int row = block[0];
62+
int col = block[1];
5263
if (row < 0 || row >= searchArea.length) {
5364
continue;
5465
}

src/com/ai/astar/domain/Node.java renamed to app/src/main/java/com/ai/astar/domain/Node.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ public class Node {
1010
private boolean isBlocked;
1111
private Node parent;
1212

13-
public Node(int row, int col) {
13+
private Node(int row, int col) {
1414
super();
1515
this.row = row;
1616
this.col = col;
1717
}
1818

19+
public static Node of(int row, int col) {
20+
return new Node(row, col);
21+
}
22+
1923
public void calculateHeuristic(Node finalNode) {
2024
this.heuristic = Math.abs(finalNode.row() - row) + Math.abs(finalNode.col() - col);
2125
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.ai.astar;
2+
3+
import com.ai.astar.domain.AStar;
4+
import com.ai.astar.domain.Node;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
class AStarTest {
13+
14+
private final Node initialNode = Node.of(2, 1);
15+
private final Node finalNode = Node.of(2, 5);
16+
private final int rows = 6;
17+
private final int cols = 7;
18+
private final int[][] blocksArray = new int[][]{{1, 3}, {2, 3}, {3, 3}};
19+
20+
@Test
21+
void shouldFindShortestPath_whenSearchingDiagonals() {
22+
AStar aStar = new AStar(rows, cols, initialNode, finalNode, blocksArray, true);
23+
List<Node> expected = new ArrayList<>();
24+
expected.add(Node.of(2, 1));
25+
expected.add(Node.of(1, 2));
26+
expected.add(Node.of(0, 3));
27+
expected.add(Node.of(1, 4));
28+
expected.add(Node.of(2, 5));
29+
List<Node> path = aStar.findPath();
30+
assertEquals(expected, path);
31+
}
32+
33+
@Test
34+
void shouldFindShortestPath_whenSearchingHorizontalVertical() {
35+
AStar aStar = new AStar(rows, cols, initialNode, finalNode, blocksArray, false);
36+
List<Node> expected = new ArrayList<>();
37+
expected.add(Node.of(2, 1));
38+
expected.add(Node.of(2, 2));
39+
expected.add(Node.of(1, 2));
40+
expected.add(Node.of(0, 2));
41+
expected.add(Node.of(0, 3));
42+
expected.add(Node.of(0, 4));
43+
expected.add(Node.of(1, 4));
44+
expected.add(Node.of(2, 4));
45+
expected.add(Node.of(2, 5));
46+
List<Node> path = aStar.findPath();
47+
assertEquals(expected, path);
48+
}
49+
}

gradle/wrapper/gradle-wrapper.jar

60.1 KB
Binary file not shown.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
4+
networkTimeout=10000
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)