Robocode 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.)

By using the bearing, you can easily turn toward something else like so:

turnRight(event.getBearing());
Which is a pretty common idiom. Note that because a bearing is a value from -180 to 180, calling turnRight() will actually make you turn left if the bearing is negative -- this is by design and it is what you want to have happen.

Sample robot: BearingBot - an example of how to turn your robot toward an enemy and ram into him. (Match him up against one other robot that moves a little bit like Tracker, SpinBot or Crazy.)

The sample robot "RamFire" further demonstrates the virtues of ramming.