diff --git a/src/world/Coordinate.java b/src/world/Coordinate.java new file mode 100644 index 0000000..51b02db --- /dev/null +++ b/src/world/Coordinate.java @@ -0,0 +1,33 @@ +package world; + +public class Coordinate { + + private int x = 0; + private int y = 0; + + public Coordinate(int x, int y) { + this.x = x; + this.y = y; + } + + public Coordinate() + { + + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } +} \ No newline at end of file diff --git a/src/world/Generator.java b/src/world/Generator.java index 1f672aa..83afc48 100644 --- a/src/world/Generator.java +++ b/src/world/Generator.java @@ -1,49 +1,59 @@ package world; import java.util.Random; +import world.Coordinate; public class Generator { + private static final int SEED = 1; + + public static final int PX_STEP = 64; public static final int COLS = 12; public static final int ROWS = 10; - private static final int SEED = 1; - public static int NumObstacles = (int) (COLS * ROWS * 0.3); + public static final int NUM_OBSTACLES = (int) (COLS * ROWS * 0.3); // Contains poistions of obstacles, start point and destination - public static int[] obstacles = new int[NumObstacles]; + public Coordinate[] obstacles = new Coordinate[NUM_OBSTACLES]; + public Coordinate start = new Coordinate(); + public Coordinate stop = new Coordinate(); + // 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 static void main(String[] args) { + - for (int NdxObstacle = 0; NdxObstacle < NumObstacles; NdxObstacle++) { - -// // try to find free place for an obstacle -// while (true) { -// int x = randInt(0, COLS); -// int y = randInt(0, ROWS); -// -// if (obstacles[x][y] == 0) { -// obstacles[x][y] = 1; -// break; -// } -// -// } + for (int NdxObstacle = 0; NdxObstacle < NUM_OBSTACLES; NdxObstacle++) { + + // try to find free place for an obstacle + while (true) { + int x = randInt(0, COLS); + int y = randInt(0, ROWS); + + if (obstacles[NdxObstacle] != null) { + obstacles[NdxObstacle].setX(x); + obstacles[NdxObstacle].setY(y); + break; + } + + } } -/* // try to find free place for a start position + // try to find free place for a start position while (true) { int x = randInt(0, COLS); int y = randInt(0, ROWS); - if (start[x][y] == 0) { - start[x][y] = 1; + if (start != null) { + start.setX(x); + start.setY(y); break; } @@ -54,12 +64,13 @@ public class Generator { int x = randInt(0, COLS); int y = randInt(0, ROWS); - if (stop[x][y] == 0) { - stop[x][y] = 1; + if (stop != null) { + stop.setX(x); + stop.setY(y); break; } - }*/ + } } diff --git a/src/world/RouteFinder.java b/src/world/RouteFinder.java index b0c608d..3eb5f43 100644 --- a/src/world/RouteFinder.java +++ b/src/world/RouteFinder.java @@ -1,18 +1,17 @@ package world; import robocode.control.*; -//import robocode.control.events.*; +//import world.Coordinate; +//import robocode.control.events.*; public class RouteFinder { - - public static void main(String[] args) { // Location of the robocode, e.g. "C:/robocode" String location = "/opt/robocode"; - + // Setup battle parameters int numberOfRounds = 1; long inactivityTime = 10000000; @@ -20,33 +19,33 @@ public class RouteFinder { int sentryBorderSize = 50; boolean hideEnemyNames = false; - // 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 NumPixelRows = world.Generator.ROWS * world.Generator.PX_STEP; - int NumPixelCols = world.Generator.COLS * world.Generator.PX_STEP; - - BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelRows, NumPixelCols); - + int NumPixelRows = Generator.ROWS * Generator.PX_STEP; + int NumPixelCols = Generator.COLS * Generator.PX_STEP; + BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelRows, NumPixelCols); + /* * 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[world.Generator.NumObstacles + 1]; - RobotSetup[] robotSetups = new RobotSetup[world.Generator.NumObstacles + 1]; + RobotSpecification[] existingRobots = new RobotSpecification[Generator.NUM_OBSTACLES + 1]; + RobotSetup[] robotSetups = new RobotSetup[Generator.NUM_OBSTACLES + 1]; - for (int NdxObstacle = 0; NdxObstacle < world.Generator.NumObstacles; NdxObstacle++) { + for (int NdxObstacle = 0; NdxObstacle < Generator.NUM_OBSTACLES; NdxObstacle++) { - double InitialObstacleCol = 1; - double InitialObstacleRow = 1; + double InitialObstacleCol = (double) gen.obstacles[NdxObstacle].getX(); + double InitialObstacleRow = (double) gen.obstacles[NdxObstacle].getY(); existingRobots[NdxObstacle] = modelRobots[0]; robotSetups[NdxObstacle] = new RobotSetup(InitialObstacleCol, InitialObstacleRow, 0.0); } @@ -54,10 +53,10 @@ public class RouteFinder { /* * Create the agent and place it in a random position without obstacle */ - existingRobots[world.Generator.NumObstacles] = modelRobots[1]; - // double InitialAgentRow = 1; - // double InitialAgentCol = 1; - robotSetups[world.Generator.NumObstacles] = new RobotSetup(null, null, 0.0); + existingRobots[world.Generator.NUM_OBSTACLES] = modelRobots[1]; + double InitialAgentCol = (double) gen.start.getX(); + double InitialAgentRow = (double) gen.start.getY(); + robotSetups[world.Generator.NUM_OBSTACLES] = new RobotSetup(InitialAgentCol, InitialAgentRow, 0.0); /* Create and run the battle */ BattleSpecification battleSpec = new BattleSpecification(battlefield, numberOfRounds, inactivityTime, gunCoolingRate,