From c9616e79f2dbacb4af24cecf78ecb21c3c6e4e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 8 Mar 2015 09:53:50 +0100 Subject: [PATCH 1/8] steps and grid --- src/myrobot/FirstRobot.java | 71 +++++---------------------------- src/standalone/RouteFinder.java | 34 +++++++++------- src/world/Generator.java | 15 +++++++ 3 files changed, 45 insertions(+), 75 deletions(-) create mode 100644 src/world/Generator.java diff --git a/src/myrobot/FirstRobot.java b/src/myrobot/FirstRobot.java index a520d0c..79b771e 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.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/standalone/RouteFinder.java b/src/standalone/RouteFinder.java index 835d724..7df3236 100644 --- a/src/standalone/RouteFinder.java +++ b/src/standalone/RouteFinder.java @@ -3,26 +3,16 @@ package standalone; import robocode.control.*; //import robocode.control.events.*; -//movement - -//... 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; long inactivityTime = 10000000; @@ -32,6 +22,20 @@ public class RouteFinder { // 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); + + // Create the battlefield + int NumPixelRows = world.Generator.HEIGHT; + int NumPixelCols = world.Generator.WIDTH; + + BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelRows, NumPixelCols); + + /* * Create obstacles and place them at random so that no pair of obstacles are at the same position diff --git a/src/world/Generator.java b/src/world/Generator.java new file mode 100644 index 0000000..5575de2 --- /dev/null +++ b/src/world/Generator.java @@ -0,0 +1,15 @@ +package world; + + + +public class Generator { + + + + public static final int STEP = 64; + + public static final int WIDTH = 10 * STEP; + public static final int HEIGHT = 8 * STEP; + + +} From debb02ce6aee1da10cba9278153bf2e8536bf1a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 8 Mar 2015 10:55:03 +0100 Subject: [PATCH 2/8] world class prepared --- src/myrobot/FirstRobot.java | 2 +- src/standalone/RouteFinder.java | 18 ++++++++---------- src/world/Generator.java | 18 +++++++++++++++--- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/myrobot/FirstRobot.java b/src/myrobot/FirstRobot.java index 79b771e..a8d3c31 100644 --- a/src/myrobot/FirstRobot.java +++ b/src/myrobot/FirstRobot.java @@ -31,7 +31,7 @@ public class FirstRobot extends Robot { } public void steps(int num) { - ahead(num * world.Generator.STEP); + ahead(num * world.Generator.PX_STEP); } private void turn(int dest) { diff --git a/src/standalone/RouteFinder.java b/src/standalone/RouteFinder.java index 7df3236..f59d149 100644 --- a/src/standalone/RouteFinder.java +++ b/src/standalone/RouteFinder.java @@ -20,9 +20,7 @@ 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)); @@ -30,8 +28,8 @@ public class RouteFinder { engine.setVisible(true); // Create the battlefield - int NumPixelRows = world.Generator.HEIGHT; - int NumPixelCols = world.Generator.WIDTH; + int NumPixelRows = world.Generator.HEIGHT * world.Generator.PX_STEP; + int NumPixelCols = world.Generator.WIDTH * world.Generator.PX_STEP; BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelRows, NumPixelCols); @@ -42,10 +40,10 @@ public class RouteFinder { */ RobotSpecification[] modelRobots = engine.getLocalRepository("sample.SittingDuck,myrobot.FirstRobot*"); - RobotSpecification[] existingRobots = new RobotSpecification[NumObstacles + 1]; - RobotSetup[] robotSetups = new RobotSetup[NumObstacles + 1]; + RobotSpecification[] existingRobots = new RobotSpecification[world.Generator.NumObstacles + 1]; + RobotSetup[] robotSetups = new RobotSetup[world.Generator.NumObstacles + 1]; - for (int NdxObstacle = 0; NdxObstacle < NumObstacles; NdxObstacle++) { + for (int NdxObstacle = 0; NdxObstacle < world.Generator.NumObstacles; NdxObstacle++) { // double InitialObstacleRow = null; // double InitialObstacleCol = null; @@ -56,10 +54,10 @@ public class RouteFinder { /* * Create the agent and place it in a random position without obstacle */ - existingRobots[NumObstacles] = modelRobots[1]; + existingRobots[world.Generator.NumObstacles] = modelRobots[1]; // double InitialAgentRow = 1; // double InitialAgentCol = 1; - robotSetups[NumObstacles] = new RobotSetup(null, null, 0.0); + robotSetups[world.Generator.NumObstacles] = new RobotSetup(null, null, 0.0); /* Create and run the battle */ BattleSpecification battleSpec = new BattleSpecification(battlefield, numberOfRounds, inactivityTime, gunCoolingRate, diff --git a/src/world/Generator.java b/src/world/Generator.java index 5575de2..2d194ab 100644 --- a/src/world/Generator.java +++ b/src/world/Generator.java @@ -1,15 +1,27 @@ package world; +import java.util.Random; + public class Generator { + public static final int PX_STEP = 64; + public static final int WIDTH = 12; + public static final int HEIGHT = 10; - public static final int STEP = 64; + private static final int SEED = 1; - public static final int WIDTH = 10 * STEP; - public static final int HEIGHT = 8 * STEP; +// public static int NumObstacles = (int) (WIDTH * HEIGHT * 0.3); + public static int NumObstacles = (int) 1; + // Initialize PRNG + Random gen = new Random(SEED); + + for (int NdxObstacle = 0; NdxObstacle < NumObstacles; NdxObstacle++) + ; + + } From ff295eaf272b188b35e3d53612f155d83514968d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 8 Mar 2015 11:17:46 +0100 Subject: [PATCH 3/8] world class prepared --- src/standalone/RouteFinder.java | 4 +-- src/world/Generator.java | 51 ++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/standalone/RouteFinder.java b/src/standalone/RouteFinder.java index f59d149..0b07479 100644 --- a/src/standalone/RouteFinder.java +++ b/src/standalone/RouteFinder.java @@ -28,8 +28,8 @@ public class RouteFinder { engine.setVisible(true); // Create the battlefield - int NumPixelRows = world.Generator.HEIGHT * world.Generator.PX_STEP; - int NumPixelCols = world.Generator.WIDTH * world.Generator.PX_STEP; + int NumPixelRows = world.Generator.ROWS * world.Generator.PX_STEP; + int NumPixelCols = world.Generator.COLS * world.Generator.PX_STEP; BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelRows, NumPixelCols); diff --git a/src/world/Generator.java b/src/world/Generator.java index 2d194ab..8729af7 100644 --- a/src/world/Generator.java +++ b/src/world/Generator.java @@ -8,20 +8,57 @@ public class Generator { public static final int PX_STEP = 64; - public static final int WIDTH = 12; - public static final int HEIGHT = 10; + public static final int COLS = 12; + public static final int ROWS = 10; private static final int SEED = 1; // public static int NumObstacles = (int) (WIDTH * HEIGHT * 0.3); - public static int NumObstacles = (int) 1; + public static int NumObstacles = 1; - // Initialize PRNG - Random gen = new Random(SEED); - for (int NdxObstacle = 0; NdxObstacle < NumObstacles; NdxObstacle++) - ; + // Contains poistions of obstacles and agent + public static int obstacles[][]; + + + public static void main(String[] args) { + + int NdxObstacle = 0; + + while (true) { + int x = randInt(0, COLS); + int y = randInt(0, ROWS); + + + NdxObstacle++; + } + + } + + /** + * 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) + */ + public static int randInt(int min, int max) { + + // NOTE: Usually this should be a field rather than a method + // variable so that it is not re-seeded every call. + Random rand = new Random(SEED); + + // 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; + } + } From 97502d37a15344a114cb43b249132be1a0f893a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 8 Mar 2015 13:02:53 +0100 Subject: [PATCH 4/8] world class prepared --- src/standalone/RouteFinder.java | 6 +-- src/world/Generator.java | 96 +++++++++++++++++++++------------ 2 files changed, 64 insertions(+), 38 deletions(-) diff --git a/src/standalone/RouteFinder.java b/src/standalone/RouteFinder.java index 0b07479..3fce394 100644 --- a/src/standalone/RouteFinder.java +++ b/src/standalone/RouteFinder.java @@ -45,10 +45,10 @@ public class RouteFinder { for (int NdxObstacle = 0; NdxObstacle < world.Generator.NumObstacles; NdxObstacle++) { - // double InitialObstacleRow = null; - // double InitialObstacleCol = null; + double InitialObstacleCol = 1; + double InitialObstacleRow = 1; existingRobots[NdxObstacle] = modelRobots[0]; - robotSetups[NdxObstacle] = new RobotSetup(null, null, 0.0); + robotSetups[NdxObstacle] = new RobotSetup(InitialObstacleCol, InitialObstacleRow, 0.0); } /* diff --git a/src/world/Generator.java b/src/world/Generator.java index 8729af7..93f04dd 100644 --- a/src/world/Generator.java +++ b/src/world/Generator.java @@ -2,63 +2,89 @@ package world; import java.util.Random; - - public class Generator { - + 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) (WIDTH * HEIGHT * 0.3); - public static int NumObstacles = 1; - - // Contains poistions of obstacles and agent - public static int obstacles[][]; + public static int NumObstacles = (int) (COLS * ROWS * 0.3); + // Contains poistions of obstacles, start point and destination + public static int[] obstacles = new int[NumObstacles]; + + // Excuse the crudity of the code, never programmed in Java before public static void main(String[] args) { + - int NdxObstacle = 0; - + 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; +// } +// +// } + + } + +/* // try to find free place for a start position while (true) { int x = randInt(0, COLS); int y = randInt(0, ROWS); - - - NdxObstacle++; + + if (start[x][y] == 0) { + start[x][y] = 1; + break; + } + } - + + // try to find free place for a destination + while (true) { + int x = randInt(0, COLS); + int y = randInt(0, ROWS); + + if (stop[x][y] == 0) { + stop[x][y] = 1; + break; + } + + }*/ + } - + /** - * Returns a pseudo-random number between min and max, inclusive. - * The difference between min and max can be at most + * 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. + * @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) */ - public static int randInt(int min, int max) { + private static int randInt(int min, int max) { - // NOTE: Usually this should be a field rather than a method - // variable so that it is not re-seeded every call. - Random rand = new Random(SEED); + // NOTE: Usually this should be a field rather than a method + // variable so that it is not re-seeded every call. + Random rand = new Random(SEED); - // nextInt is normally exclusive of the top value, - // so add 1 to make it inclusive - int randomNum = rand.nextInt((max - min) + 1) + min; + // 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; + } - return randomNum; - } - - - } From 74eae674458391bc5b03c510ef4183fe74160426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 8 Mar 2015 13:25:09 +0100 Subject: [PATCH 5/8] world class prepared --- src/world/Generator.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/world/Generator.java b/src/world/Generator.java index 93f04dd..1f672aa 100644 --- a/src/world/Generator.java +++ b/src/world/Generator.java @@ -88,3 +88,5 @@ public class Generator { } } + + From 3d88ece1ef17de95bd52ea9f7bd6c3efaac23049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 8 Mar 2015 13:27:08 +0100 Subject: [PATCH 6/8] world class prepared --- src/{standalone => world}/RouteFinder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/{standalone => world}/RouteFinder.java (99%) diff --git a/src/standalone/RouteFinder.java b/src/world/RouteFinder.java similarity index 99% rename from src/standalone/RouteFinder.java rename to src/world/RouteFinder.java index 3fce394..b0c608d 100644 --- a/src/standalone/RouteFinder.java +++ b/src/world/RouteFinder.java @@ -1,4 +1,4 @@ -package standalone; +package world; import robocode.control.*; //import robocode.control.events.*; From 43d0c00b71625e8cd37b53e6101e4e6596ead696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 8 Mar 2015 14:44:55 +0100 Subject: [PATCH 7/8] world class prepared --- src/world/Coordinate.java | 33 +++++++++++++++++++++++ src/world/Generator.java | 55 +++++++++++++++++++++++--------------- src/world/RouteFinder.java | 37 +++++++++++++------------ 3 files changed, 84 insertions(+), 41 deletions(-) create mode 100644 src/world/Coordinate.java 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, From 00a72936bb0714591d2342df6d2fe7752a6fac6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 8 Mar 2015 17:10:39 +0100 Subject: [PATCH 8/8] world generation working --- src/world/Coordinate.java | 33 ------------ src/world/Generator.java | 106 +++++++++++++++++++++---------------- src/world/Point.java | 55 +++++++++++++++++++ src/world/RouteFinder.java | 23 ++++---- 4 files changed, 126 insertions(+), 91 deletions(-) delete mode 100644 src/world/Coordinate.java create mode 100644 src/world/Point.java diff --git a/src/world/Coordinate.java b/src/world/Coordinate.java deleted file mode 100644 index 51b02db..0000000 --- a/src/world/Coordinate.java +++ /dev/null @@ -1,33 +0,0 @@ -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 83afc48..1e30948 100644 --- a/src/world/Generator.java +++ b/src/world/Generator.java @@ -1,78 +1,94 @@ package world; +import java.util.HashSet; import java.util.Random; -import world.Coordinate; +import java.util.Set; + +import world.Point; public class Generator { - private static final int SEED = 1; + 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 = 12; + public static final int COLS = 16; public static final int ROWS = 10; - public static final int NUM_OBSTACLES = (int) (COLS * ROWS * 0.3); + public static final int NUM_OBSTACLES = (int) (COLS * ROWS * 0.25); // Contains poistions of obstacles, start point and destination - public Coordinate[] obstacles = new Coordinate[NUM_OBSTACLES]; - public Coordinate start = new Coordinate(); - public Coordinate stop = new Coordinate(); - +// 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 static void main(String[] args) { + 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++) { - // 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; - } - - } + addPoint(); } + + // Add the starting position + addStart(); + + // Add the - // try to find free place for a start position + } + + public void addPoint() { + // try to find free place for an obstacle while (true) { - int x = randInt(0, COLS); - int y = randInt(0, ROWS); - - if (start != null) { - start.setX(x); - start.setY(y); - break; + + 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; } - } - - // try to find free place for a destination + } + + public void addStart() { + // try to find free place for an obstacle while (true) { - int x = randInt(0, COLS); - int y = randInt(0, ROWS); - - if (stop != null) { - stop.setX(x); - stop.setY(y); - break; + // -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 @@ -85,11 +101,7 @@ public class Generator { * @return Integer between min and max, inclusive. * @see java.util.Random#nextInt(int) */ - private static int randInt(int min, int max) { - - // NOTE: Usually this should be a field rather than a method - // variable so that it is not re-seeded every call. - Random rand = new Random(SEED); + private int randInt(int min, int max) { // nextInt is normally exclusive of the top value, // so add 1 to make it inclusive 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/world/RouteFinder.java b/src/world/RouteFinder.java index 3eb5f43..10bf72b 100644 --- a/src/world/RouteFinder.java +++ b/src/world/RouteFinder.java @@ -28,10 +28,10 @@ public class RouteFinder { Generator gen = new Generator(); // Create the battlefield - int NumPixelRows = Generator.ROWS * Generator.PX_STEP; int NumPixelCols = Generator.COLS * Generator.PX_STEP; + int NumPixelRows = Generator.ROWS * Generator.PX_STEP; - BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelRows, NumPixelCols); + BattlefieldSpecification battlefield = new BattlefieldSpecification(NumPixelCols, NumPixelRows); /* @@ -42,21 +42,22 @@ public class RouteFinder { RobotSpecification[] existingRobots = new RobotSpecification[Generator.NUM_OBSTACLES + 1]; RobotSetup[] robotSetups = new RobotSetup[Generator.NUM_OBSTACLES + 1]; - for (int NdxObstacle = 0; NdxObstacle < Generator.NUM_OBSTACLES; NdxObstacle++) { - - double InitialObstacleCol = (double) gen.obstacles[NdxObstacle].getX(); - double InitialObstacleRow = (double) gen.obstacles[NdxObstacle].getY(); + 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(InitialObstacleCol, InitialObstacleRow, 0.0); + robotSetups[NdxObstacle++] = new RobotSetup(InitialObstacleCol, InitialObstacleRow, 0.0); } /* * Create the agent and place it in a random position without obstacle */ - 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); + 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,