CS 3230 - Lab 3

EnemyBot class

This week (and weeks hereafter) I'm going to try to blend together the lab assignments and robocode lessons.

Specifications

Write a class that can hold the information about an enemy robot.

Detail

  1. In a new file called "EnemyBot.java" make a public class called EnemyBot. (Remember: the filename must be the same as the class name + a .java extension.)
  2. Add the following private variables to the class: bearing, distance, energy, heading, name, velocity. All of these will be of type double except for name which will be of type String.
  3. Add the following public accessor methods to the class: getBearing(), getDistance(), getEnergy(), getHeading(), getName(), getVelocity(). These will all return the values in the private variables.
  4. Implement a state-change method called update which takes a ScannedRobotEvent as a parameter. Call the ScannedRobotEvent's methods (same names as the ones in step #3) to set your private variables (step #2). The update method will return void.
  5. Implement another state-change method called reset which sets the name variable to the empty string ("") and all the variables of type double to 0.0. The reset method will also return void.
  6. Implement a (state-reporting) accessor method called none which will return true if name is "" or false otherwise. (Remember to use the equals() method of the String class.) Basically, this method will return true if the reset method was just called.
  7. Lastly, implement a public constructor which just calls reset. Note: the constructor must be the same name as the class. Also, constructors never specify a return value.

Questions and Answers

Do I derive this class from Robot or AdvancedRobot?

Neither. This is a standalone class that does not (explicitly) inherit from anything.

Do I make a main() method or a run() method in here?

Neither. Again, this is a standalone class.

Help! It won't compile!

Be sure to import stuff from the robocode package by putting this line at the top of your EnemyBot.java file:

 import robocode.*; 

Help! It still won't compile!

You might notice that, even after you import everything from the robocode package, when you try to compile the code it spits out a bunch of "could not resolve symbol" errors. This is because the compiler doesn't know where to find the robocode classes that you're trying to use.

Solution: set the classpath. Look at p.137 in your book for an explanation. Basically, you can set the classpath in one of two ways:

  1. Give the javac compiler a command-line argument: Instead of typing
     javac EnemyBot.java 
    instead type
     javac -classpath c:\robocode\robocode.jar EnemyBot.java 

    Alternatively you could...

  2. Set the CLASSPATH environment variable: p.137 in your book explains how to do this. On a DOS/Win* box you can type
     set CLASSPATH=c:\robocode\robocode.jar;. 

    That ";." at the end means "include the current directory in the classpath". You will surely want to do that. If you are using WinNT/XP/2K/03 you should set this in the "System" proglett in the control panel.

    Warning: If you cut-n-paste the above commands into a DOS prompt, you may include a trailing space which will give you no end of headaches. Just type it in.

Now I can't compile a robot that uses the EnemyBot class!

The problem you are now experiencing concerns the way Java handles packages. For starters, you need to be sure that both your robot and the EnemyBot class belong to the same package and that they live in a directory with the same name as the package. Example, if my initials were "mkw" and I was making a robot called MyBot, I would put the line:

package mkw;
at the top of both EnemyBot.java and MyBot.java.

Next, to compile these files, you actually need to be one directory up from the directory where the files live. So, change into the c:\robocode\robots directory and type:

javac mkw\EnemyBot.java
javac mkw\MyBot.java
Note: the reason why you should compile EnemyBot independently is so the right package / directory path gets written to the .class file. You should only need to do this once.

Can't I just store the ScannedRobotEvent?

You may have noticed that I'm asking you to duplicate many of the methods (and data) that are found in the ScannedRobotEvent class and are thinking that you could just make a ScannedRobotEvent member variable.

Answer: It will not work. Remember, primitives are passed by value and objects are passed by reference. If you try to store the reference, the Robocode engine will destroy the object before you get a chance to use it and your code will throw a bunch of exceptions. ScannedRobotEvents are ephemeral so you have only a brief time to use them.

If you really don't believe me, you're welcome to try... :-)

How big does the file have to be?

I was able to meet the above specifcations in about 45 lines of code (including comments and curlies). Lots of it was cut 'n paste, too.

Where do I put this file?

Put it in the c:\robocode\robots\[initials] directory because you're going to use it with your robot.

You do need to print it up and give me a copy, though.

How do I show you this working?

Write a robot that uses it and show me it running around killing other robots. You will surely want to read Robocode Lesson #3 for advice on how to do this.

What do you want me to turn in?

The EnemyBot.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.