diff --git a/bots/example_java/MyRobot.java b/bots/example_java/MyRobot.java index ff6838a..e05665b 100644 --- a/bots/example_java/MyRobot.java +++ b/bots/example_java/MyRobot.java @@ -3,58 +3,62 @@ public class MyRobot extends BCAbstractRobot { public Action turn() { - if (me.unit == SPECS.CASTLE) { - log("Building a pilgrim."); - return buildUnit(SPECS.PILGRIM,1,0); - } - return move(1,0); + turn++; - } -} -// package bc19; -// import java.awt.Point; - -// public class MyRobot extends BCAbstractRobot { -// public int turn; -// public Point destination; - -// public Action turn() { -// turn++; - -// if (me.unit == SPECS.CASTLE) { -// if (turn == 1) { -// log("Building a pilgrim."); -// return buildUnit(SPECS.PILGRIM,1,0); -// } - -// Robot[] visibleRobots = getVisibleRobots(); -// for(Robot r: visibleRobots) { -// if (r.team != me.team) { -// int diffX = r.x - me.x; -// int diffY = r.y - me.y; -// return attack(diffX, diffY); -// } -// } - -// Point myLocation = new Point(me.x, me.y); - -// if (destination == null) { -// destination = Navigation.reflect(myLocation, getPassableMap(), me.id % 2 == 0); -// } - -// Point movementDirection = Navigation.goTo(myLocation, destination, getPassableMap(), getVisibleRobotMap()); -// return move(movementDirection.x, movementDirection.y); -// } - -// if (me.unit == SPECS.PILGRIM) { -// if (turn == 1) { -// log("I am a pilgrim."); - -// //log(Integer.toString([0][getVisibleRobots()[0].castle_talk])); -// } -// } + if (me.unit == SPECS.CASTLE) { + if (turn < 3) { + log("Building a Crusader."); + return buildUnit(SPECS.CRUSADER,1,0); + } + } + + if (me.unit == SPECS.CRUSADER) { + if (turn == 1) { + log("I am a Crusader."); + } + + Robot[] visibleRobots = getVisibleRobots(); + for(Robot r: visibleRobots) { + if (r.team != me.team) { + int diffX = r.x - me.x; + int diffY = r.y - me.y; + int dist = diffX * diffX + diffY * diffY; + if (dist >= SPECS.UNITS[SPECS.CRUSADER].ATTACK_RADIUS[0] && dist <= SPECS.UNITS[SPECS.CRUSADER].ATTACK_RADIUS[1]) { + return attack(diffX, diffY); + } + } + } + + Point myLocation = new Point(me.x, me.y); + + if (destination == null) { + destination = Navigation.reflect(myLocation, getPassableMap(), me.id % 2 == 0); + } + + Point movementDirection = Navigation.goTo(myLocation, destination, getPassableMap(), getVisibleRobotMap()); + return move(movementDirection.x, movementDirection.y); + } // return null; -// } -// } \ No newline at end of file + } +} + +class Point { + public int x; + public int y; + Point(int x, int y) { + this.x = x; + this.y = y; + } + + int getSquaredDistTo(Point other) { + int dx = x - other.x; + int dy = y - other.y; + return dx * dx + dy * dy; + } + + Point applyDir(Point dir) { + return new Point(x + dir.x, y + dir.y); + } +} diff --git a/bots/example_java/Navigation.java b/bots/example_java/Navigation.java new file mode 100644 index 0000000..df839c9 --- /dev/null +++ b/bots/example_java/Navigation.java @@ -0,0 +1,98 @@ +package bc19; +import java.util.*; + + +public class Navigation { + public static final String[][] COMPASS = {{"NW", "N", "NE"},{"W", "C", "E"},{"SE", "S", "SE"}}; + public static final String[] ROTATION_ARRAY = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"}; + + public static Point goTo(Point start, Point target, boolean[][] fullMap, int[][] robotMap) { + Point direction = calculateDirection(start, target); + if (direction.x == 0 && direction.y == 0) { + return direction; + } + while(!isPassable(start.applyDir(direction), fullMap, robotMap)) { + direction = rotate(direction, 1); + } + + return direction; + } + + static Point compassToPoint(String dir) { + switch(dir) { + case "N": + return new Point(0, -1); + case "NE": + return new Point(1, -1); + case "NW": + return new Point(-1, -1); + case "E": + return new Point(1, 0); + case "W": + return new Point(-1, 0); + case "SE": + return new Point(1, 1); + case "SW": + return new Point(-1, 1); + default: + return new Point(0,0); + } + } + + static String pointToCompass(Point dir) { + return COMPASS[(int)(dir.y + 1)][(int)(dir.x + 1)]; + } + + public static Point rotate(Point dir, int amount) { + String compassDir = pointToCompass(dir); + List rotationList = Arrays.asList(ROTATION_ARRAY); + String rotateCompassDir = rotationList.get((rotationList.indexOf(compassDir) + amount + 8) % 8); + return compassToPoint(rotateCompassDir); + } + + public static Point reflect(Point loc, boolean[][] fullMap, boolean isHorizontalReflection) { + Point hReflect = new Point(loc.x, fullMap.length - loc.y); + Point vReflect = new Point(fullMap.length - loc.x, loc.y); + + if (isHorizontalReflection) { + return fullMap[hReflect.y][hReflect.x] ? hReflect : vReflect; + } else { + return fullMap[vReflect.y][vReflect.x] ? vReflect : hReflect; + } + } + + public static Point calculateDirection(Point start, Point target) { + int dX = target.x - start.x; + int dY = target.y - start.y; + + if (dX < 0) { + dX = -1; + } else if (dX > 0) { + dX = 1; + } + + if (dY < 0) { + dY = -1; + } else if (dY > 0) { + dY = 1; + } + + return new Point(dX, dY); + } + + public static boolean isPassable(Point loc, boolean[][] fullMap, int[][] robotMap) { + int mapLength = fullMap.length; + int x = loc.x; + int y = loc.y; + if (x < 0 || x >= mapLength) { + return false; + } + if (y < 0 || y >= mapLength) { + return false; + } + if (!fullMap[y][x] || robotMap[y][x] > 0) { + return false; + } + return true; + } +} \ No newline at end of file