Merge pull request #3 from delmadord/a-star

A star
master
delmadord 9 years ago
commit 1e2f9db85a
  1. 3
      .classpath
  2. 0
      .metadata/.lock
  3. BIN
      .metadata/.mylyn/.taskListIndex/segments.gen
  4. BIN
      .metadata/.mylyn/.taskListIndex/segments_1
  5. BIN
      .metadata/.mylyn/repositories.xml.zip
  6. 1
      .metadata/.plugins/org.eclipse.core.resources/.root/.indexes/history.version
  7. BIN
      .metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index
  8. 1
      .metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.version
  9. BIN
      .metadata/.plugins/org.eclipse.core.resources/.root/1.tree
  10. BIN
      .metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources
  11. 2
      .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs
  12. 13
      .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs
  13. 2
      .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.context.core.prefs
  14. 2
      .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.monitor.ui.prefs
  15. 4
      .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.tasks.ui.prefs
  16. 2
      .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.team.cvs.ui.prefs
  17. 5
      .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.ide.prefs
  18. 2
      .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.prefs
  19. 2
      .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs
  20. 1886
      .metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi
  21. BIN
      .metadata/.plugins/org.eclipse.jdt.core/assumedExternalFilesCache
  22. BIN
      .metadata/.plugins/org.eclipse.jdt.core/externalFilesCache
  23. BIN
      .metadata/.plugins/org.eclipse.jdt.core/invalidArchivesCache
  24. BIN
      .metadata/.plugins/org.eclipse.jdt.core/nonChainingJarsCache
  25. BIN
      .metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat
  26. 2
      .metadata/.plugins/org.eclipse.jdt.ui/OpenTypeHistory.xml
  27. 2
      .metadata/.plugins/org.eclipse.jdt.ui/QualifiedTypeNameHistory.xml
  28. 10
      .metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml
  29. BIN
      .metadata/.plugins/org.eclipse.mylyn.trac.core/repositoryConfigurations
  30. 2
      .metadata/.plugins/org.eclipse.pde.core/.cache/clean-cache.properties
  31. 11
      .metadata/.plugins/org.eclipse.ui.ide/dialog_settings.xml
  32. 4
      .metadata/.plugins/org.eclipse.ui.intro/dialog_settings.xml
  33. 15
      .metadata/.plugins/org.eclipse.ui.workbench/dialog_settings.xml
  34. 4
      .metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml
  35. 3
      .metadata/version.ini
  36. 109
      src/environment/AStar.java
  37. 232
      src/environment/Node.java
  38. 44
      src/environment/RouteFinder.java
  39. 170
      src/environment/World.java
  40. 51
      src/myrobot/FirstRobot.java
  41. 112
      src/myrobot/RouteFindingRobot.java
  42. 115
      src/world/Generator.java
  43. 55
      src/world/Point.java

@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry excluding="world/Node_.java|environment/Node_.java" kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="/opt/robocode/libs/robocode.jar" sourcepath="robocode-source/robocode-1.9.2.3-src.zip">
<attributes>
<attribute name="javadoc_location" value="file:/opt/robocode/javadoc/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="/usr/share/java/guava/guava.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

@ -0,0 +1,13 @@
content_assist_proposals_background=255,255,255
content_assist_proposals_foreground=60,60,62
eclipse.preferences.version=1
fontPropagated=true
org.eclipse.jdt.ui.editor.tab.width=
org.eclipse.jdt.ui.formatterprofiles.version=12
org.eclipse.jdt.ui.javadoclocations.migrated=true
org.eclipse.jface.textfont=1|Monospace|10.0|0|GTK|1|;
proposalOrderMigrated=true
spelling_locale_initialized=true
tabWidthPropagated=true
useAnnotationsPrefPage=true
useQuickDiffPrefPage=true

@ -0,0 +1,2 @@
eclipse.preferences.version=1
mylyn.attention.migrated=true

@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.mylyn.monitor.activity.tracking.enabled.checked=true

@ -0,0 +1,4 @@
eclipse.preferences.version=1
migrated.task.repositories.secure.store=true
org.eclipse.mylyn.tasks.ui.filters.nonmatching=true
org.eclipse.mylyn.tasks.ui.filters.nonmatching.encouraged=true

@ -0,0 +1,2 @@
eclipse.preferences.version=1
pref_first_startup=false

@ -0,0 +1,5 @@
PROBLEMS_FILTERS_MIGRATE=true
eclipse.preferences.version=1
platformState=1426418472410
quickStart=false
tipsAndTricks=true

@ -0,0 +1,2 @@
eclipse.preferences.version=1
showIntro=false

@ -0,0 +1,2 @@
ENABLED_DECORATORS=org.eclipse.egit.ui.internal.decorators.GitLightweightDecorator\:true,org.eclipse.jdt.ui.override.decorator\:true,org.eclipse.jdt.ui.interface.decorator\:false,org.eclipse.jdt.ui.buildpath.decorator\:true,org.eclipse.mylyn.context.ui.decorator.interest\:true,org.eclipse.mylyn.tasks.ui.decorators.task\:true,org.eclipse.mylyn.team.ui.changeset.decorator\:true,org.eclipse.pde.ui.binaryProjectDecorator\:false,org.eclipse.team.cvs.ui.decorator\:true,org.eclipse.ui.LinkedResourceDecorator\:true,org.eclipse.ui.SymlinkDecorator\:true,org.eclipse.ui.VirtualResourceDecorator\:true,org.eclipse.ui.ContentTypeDecorator\:true,org.eclipse.ui.ResourceFilterDecorator\:false,
eclipse.preferences.version=1

File diff suppressed because it is too large Load Diff

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<typeInfoHistroy/>

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<qualifiedTypeNameHistroy/>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<section name="Workbench">
<section name="org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart">
<item value="true" key="group_libraries"/>
<item value="false" key="linkWithEditor"/>
<item value="2" key="layout"/>
<item value="1" key="rootMode"/>
<item value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#x0A;&lt;packageExplorer group_libraries=&quot;1&quot; layout=&quot;2&quot; linkWithEditor=&quot;0&quot; rootMode=&quot;1&quot; workingSetName=&quot;Aggregate for window 1426418533468&quot;&gt;&#x0A;&lt;customFilters userDefinedPatternsEnabled=&quot;false&quot;&gt;&#x0A;&lt;xmlDefinedFilters&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.LibraryFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.LocalTypesFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.StaticsFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.pde.ui.ExternalPluginLibrariesFilter1&quot; isEnabled=&quot;true&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.ClosedProjectsFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.NonSharedProjectsFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.NonJavaElementFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.ContainedLibraryFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.CuAndClassFileFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.NonJavaProjectsFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.internal.ui.PackageExplorer.EmptyInnerPackageFilter&quot; isEnabled=&quot;true&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.PackageDeclarationFilter&quot; isEnabled=&quot;true&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.internal.ui.PackageExplorer.EmptyPackageFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.ImportDeclarationFilter&quot; isEnabled=&quot;true&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.FieldsFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.internal.ui.PackageExplorer.HideInnerClassFilesFilter&quot; isEnabled=&quot;true&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.NonPublicFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer_patternFilterId_.*&quot; isEnabled=&quot;true&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.EmptyLibraryContainerFilter&quot; isEnabled=&quot;true&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.pde.ui.BinaryProjectFilter1&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.jdt.ui.PackageExplorer.SyntheticMembersFilter&quot; isEnabled=&quot;true&quot;/&gt;&#x0A;&lt;child filterId=&quot;org.eclipse.mylyn.java.ui.MembersFilter&quot; isEnabled=&quot;false&quot;/&gt;&#x0A;&lt;/xmlDefinedFilters&gt;&#x0A;&lt;/customFilters&gt;&#x0A;&lt;/packageExplorer&gt;" key="memento"/>
</section>
</section>

@ -0,0 +1,2 @@
#Cached timestamps
#Sun Mar 15 12:22:27 CET 2015

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<section name="Workbench">
<section name="ChooseWorkspaceDialogSettings">
<item value="122" key="DIALOG_Y_ORIGIN"/>
<item value="200" key="DIALOG_X_ORIGIN"/>
</section>
<section name="WORKBENCH_SETTINGS">
<list key="ENABLED_TRANSFERS">
</list>
</section>
</section>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<section name="Workbench">
<item value="1024" key="introLaunchBar.location"/>
</section>

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<section name="Workbench">
<section name="org.eclipse.ui.internal.QuickAccess">
<item value="900" key="dialogWidth"/>
<item value="562" key="dialogHeight"/>
<list key="orderedProviders">
</list>
<list key="textArray">
</list>
<list key="orderedElements">
</list>
<list key="textEntries">
</list>
</section>
</section>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<workingSetManager>
<workingSet aggregate="true" factoryID="org.eclipse.ui.internal.WorkingSetFactory" id="1426418533469_0" label="Window Working Set" name="Aggregate for window 1426418533468"/>
</workingSetManager>

@ -0,0 +1,3 @@
#Sun Mar 15 12:22:05 CET 2015
org.eclipse.core.runtime=2
org.eclipse.platform=4.4.2.v20150204-1700

@ -0,0 +1,109 @@
package environment;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import environment.Node;
import environment.World;
public class AStar {
private World world;
public AStar(World world) {
this.world = world;
}
public List<Node> getNodes() {
List<Node> nodes = new ArrayList<Node>();
Set<Node> open = new HashSet<Node>();
Set<Node> closed = new HashSet<Node>();
Node start = new Node(world, world.getStart().getX(), world.getStart().getY());
Node stop = new Node(world, world.getStop().getX(), world.getStop().getY());
start.setG(0);
start.setH(manhattanDistance(start, stop));
start.setF(start.getH());
open.add(start);
while (true) {
Node current = null;
if (open.size() == 0) {
throw new RuntimeException("no route");
}
for (Node node : open) {
if (current == null || node.getF() < current.getF()) {
current = node;
}
}
if (current.equals(stop)) {
stop.setParent(current.getParent());
break;
}
open.remove(current);
closed.add(current);
List<Node> neighbors = new ArrayList<Node>();
neighbors.add(new Node(world, current.getX() + world.getSquareEdgeLenght(), current.getY()));
neighbors.add(new Node(world, current.getX() - world.getSquareEdgeLenght(), current.getY()));
neighbors.add(new Node(world, current.getX(), current.getY() + world.getSquareEdgeLenght()));
neighbors.add(new Node(world, current.getX(), current.getY() - world.getSquareEdgeLenght()));
current.setNeighbors(neighbors);
for (Node neighbor : current.getNeighbors()) {
if (neighbor == null || isOccupied(neighbor) || isOutsideMap(neighbor)) {
continue;
}
int nextG = current.getG() + neighbor.getCost();
if (nextG < neighbor.getG()) {
open.remove(neighbor);
closed.remove(neighbor);
}
if (!open.contains(neighbor) && !closed.contains(neighbor)) {
neighbor.setG(nextG);
neighbor.setH(manhattanDistance(neighbor, stop));
neighbor.setF(neighbor.getG() + neighbor.getH());
neighbor.setParent(current);
open.add(neighbor);
}
}
}
Node current = stop;
while (current.getParent() != null) {
nodes.add(current);
current = current.getParent();
}
nodes.add(start);
return nodes;
}
private int manhattanDistance(Node node1, Node node2) {
return Math.abs(node1.getX() - node2.getX()) + Math.abs(node1.getY() - node2.getY());
}
private boolean isOccupied(Node node) {
return world.getObstacles().contains(node);
}
private boolean isOutsideMap(Node node) {
return !(node.getX() >= 0 && node.getX() < world.getWorldWidth() && node.getY() >= 0 && node.getY() < world
.getWorldHeight());
}
}

@ -0,0 +1,232 @@
package environment;
import java.util.List;
import java.util.ArrayList;
import environment.World;
public class Node {
private List<Node> neighbors = new ArrayList<Node>();
private Node parent;
private int f;
private int g;
private int h;
private int x;
private int y;
private final int cost = 1;
private int col;
private int row;
private World world;
public Node(World world, int x, int y) {
this.world = world;
this.x = x;
this.y = y;
this.col = xToCol(x);
this.row = yToRow(y);
}
public Node(World world, int col, int row, boolean useRowsCols) {
this.world = world;
if (useRowsCols) {
this.col = col;
this.row = row;
this.x = colToX(col);
this.y = rowToY(row);
}
else {
this.x = col;
this.y = row;
this.col = xToCol(col);
this.row = yToRow(row);
}
}
/**
* @return the neighbors
*/
public List<Node> getNeighbors() {
return neighbors;
}
/**
* @param neighbors
* the neighbors to set
*/
public void setNeighbors(List<Node> neighbors) {
this.neighbors = neighbors;
}
/**
* @return the parent
*/
public Node getParent() {
return parent;
}
/**
* @param parent
* the parent to set
*/
public void setParent(Node parent) {
this.parent = parent;
}
/**
* @return the f
*/
public int getF() {
return f;
}
/**
* @param f
* the f to set
*/
public void setF(int f) {
this.f = f;
}
/**
* @return the g
*/
public int getG() {
return g;
}
/**
* @param g
* the g to set
*/
public void setG(int g) {
this.g = g;
}
/**
* @return the h
*/
public int getH() {
return h;
}
/**
* @param h
* the h to set
*/
public void setH(int h) {
this.h = h;
}
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @param x
* the x to set
*/
public void setX(int x) {
this.x = x;
this.col = xToCol(x);
}
/**
* @return the y
*/
public int getY() {
return y;
}
/**
* @param y
* the y to set
*/
public void setY(int y) {
this.y = y;
this.row = yToRow(y);
}
/**
* @return the cost
*/
public int getCost() {
return cost;
}
/**
* @return
*/
public int getCol() {
return col;
}
/**
* @param col
*/
public void setCol(int col) {
this.col = col;
this.x = colToX(col);
}
/**
* @return
*/
public int getRow() {
return row;
}
/**
* @param row
*/
public void setRow(int row) {
this.row = row;
this.y = rowToY(row);
}
public int colToX(int col) {
return col * world.getSquareEdgeLenght() + world.getSquareCornerOffset();
}
public int rowToY(int row) {
return colToX(row);
}
public int xToCol(int x) {
return (x - world.getSquareCornerOffset()) / world.getSquareEdgeLenght();
}
public int yToRow(int y) {
return xToCol(y);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
if (col != node.col) return false;
if (row != node.row) return false;
return true;
}
@Override
public int hashCode() {
return world.getSquareEdgeLenght() * col + row;
}
}

@ -1,4 +1,4 @@
package world;
package environment;
import robocode.control.*;
//import world.Coordinate;
@ -10,7 +10,7 @@ public class RouteFinder {
public static void main(String[] args) {
// Location of the robocode, e.g. "C:/robocode"
String location = "/opt/robocode";
final String location = "/opt/robocode";
// Setup battle parameters
int numberOfRounds = 1;
@ -24,42 +24,36 @@ public class RouteFinder {
// Show the Robocode battle view
engine.setVisible(true);
Generator gen = new Generator();
// The world instance with the obstacles, the start and the stop position
World world = new World();
// Create the battlefield
int NumPixelCols = Generator.COLS * Generator.PX_STEP;
int NumPixelRows = Generator.ROWS * Generator.PX_STEP;
BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelCols, NumPixelRows);
BattlefieldSpecification battlefield = new BattlefieldSpecification(world.getWorldWidth(), world.getWorldHeight());
/*
* Create obstacles and place them at random so that no pair of obstacles are at the same position
*/
RobotSpecification[] modelRobots = engine.getLocalRepository("sample.SittingDuck,myrobot.FirstRobot*");
// Create obstacles and place them at random so that no pair of obstacles are at the same position
RobotSpecification[] modelRobots = engine.getLocalRepository("sample.SittingDuck,myrobot.RouteFindingRobot*");
RobotSpecification[] existingRobots = new RobotSpecification[Generator.NUM_OBSTACLES + 1];
RobotSetup[] robotSetups = new RobotSetup[Generator.NUM_OBSTACLES + 1];
RobotSpecification[] existingRobots = new RobotSpecification[world.getNumObstacles() + 1];
RobotSetup[] robotSetups = new RobotSetup[world.getNumObstacles() + 1];
int NdxObstacle = 0;
for (Point p : gen.obstacles) {
for (Node node : world.getObstacles()) {
// added offset so the tanks are in the middle of the tiles
double InitialObstacleCol = (double) p.getX_();
double InitialObstacleRow = (double) p.getY_();
double InitialObstacleCol = (double) node.getX();
double InitialObstacleRow = (double) node.getY();
existingRobots[NdxObstacle] = modelRobots[0];
robotSetups[NdxObstacle++] = new RobotSetup(InitialObstacleCol, InitialObstacleRow, 0.0);
}
/*
* Create the agent and place it in a random position without obstacle
*/
existingRobots[Generator.NUM_OBSTACLES] = modelRobots[1];
double InitialAgentCol = (double) gen.start.getX_();
double InitialAgentRow = (double) gen.start.getY_();
robotSetups[Generator.NUM_OBSTACLES] = new RobotSetup(InitialAgentCol, InitialAgentRow, 0.0);
// Create the agent and place it in a random position without obstacle
existingRobots[world.getNumObstacles()] = modelRobots[1];
double InitialAgentCol = (double) world.getStart().getX();
double InitialAgentRow = (double) world.getStart().getY();
robotSetups[world.getNumObstacles()] = new RobotSetup(InitialAgentCol, InitialAgentRow, 0.0);
/* Create and run the battle */
// Create and run the battle
BattleSpecification battleSpec = new BattleSpecification(battlefield, numberOfRounds, inactivityTime, gunCoolingRate,
sentryBorderSize, hideEnemyNames, existingRobots, robotSetups);

@ -0,0 +1,170 @@
package environment;
import java.util.Set;
import java.util.HashSet;
import java.util.Random;
import environment.Node;
public class World {
// PRNG seed - level
private final int LEVEL = 4;
// Dimensional constants to match the background's grid pattern
private final int SQUARE_EDGE_LENGHT = 64;
private final int SQUARE_CORNER_OFFSET = 32;
// Map size
private final int NUM_COLS = 16;
private final int NUM_ROWS = 10;
// Obstacle (sitting ducks) count
private final int NUM_OBSTACLES = (int) (NUM_COLS * NUM_ROWS * 0.25);
// Contains poistions of obstacles, start node and destination
private Set<Node> obstacles = new HashSet<>();
private Node start, stop;
// PRNG instance
private Random rand;
// Excuse the crudity of the code, never programmed in Java before
// Could not resolve "pass-by-reference" in Java, thus the code is repeating :/
public World() {
// Seed the PRNG with the level
rand = new Random();
rand.setSeed(LEVEL);
// Generate the obstacles
for (int i = 0; i < getNumObstacles(); i++)
obstacles.add(fillEmptyPosition());
// Generate start/stop positions
start = fillEmptyPosition();
stop = fillEmptyPosition();
}
/**
* @return the level
*/
public int getLevel() {
return LEVEL;
}
/**
* @return the squareEdgeLenght
*/
public int getSquareEdgeLenght() {
return SQUARE_EDGE_LENGHT;
}
/**
* @return the squareCornerOffset
*/
public int getSquareCornerOffset() {
return SQUARE_CORNER_OFFSET;
}
/**
* @return the numCols
*/
public int getNumCols() {
return NUM_COLS;
}
/**
* @return the numRows
*/
public int getNumRows() {
return NUM_ROWS;
}
/**
* @return the numObstacles
*/
public int getNumObstacles() {
return NUM_OBSTACLES;
}
/**
* @return the obstacles
*/
public Set<Node> getObstacles() {
return obstacles;
}
/**
* @return the start node
*/
public Node getStart() {
return start;
}
/**
* @return the stop node
*/
public Node getStop() {
return stop;
}
/**
* @return the worldWidth in px
*/
public int getWorldWidth() {
return getNumCols() * getSquareEdgeLenght();
}
/**
* @return the worldHeight in px
*/
public int getWorldHeight() {
return getNumRows() * getSquareEdgeLenght();
}
private Node fillEmptyPosition() {
// try to find free place for an obstacle
while (true) {
int col = randInt(0, getNumCols() - 1);
int row = randInt(0, getNumRows() - 1);
// Generate node in this world (true for rows and cols not x and y)
Node node = new Node(this, col, row, true);
// Check if the node is still free
if (!obstacles.contains(node) && start != node && stop != node) {
return node;
}
}
}
/**
* Returns a pseudo-random number between min and max, inclusive. The difference between min and max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*
* @param min
* Minimum value
* @param max
* Maximum value. Must be greater than min.
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
private int randInt(int min, int max) {
// Make sure the randomizer was initialised
if (rand == null)
throw new RuntimeException("PRNG not initialised prior to call");
// nextInt is normally exclusive of the top value, so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}

@ -1,51 +0,0 @@
package myrobot;
import robocode.Robot;
public class FirstRobot extends Robot {
public void run() {
while (true) {
doNothing();
}
}
public void north() {
turn(0);
}
public void east() {
turn(90);
}
public void south() {
turn(180);
}
public void west() {
turn(270);
}
public void steps(int num) {
ahead(num * world.Generator.PX_STEP);
}
private void turn(int dest) {
int curr = (int) getHeading();
if (curr != dest) {
int diff = curr - dest;
if (Math.abs(diff) > 180)
turnRight(diff % 180);
else
turnLeft(diff);
}
}
}

@ -0,0 +1,112 @@
package myrobot;
import java.awt.Graphics2D;
import java.util.List;
import com.google.common.collect.Lists;
import environment.AStar;
import environment.World;
import environment.Node;
import robocode.Robot;
public class RouteFindingRobot extends Robot {
private World world = new World();
public void run() {
AStar aStar = new AStar(world);
List<Node> nodes = aStar.getNodes();
for (Node node : Lists.reverse(nodes)) {
Node parent = node.getParent();
if (parent != null)
followPath(node, parent);
}
doNothing();
}
public void onPaint(Graphics2D g) {
// Set the paint color to red
g.setColor(java.awt.Color.GREEN);
// Paint a filled rectangle at (50,50) at size 100x150 pixels
g.fillRect(world.getStop().getX() - 16,
world.getStop().getY() - 16, 32, 32);
}
private void followPath(Node node, Node parent) {
if (node.getCol() < parent.getCol())
left();
else if (node.getCol() > parent.getCol())
right();
else if (node.getRow() < parent.getRow())
down();
else if (node.getRow() > parent.getRow())
up();
else
throw new RuntimeException("wrong movement");
}
private void turn(int destination) {
int current = (int) getHeading();
if (current != destination) {
int difference = current - destination;
if (Math.abs(difference) > 180)
turnRight(difference % 180);
else
turnLeft(difference);
}
}
private void steps(int num) {
ahead(num * world.getSquareEdgeLenght());
}
private void step() {
steps(1);
}
private void up() {
north();
step();
}
private void right() {
east();
step();
}
private void down() {
south();
step();
}
private void left() {
west();
steps(1);
}
private void north() {
turn(0);
}
private void east() {
turn(90);
}
private void south() {
turn(180);
}
private void west() {
turn(270);
}
}

@ -1,115 +0,0 @@
package world;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import world.Point;
public class Generator {
private static final int SEED = 2;
public static final int PX_STEP = 64;
public static final int PX_OFFSET = 32;
public static final int COLS = 16;
public static final int ROWS = 10;
public static final int NUM_OBSTACLES = (int) (COLS * ROWS * 0.25);
// Contains poistions of obstacles, start point and destination
// public static Point[] obstacles = new Point[NUM_OBSTACLES];
// public static Point[] agent = new Point[2];
public Set<Point> obstacles = new HashSet<>();
public Point start;
Random rand;
// Excuse the crudity of the code, never programmed in Java before
// Could not resolve "pass-by-reference" in Java, thus the code is repeating :/
public Generator() {
// NOTE: Usually this should be a field rather than a method
// variable so that it is not re-seeded every call.
rand = new Random();
rand.setSeed(SEED);
// add the obstacles
for (int NdxObstacle = 0; NdxObstacle < NUM_OBSTACLES; NdxObstacle++) {
addPoint();
}
// Add the starting position
addStart();
// Add the
}
public void addPoint() {
// try to find free place for an obstacle
while (true) {
int x = randInt(0, COLS -1);
int y = randInt(0, ROWS -1);
Point point = new Point(x, y);
if (!obstacles.contains(point)) {
obstacles.add(point);
return;
}
}
}
public void addStart() {
// try to find free place for an obstacle
while (true) {
// -1 is due the offset
int x = randInt(0, COLS -1);
int y = randInt(0, ROWS -1);
Point point = new Point(x, y);
if (!obstacles.contains(point)) {
start = point;
return;
}
}
}
//
// public boolean isObstacle(int x, int y) {
// return this.obstacles
// }
//
/**
* Returns a pseudo-random number between min and max, inclusive. The difference between min and max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*
* @param min
* Minimum value
* @param max
* Maximum value. Must be greater than min.
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
private int randInt(int min, int max) {
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}

@ -1,55 +0,0 @@
package world;
public class Point{
private int x,y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getX_() {
return x * Generator.PX_STEP + Generator.PX_OFFSET;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public int getY_() {
return y * Generator.PX_STEP + Generator.PX_OFFSET;
}
public void setY(int y) {
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
if (x != point.x) return false;
if (y != point.y) return false;
return true;
}
@Override
public int hashCode() {
int result = x;
result = Generator.PX_STEP * result + y;
return result;
}
}
Loading…
Cancel
Save