Re: Importing Custom Classes

Johannes Slotta <[email protected]> Fri, 17 Jul 2015 11:06:20 +0200
Newsgroups gmane.comp.java.robocode
Message-ID <CA+-w=RwU=z7utRB_v1NvwJQQ+j4DeS1qaErvE+DNUB4kZON1cg@mail.gmail.com>
Hi, you should not set myRobot to a new AdvancedRobot instance (which the
game cannot recognise), but to your own one given by the game. Do this by
using a custom constructor for EnemyData. HTH Johannes
Am 17.07.2015 07:55 schrieb "Mike Oliver" <[email protected]>:

> Hi Everyone,
> Back again for questions regarding custom classes.  I've broken out my bot
> into multiple files and am having troubles understanding where I've gone
> wrong.  When I run my bot I get:
>
> robocode.exception.RobotException: You cannot call the getHeadingRadians()
> method before your run() method is called, or you are using a Robot object
> that the game doesn't know about.
> at robocode._RobotBase.uninitializedException(_RobotBase.java:88)
> at
> robocode._AdvancedRadiansRobot.getHeadingRadians(_AdvancedRadiansRobot.java:40)
> at robocode.AdvancedRobot.getHeadingRadians(AdvancedRobot.java:1439)
> at mo.data.EnemyData.update(EnemyData.java:25)
> at mo.PewPew.onScannedRobot(PewPew.java:18)
>
> I'm trying to figure out how to declare my bot object properly so that
> methods like getHeadingRadians will work.
> Any input or suggestions is really appreciated.
> Thanks
> Mike
>
>
> Pew.Pew.java
> package mo;
> import mo.data.*;
>
> import robocode.*;
>
> public class PewPew extends AdvancedRobot
> {
>  private EnemyData enemy = new EnemyData();
>  public void run() {
> while(true) turnRadarRightRadians(Double.POSITIVE_INFINITY);
> }
>  public void onScannedRobot(ScannedRobotEvent e) {
> enemy.update(e);
> }
>     public void onRobotDeath(RobotDeathEvent e) {
>     enemy.update(e);
>     }
> }
>
> EnemyData.Java
> package mo.data;
> import mo.utils.*;
>
> import robocode.*;
>
> import java.util.HashMap;
> import java.util.LinkedHashMap;
>
> public class EnemyData {
>
> public static AdvancedRobot myRobot = new AdvancedRobot();
> public static LinkedHashMap<String, HashMap<String, Object>> enemy = new
> LinkedHashMap<String, HashMap<String, Object>>(5, 2, false);
>  protected double velocity;
> protected double bearing;
> protected double absBearing;
>  //onScannedRobot
> public void update(ScannedRobotEvent e) {
> String name = e.getName();
> if (!enemy.containsKey(name))
>     enemy.put(name, new HashMap<String, Object>());
>
> enemy.get(name).put("velocity", e.getVelocity());
> enemy.get(name).put("absBearing", myRobot.getHeadingRadians() +
> e.getBearingRadians());
> enemy.get(name).put("bearing", e.getBearingRadians());
> Utils.printMap(enemy); //print out debug info
> }
>  //onRobotDeath
> public void update(RobotDeathEvent e) {
> enemy.remove(e.getName());
> }
> }
>
> Utils.java
> package mo.utils;
>
> import java.util.HashMap;
> import java.util.LinkedHashMap;
> import java.util.Map;
>
>
> public class Utils {
>  //print out Map information
> public static void printMap(LinkedHashMap<String, HashMap<String, Object>>
> map) {
> for (Map.Entry<String, HashMap<String, Object>> cursor : map.entrySet())
> System.out.println(cursor.getKey() +"="+ cursor.getValue() +"\r");
> System.out.println("\r");
> }
> }
>
>
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "robocode" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "robocode" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.