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.
- 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.)
- Declare 2 new private variables in AdvancedEnemyBot called:
x and y. They will be of type double.
- Add the accessor methods getX() and getY(); they
will return the appropriate variables.
- 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.
- Make a constructor for the class which simply calls the
reset() method. (Your own, not the parent class'.)
- 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:
- 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.)
- 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;
- 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();
- 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();
- 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;
- 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.