Robocode Lessons
Translations of this material:
- into Russian: Уроки Robocode. 98% translated in draft. Almost done, let's finish it!
-
Submitted for translation by RANUX 27.02.2009
Published 2 years, 2 months ago.
Text
Description of Robocode Project
You will all need to write a little fighting robot and turn it in the class period after the Midterm.
Stuff I'm expecting to see:
* At the very least, a class derived from Robot or AdvancedRobot. Please include part of your name in the name of your robot. Examples include: "PingBot", "LanceAlot", "XhinStriker", "The Valinator", "AJ-D2", "RoboWendy", or something along those lines.
* Code that moves your robot around the battlefield.
* Code that scans for other robots (turns the radar), including event-handling code that does something when you scan another robot.
* Code that turns the turret and fires at one of your enemies! (where enemies == your classmates!)
What do I do with this robot after I write it?
The class after the Midterm, we will meet in the lab and have a big "showdown" where we load all of your robots onto the instructor's computer and have 3 heats with 10 rounds of battle each. These battles will be displayed on the projector for everyone to see. (Your grade is not dependent on winning the battle, but everyone will think you're a little girly-man if you lose.)
Also, on the same night, I want you to turn in to me a printout of your robot's code with your name on it.
Lesson #1: MyFirstRobot
Creating a Robot
Here's the fun stuff: This is what Robocode is all about!
Creating a robot can be easy. Making your robot a winner is not. You can spend only a few minutes on it, or you can spend months and months. I'll warn you that writing a robot can be addictive! Once you get going, you'll watch your creation as it goes through growing pains, making mistakes and missing critical shots. But as you learn, you'll be able to teach your robot how to act and what to do, where to go, who to avoid, and where to fire. Should it hide in a corner, or jump into the fray? Picture of the robot named MatBot, which is one Mathew Nelson's robots
My First Robot
Ready to create your first robot? I hope you'll find that it's easy, straightforward, fun, and addictive!
Robocode ships with a number of sample robots that you can look at for ideas, and to see how things work. You can use the Robot Editor to look at all of them.
In this section, we'll use the Robot Editor to create your very own, brand new robot.
The Robot Editor
The first step is to open up the Robot Editor. From the main Robocode screen, click on the Robot menu, then select Editor.
When the editor window comes up, click on the File menu, then select New Robot.
In the dialogs that follow, type in a name for your robot, and enter your initials.
Voila! You now see the code for your own robot.
A New Robot
This is what you should be looking at (names have been changed to protect the innocent):
package man;
import robocode.*;
public class MyFirstRobot extends Robot {
public void run() {
while (true) {
ahead(100);
turnGunRight(360);
back(100);
turnGunRight(360);
}
}
public void onScannedRobot(ScannedRobotEvent e) {
fire(1);
}
}
We're only concerned with the bits in bold here... you won't need to change anything else. Not that much, right?
By the way, if you're REALLY concerned about the rest of it, right now, I describe it here:
package man;
import robocode.*;
public class MyFirstRobot extends Robot {
public void run() {
}
}
import robocode.*; Tells Java that you're going to use Robocode objects in your robot.
public class MyFirstRobot extends Robot Tells Java: "The object I'm describing here is a type of Robot, named MyFirstRobot".
public void run() { } The game calls your run() method when the battle begins.
{ } "Curly brackets" ( { } ) group things together. In this case, they're grouping together all the code for the robot.
Let's move somewhere
Let's add a couple lines so that it will do something.
First, we'll examine the run() method:
while (true) {
ahead(100);
turnGunRight(360);
back(100);
turnGunRight(360);
}
while(true) { } means: "While the condition true is true, do everything between the curly brackets { }".
Since true is always true (no kidding? ;-), it means: "Do the stuff inside my curly brackets, forever".
So this robot will:
1. move ahead 100 pixels
2. turn the gun right by 360 degrees
3. move back 100 pixels
4. turn the gun left/back by 360 degrees
The robot will continue doing this over and over and over, until it dies, due to the while(true) statement.
Not so bad, right?
Fire at Will!
When our radar scans a robot, we want to fire:
public void onScannedRobot(ScannedRobotEvent e) {
fire(1);
}
The game calls your onScannedRobot method whenever you can see another robot. It sends along an event that can tell us lots of information about the robot -- its name, how much life it has, where it is, where it's heading, how fast it's going, etc.
However, since this is a simple robot, we're not going to look at any of that stuff. Let's just fire!
Compile your robot
First, save your robot by selecting the Save in the File menu. Follow the prompts to save your robot.
Now, compile it by selecting Compile in the Compiler menu.
If your robot compiles without any errors, you can start a new battle with your robot. Start a new battle by selecting New in the Battle menu. If you cannot see your robot, you might have to refresh the list of robots by pressing F5. Add your robot to the battle together with at least one other robot as e.g. sample.Target, and press the Start Battle button to let the games begin!
Lesson #2: Battlefield Basics
In this lesson, we describe the basics of getting around the battlefield.
Robot Anatomy
Your robot consists of three parts: the "tank" (body), the gun, and the radar.
Image taken from Rock 'em, sock 'em Robocode!
Each of these parts can be moved independently from each other, provided you call the setAdjustGunForRobotTurn(true) or setAdjustRadarForRobotTurn(true) methods.
Sample robot: AnatomyBot - shows each of the three parts turning independently. Incidentally, it also shows how you can print debug statements. (Click on the button with the robot's name on the right to see debug output.)
Notice that the tank moves pretty slow, the gun turret moves faster, and the radar moves faster still.
Battlefield Measurements
The battlefield is laid out in a cartesian plane with the origin in the lower-left corner. Degree measurements are top-wise; IOW 0/360 is at the top.
Image taken from Rock 'em, sock 'em Robocode: Round 2
Sample robot: WallBanger - demonstrates how you can move around the battlefield and hit the walls.
Note that even though we called ahead(10000) the call returns as soon as the robot hits a wall. (See the Robocode API.)
The sample robot "Walls" will also show you how a robot can interact with the battlefield walls.
Bearings
You can get your degree heading by calling getHeading(), but there are occasions where you get a Bearing, that is to say, a degree measurement from -180 to 180 that represents your offset from something (a wall, another robot, etc.)
