Robocode Homework: AdvancedEnemyBot class

You may find the following information useful. These are links to Sun's online Java tutorials.


Write a class that extends the EnemyBot class you wrote previously.


These steps are deliniated in such a way that you should be able to compile after each step to make sure your code is working properly.

  1. In a file called "" please declare a public class called AdvancedEnemyBot that extends EnemyBot. (Both files need to be in the same directory AND need to have the same package line at the top.)
  2. Declare 2 new private variables in AdvancedEnemyBot called: x and y. They will be of type double.
  3. Add the accessor methods getX() and getY(); they will return the appropriate variables.
  4. Override the parent class' reset() method and write the following code inside it: first, call the parent's reset() method as super.reset() so that it will blank out all of its variables; second, set all the AdvancedEnemyBot's private class variables to 0.
  5. Make a constructor for the class which simply calls the reset() method. (Your own, not the parent class'.)
  6. Write a new update() method which takes two parameters: a ScannedRobotEvent (call it e) and a Robot (call it robot -- ain't case-sensetivity grand?). (Note that you are not overriding the parent class' update() method because it takes only one parameter.)

    Inside the AdvancedEnemyBot's update() method, please do the following:

    1. Call the parent class' update() method with super.update(), passing it the ScannedRobotEvent that was passed to this method. (And yes, I realize that using the super keyword is unnecessary here, but it makes the code more obvious and self-documenting.)
    2. Compute the absolute bearing between the robot and the enemy with the following code:
      double absBearingDeg = (robot.getHeading() + e.getBearing());
      if (absBearingDeg < 0) absBearingDeg += 360;
    3. Set the x variable using the following code:
      // yes, you use the _sine_ to get the X value because 0 deg is North
      x = robot.getX() + Math.sin(Math.toRadians(absBearingDeg)) * e.getDistance();
      In a nutshell, this line computes the lentgh of the opposite side of a triangle (which may actually be negative in some cases), and then offsets it by our robot's X value.
    4. Set the y variable using the following code:
      // yes, you use the _cosine_ to get the Y value because 0 deg is North
      y = robot.getY() + Math.cos(Math.toRadians(absBearingDeg)) * e.getDistance();
      Similarly, this line computes the lentgh of the adjacent side of a triangle (which may actually be negative in some cases), and then offsets it by our robot's Y value.
  7. Make an accessor method called getFutureX() which takes a long parameter (call it when) and returns a double. Use the following code to implement it:
    return x + Math.sin(Math.toRadians(getHeading())) * getVelocity() * when;
  8. Lastly, make an accessor method called getFutureY() which takes a long parameter (call it when) and returns a double. Use the following code to implement it:
    return y + Math.cos(Math.toRadians(getHeading())) * getVelocity() * when;
    Note that getFutureX() and getFutureY() are much like the getX() and getY() above except that it uses Rate x Time (getVelocity() * when) instead of distance (e.getDistance())

Questions and Answers

Help! It won't compile!

Please look at the EnemyBot lab's Q & A for answers to this.

How do I show you this lab working?

Make a robot that uses the AdvancedEnemyBot class you wrote and show me it running around the screen, tracking enemies and shooting at them. I imagine you'll want to look at Improved Targeting for some pointers on how to dow that.

What do you want me to turn in?

The file with your name, the lab # and the course # at the top of the page in comments. I do not need to see a printout of a robot that uses this class, but I _do_ want to see it running around the screen shooting (predictively) at other bots.

Can you explain the Trigonometry stuff to me?

Yeah, probably.

How come we're learning so much math in a Java class?

Remember back in your math classes when you used to ask "When are we ever gonna use this stuff?" Well class, ask not for whom the bell tolls, your day has come. Be thankful that I'm giving you a ton of the code rather than making you figure it all out by yourself.

He doesn't always hit

Technically, not a question, but I'll take it anyway. Answer: There is no fool-proof way to predictively target an opponent. There are too many variables at work (such as starting and stopping of the target bot) and no way to anticipate with 100% accuracy where your enemy will be. If you want to try to improve it, you're welcome to, but I want you to know right now that you won't be able to hit every time.

Help! My bot's on loco weed!

You may find that your robot is not predicting properly. He may be turning around the opposite way, not firing properly into a specific quadrant of the screen or something similar.

The most likely reason why this is happening is because you've used the wrong math operator someplace (i.e. you used a '*' when you should have used a '+' -- Alterhatively, you may have used an 'x' someplace when you should have used a 'y'.) Double-check the above instructions and make sure you've got the right code in the right place.

If, after checking, you're still having a devil of a time, don't worry about it too much. Next week I'll show you how to make a robot that prints output to a file. This will help you to troubleshoot your bot by getting a view of what's going on inside. For right now, you can use the out.println() to print some debug noise into your bot's output window.