From 59f9158c1e44c72ef706df5129ced2373d854e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Thu, 19 Mar 2015 12:20:09 +0100 Subject: [PATCH] added some comments --- Robocode.odp | 3 +++ Robocode.pptx | 3 +++ github.txt | 1 + src/environment/AStar.java | 19 +++++++++++++++++++ src/environment/Node.java | 4 ++++ src/environment/World.java | 3 ++- 6 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 Robocode.odp create mode 100644 Robocode.pptx create mode 100644 github.txt diff --git a/Robocode.odp b/Robocode.odp new file mode 100644 index 0000000..39558cf --- /dev/null +++ b/Robocode.odp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ef055dde0b21c9fdc4055ea85859358cc51e8dd5baa2715fb7e0a18b20aeb182 +size 489443 diff --git a/Robocode.pptx b/Robocode.pptx new file mode 100644 index 0000000..2dbaf2d --- /dev/null +++ b/Robocode.pptx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c28279e4fa4accd01252eb928fbe9191478166ff5c4946d9eff8a44cdabb95b6 +size 362000 diff --git a/github.txt b/github.txt new file mode 100644 index 0000000..d4d1db3 --- /dev/null +++ b/github.txt @@ -0,0 +1 @@ +https://github.com/delmadord/robocode diff --git a/src/environment/AStar.java b/src/environment/AStar.java index c1e169d..285af26 100644 --- a/src/environment/AStar.java +++ b/src/environment/AStar.java @@ -17,7 +17,12 @@ public class AStar { } + + /** + * @return the nodes + */ public List getNodes() { + // Variable initializations List nodes = new ArrayList(); Set open = new HashSet(); @@ -26,33 +31,40 @@ public class AStar { Node start = new Node(world, world.getStart().getX(), world.getStart().getY()); Node stop = new Node(world, world.getStop().getX(), world.getStop().getY()); + // Calculate the initial attributes start.setG(0); start.setH(manhattanDistance(start, stop)); start.setF(start.getH()); + // Add the first node to the open set open.add(start); while (true) { Node current = null; + // Stop when there are no other nodes in the open set if (open.size() == 0) { throw new RuntimeException("no route"); } + // Find the node with the lowest F for (Node node : open) { if (current == null || node.getF() < current.getF()) { current = node; } } + // Stop when the destination node is found if (current.equals(stop)) { stop.setParent(current.getParent()); break; } + // Swtich the current node from open to closed list open.remove(current); closed.add(current); + // Add four adjacent neihbours List neighbors = new ArrayList(); neighbors.add(new Node(world, current.getX() + world.getSquareEdgeLenght(), current.getY())); neighbors.add(new Node(world, current.getX() - world.getSquareEdgeLenght(), current.getY())); @@ -61,18 +73,24 @@ public class AStar { current.setNeighbors(neighbors); + // Foreach through the neighbours for (Node neighbor : current.getNeighbors()) { + + // Ignore occupied and out-of-the-map modes if (neighbor == null || isOccupied(neighbor) || isOutsideMap(neighbor)) { continue; } + // Recalculate the G int nextG = current.getG() + neighbor.getCost(); + // Comapre and use the one with lower G (better path) if (nextG < neighbor.getG()) { open.remove(neighbor); closed.remove(neighbor); } + // Add the new neighbour to the open set if not present if (!open.contains(neighbor) && !closed.contains(neighbor)) { neighbor.setG(nextG); neighbor.setH(manhattanDistance(neighbor, stop)); @@ -83,6 +101,7 @@ public class AStar { } } + // Create the List of the path Node current = stop; while (current.getParent() != null) { nodes.add(current); diff --git a/src/environment/Node.java b/src/environment/Node.java index 978bec0..ae7bd6c 100644 --- a/src/environment/Node.java +++ b/src/environment/Node.java @@ -5,6 +5,10 @@ import java.util.ArrayList; import environment.World; +/** + * @author delmadord + * + */ public class Node { private List neighbors = new ArrayList(); diff --git a/src/environment/World.java b/src/environment/World.java index 553c57c..4e6b363 100644 --- a/src/environment/World.java +++ b/src/environment/World.java @@ -8,7 +8,8 @@ import environment.Node; public class World { // PRNG seed - level - private final int LEVEL = 4; +// private final int LEVEL = 25; + private final int LEVEL = 25; // Dimensional constants to match the background's grid pattern private final int SQUARE_EDGE_LENGHT = 64;