From 13c09a8995ec449512f520ce6be2bf6957adfa7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 8 Mar 2015 20:04:03 +0100 Subject: [PATCH] preparation for a*search --- src/myrobot/FirstRobot.java | 115 ++++++++++++++++++++++++++++++------ src/world/Generator.java | 18 +++++- src/world/Node.java | 10 ++++ 3 files changed, 122 insertions(+), 21 deletions(-) create mode 100644 src/world/Node.java diff --git a/src/myrobot/FirstRobot.java b/src/myrobot/FirstRobot.java index a8d3c31..8a72387 100644 --- a/src/myrobot/FirstRobot.java +++ b/src/myrobot/FirstRobot.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 open = new HashSet<>(); + private static Set 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); + } } diff --git a/src/world/Generator.java b/src/world/Generator.java index 1e30948..6e306d2 100644 --- a/src/world/Generator.java +++ b/src/world/Generator.java @@ -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 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) { diff --git a/src/world/Node.java b/src/world/Node.java new file mode 100644 index 0000000..408c3f4 --- /dev/null +++ b/src/world/Node.java @@ -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 + } + +}