commit
1e2f9db85a
@ -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> |
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,2 @@ |
||||
eclipse.preferences.version=1 |
||||
version=1 |
@ -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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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="<?xml version="1.0" encoding="UTF-8"?>
<packageExplorer group_libraries="1" layout="2" linkWithEditor="0" rootMode="1" workingSetName="Aggregate for window 1426418533468">
<customFilters userDefinedPatternsEnabled="false">
<xmlDefinedFilters>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.LibraryFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.LocalTypesFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.StaticsFilter" isEnabled="false"/>
<child filterId="org.eclipse.pde.ui.ExternalPluginLibrariesFilter1" isEnabled="true"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.ClosedProjectsFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.NonSharedProjectsFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.NonJavaElementFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.ContainedLibraryFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.CuAndClassFileFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.NonJavaProjectsFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.internal.ui.PackageExplorer.EmptyInnerPackageFilter" isEnabled="true"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.PackageDeclarationFilter" isEnabled="true"/>
<child filterId="org.eclipse.jdt.internal.ui.PackageExplorer.EmptyPackageFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.ImportDeclarationFilter" isEnabled="true"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.FieldsFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.internal.ui.PackageExplorer.HideInnerClassFilesFilter" isEnabled="true"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.NonPublicFilter" isEnabled="false"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer_patternFilterId_.*" isEnabled="true"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.EmptyLibraryContainerFilter" isEnabled="true"/>
<child filterId="org.eclipse.pde.ui.BinaryProjectFilter1" isEnabled="false"/>
<child filterId="org.eclipse.jdt.ui.PackageExplorer.SyntheticMembersFilter" isEnabled="true"/>
<child filterId="org.eclipse.mylyn.java.ui.MembersFilter" isEnabled="false"/>
</xmlDefinedFilters>
</customFilters>
</packageExplorer>" key="memento"/> |
||||
</section> |
||||
</section> |
Binary file not shown.
@ -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; |
||||
} |
||||
|
||||
} |
@ -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…
Reference in new issue