preparation for a*search

master
Peter Babič 9 years ago
parent 00a72936bb
commit 13c09a8995
  1. 115
      src/myrobot/FirstRobot.java
  2. 18
      src/world/Generator.java
  3. 10
      src/world/Node.java

@ -1,37 +1,73 @@
package myrobot;
import java.util.HashSet;
import java.util.Set;
import robocode.Robot;
import world.Generator;
import world.Point;
public class FirstRobot extends Robot {
// cost is always 1 because we do not move on diagonals
public static final int G = 1;
private static Generator gen = new Generator();
private static Set<Point> open = new HashSet<>();
private static Set<Point> closed = new HashSet<>();
private static Point lowestP;
private static int lowestF = 0;
public void run() {
while (true) {
doNothing();
open.add(gen.start);
for (Point p : open) {
int H = Manhattan(p, gen.stop);
int F = G + H;
if (lowestF == 0 || lowestF >= F) {
lowestF = F;
lowestP = p;
}
}
open.remove(lowestP);
closed.add(lowestP);
Point pu = new Point(lowestP.getX(), lowestP.getY() + 1);
Point pr = new Point(lowestP.getX() + 1, lowestP.getY());
Point pd = new Point(lowestP.getX(), lowestP.getY() - 1);
Point pl = new Point(lowestP.getX() - 1, lowestP.getY());
// if (lowestP)
}
}
public void north() {
turn(0);
}
public void east() {
turn(90);
}
public void south() {
turn(180);
}
public void west() {
turn(270);
private void calculatePoint(Point p) {
if (isWalkable(p)) {
if (!open.contains(p)) {
open.add(p);
}
}
}
public void steps(int num) {
ahead(num * world.Generator.PX_STEP);
private boolean isWalkable(Point p) {
return (!gen.obstacles.contains(p) &&
!closed.contains(p) &&
p.getX() >= 0 &&
p.getX() < Generator.ROWS &&
p.getY() >= 0 &&
p.getY() < Generator.COLS);
}
private void turn(int dest) {
@ -44,8 +80,49 @@ public class FirstRobot extends Robot {
turnLeft(diff);
}
}
public void steps(int num) {
ahead(num * world.Generator.PX_STEP);
}
private int Manhattan(Point start, Point stop) {
return Math.abs(start.getX() - stop.getX()) + Math.abs(start.getX() - stop.getY());
}
public void up() {
north();
steps(1);
}
public void right() {
east();
steps(1);
}
public void down() {
south();
steps(1);
}
public void left() {
west();
steps(1);
}
public void north() {
turn(0);
}
public void east() {
turn(90);
}
public void south() {
turn(180);
}
public void west() {
turn(270);
}
}

@ -4,7 +4,6 @@ import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import world.Point;
public class Generator {
@ -25,7 +24,7 @@ public class Generator {
// public static Point[] obstacles = new Point[NUM_OBSTACLES];
// public static Point[] agent = new Point[2];
public Set<Point> obstacles = new HashSet<>();
public Point start;
public Point start, stop;
Random rand;
@ -83,6 +82,21 @@ public class Generator {
}
}
public void addStop() {
// 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) {
stop = point;
return;
}
}
}
//
// public boolean isObstacle(int x, int y) {

@ -0,0 +1,10 @@
package world;
public class Node extends Point {
public Node(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}
}
Loading…
Cancel
Save