CS 3230 - Lab 4

AdvancedEnemyBot class

Specifications

Write a class that extends the EnemyBot class you wrote last week.

Details

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 "AdvancedEnemyBot.java" please declare a public class called AdvancedEnemyBot that extends EnemyBot. (Both files need to be in the same directory.)
  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();
      
    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();
      
  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;
    

Questions and Answers

Help! It won't compile!

Please look at last week'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 Robocode Lesson #4 for some pointers on how to dow that.

What do you want me to turn in?

The AdvancedEnemyBot.java 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.

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, your day has come. Be thankful that I'm giving you the code rather than making you figure it all out by yourself.