This week (and weeks hereafter) I'm going to try to blend together the lab assignments and robocode lessons.
Write a class that can hold the information about an enemy robot.
Neither. This is a standalone class that does not (explicitly) inherit from anything.
Neither. Again, this is a standalone class.
Be sure to import stuff from the robocode package by putting this line at the top of your EnemyBot.java file:
import robocode.*;
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:
javac EnemyBot.javainstead type
javac -classpath c:\robocode\robocode.jar EnemyBot.java
Alternatively you could...
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.
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.javaNote: 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.
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... :-)
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.
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.
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.
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.