You may find the following information useful. These are links to Sun'sonline Java tutorials.
When you are ready to release your robot, you will want to package it, bundling the entire robot into a single JAR file. This is the de facto way to distribute Robocode robots on the Internet, and it is a requirement for entering it into RoboRumble. To package a robot, use the Robot - Package robot or team tool: Select the robot you want to package.
- What Is aClass? - very basic primer on class basics
- CreatingClasses - the main components of a class
Specifications
Write a class that can hold the information about an enemy robot.
Detail
- 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.)
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Layout of the code
Kindly lay out your code like so:
Questions and Answers
Do I derive this class from Robot or AdvancedRobot?
Neither. This is a standalone class that does not (explicitly) inheritfrom 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 atthe top of your EnemyBot.java file:
Help! It still won't compile!
You might notice that, even after you import everything from the robocodepackage, when you try to compile the code it spits out a bunch of 'could notresolve symbol' errors. This is because the compiler doesn't know where tofind the robocode classes that you're trying to use.
Solution: set the classpath. Look at p.137 in your book for anexplanation. Basically, you can set the classpath in one of two ways:
- Give the javac compiler a command-line argument: Instead of typinginstead typeAlternatively you could...
- Set the CLASSPATH environment variable: p.137 in your bookexplains how to do this. On a DOS/Win* box you can typeThat ';.' at the end means 'include the current directory in theclasspath'. You will surely want to do that. If you are using WinNT/XP/2K/03you 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. Justtype it in.
Now I can't compile a robot that uses the EnemyBot class!
The problem you are now experiencing concerns the way Java handlespackages. For starters, you need to be sure that both your robot and theEnemyBot class belong to the same package and that they live in a directorywith the same name as the package. Example, if my initials were 'mkw' and Iwas making a robot called MyBot, I would put the line:at the top of both EnemyBot.java and MyBot.java.
Next, to compile these files, you actually need to be one directoryup from the directory where the files live. So, change into thec:robocoderobots directory and type:Note: the reason why you should compile EnemyBot independently is so theright package / directory path gets written to the .class file. You shouldonly 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 thatyou could just make a ScannedRobotEvent member variable.
Answer: It will not work. Remember, primitives are passed by valueand 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 itand your code will throw a bunch of exceptions. ScannedRobotEvents are ephemeral so you have only abrief 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:robocoderobots[initials] directory becauseyou'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 otherrobots. You will surely want to read Improved Scanning 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 robotthat uses this class, but I do want to see it running around thescreen.