diff --git a/src/myrobot/FirstRobot.java b/src/myrobot/FirstRobot.java index a520d0c..a8d3c31 100644 --- a/src/myrobot/FirstRobot.java +++ b/src/myrobot/FirstRobot.java @@ -1,75 +1,38 @@ package myrobot; -/******************************************************************************* - * Copyright (c) 2001-2014 Mathew A. Nelson and Robocode contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://robocode.sourceforge.net/license/epl-v10.html - *******************************************************************************/ - -import robocode.HitByBulletEvent; import robocode.Robot; -import robocode.ScannedRobotEvent; - -/** - * MyFirstRobot - a sample robot by Mathew Nelson. - *

- * Moves in a seesaw motion, and spins the gun around at each end. - * - * @author Mathew A. Nelson (original) - */ + + public class FirstRobot extends Robot { - /** - * MyFirstRobot's run method - Seesaw - */ public void run() { while (true) { - right(); - ahead(128); - - down(); - ahead(64); - - left(); - ahead(128); - - up(); - ahead(64); - - right(); - ahead(128); - - up(); - ahead(64); - - left(); - ahead(128); - - down(); - ahead(64); + doNothing(); } } - public void up() { + public void north() { turn(0); } - public void right() { + public void east() { turn(90); } - public void down() { + public void south() { turn(180); } - public void left() { + 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(); @@ -84,17 +47,5 @@ public class FirstRobot extends Robot { - /** - * Fire when we see a robot - */ - public void onScannedRobot(ScannedRobotEvent e) { - fire(1); - } - /** - * We were hit! Turn perpendicular to the bullet, so our seesaw might avoid a future shot. - */ - public void onHitByBullet(HitByBulletEvent e) { - turnLeft(90 - e.getBearing()); - } } diff --git a/src/world/Generator.java b/src/world/Generator.java new file mode 100644 index 0000000..1e30948 --- /dev/null +++ b/src/world/Generator.java @@ -0,0 +1,115 @@ +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 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 + * Integer.MAX_VALUE - 1. + * + * @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; + } + +} + + diff --git a/src/world/Point.java b/src/world/Point.java new file mode 100644 index 0000000..fc91592 --- /dev/null +++ b/src/world/Point.java @@ -0,0 +1,55 @@ +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; + } +} \ No newline at end of file diff --git a/src/standalone/RouteFinder.java b/src/world/RouteFinder.java similarity index 53% rename from src/standalone/RouteFinder.java rename to src/world/RouteFinder.java index 835d724..10bf72b 100644 --- a/src/standalone/RouteFinder.java +++ b/src/world/RouteFinder.java @@ -1,27 +1,16 @@ -package standalone; +package world; import robocode.control.*; -//import robocode.control.events.*; - -//movement +//import world.Coordinate; -//... +//import robocode.control.events.*; public class RouteFinder { public static void main(String[] args) { - // Create the RobocodeEngine, e.g. "C:/robocode" - RobocodeEngine engine = new RobocodeEngine(new java.io.File("/opt/robocode")); - - // Show the Robocode battle view - engine.setVisible(true); - - // Create the battlefield - int NumPixelRows = 800; - int NumPixelCols = 600; - - BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelRows, NumPixelCols); // 800x600 + // Location of the robocode, e.g. "C:/robocode" + String location = "/opt/robocode"; // Setup battle parameters int numberOfRounds = 1; @@ -30,32 +19,45 @@ public class RouteFinder { int sentryBorderSize = 50; boolean hideEnemyNames = false; - // int NumObstacles = (int) Math.round(NumPixelCols * NumPixelRows * 0.3); - int NumObstacles = 5; + // Create the RobocodeEngine + RobocodeEngine engine = new RobocodeEngine(new java.io.File(location)); + + // Show the Robocode battle view + engine.setVisible(true); + + Generator gen = new Generator(); + + // Create the battlefield + int NumPixelCols = Generator.COLS * Generator.PX_STEP; + int NumPixelRows = Generator.ROWS * Generator.PX_STEP; + + BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelCols, NumPixelRows); + /* * 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*"); - RobotSpecification[] existingRobots = new RobotSpecification[NumObstacles + 1]; - RobotSetup[] robotSetups = new RobotSetup[NumObstacles + 1]; - - for (int NdxObstacle = 0; NdxObstacle < NumObstacles; NdxObstacle++) { + RobotSpecification[] existingRobots = new RobotSpecification[Generator.NUM_OBSTACLES + 1]; + RobotSetup[] robotSetups = new RobotSetup[Generator.NUM_OBSTACLES + 1]; - // double InitialObstacleRow = null; - // double InitialObstacleCol = null; + int NdxObstacle = 0; + for (Point p : gen.obstacles) { + // added offset so the tanks are in the middle of the tiles + double InitialObstacleCol = (double) p.getX_(); + double InitialObstacleRow = (double) p.getY_(); existingRobots[NdxObstacle] = modelRobots[0]; - robotSetups[NdxObstacle] = new RobotSetup(null, null, 0.0); + robotSetups[NdxObstacle++] = new RobotSetup(InitialObstacleCol, InitialObstacleRow, 0.0); } /* * Create the agent and place it in a random position without obstacle */ - existingRobots[NumObstacles] = modelRobots[1]; - // double InitialAgentRow = 1; - // double InitialAgentCol = 1; - robotSetups[NumObstacles] = new RobotSetup(null, null, 0.0); + 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 and run the battle */ BattleSpecification battleSpec = new BattleSpecification(battlefield, numberOfRounds, inactivityTime, gunCoolingRate,