robocode.Robot
class. In this section, we'll cover each of the available commands, by category.turnRight(double degree)
and turnLeft(double degree)
turn the robot by a specified degree. ahead(double distance)
and back(double distance)
move the robot by the specified pixel distance; these two methods are completed if the robot hits a wall or another robot. turnGunRight(double degree)
and turnGunLeft(double degree)
turn the gun, independent of the vehicle's direction. turnRadarRight(double degree)
and turnRadarLeft(double degree)
turn the radar on top of the gun, independent of the gun's direction (and the vehicle's direction).setAdjustGunForRobotTurn(boolean flag)
: If the flag is set to true, the gun will remain in the same direction while the vehicle turns. setAdjustRadarForRobotTurn(boolean flag)
: If the flag is set to true, the radar will remain in the same direction while the vehicle (and the gun) turns. setAdjustRadarForGunTurn(boolean flag)
: If the flag is set to true, the radar will remain in the same direction while the gun turns. It will also act as if setAdjustRadarForRobotTurn(true)
has been called.getX()
and getY()
get the current coordinate of the robot. getHeading()
, getGunHeading()
, and getRadarHeading()
get the current heading of the vehicle, gun, or radar in degrees. getBattleFieldWidth()
and getBattleFieldHeight()
get the dimension of the battlefield for the current round.fire(double power)
and fireBullet(double power)
are used to fire a bullet with the specified energy (fire power). The fireBullet()
version of the call returns a reference to a robocode.Bullet
object that can be used in advanced robots.Robot
class has default handlers for all of these events. However, you can override any of these 'do nothing' default handlers and implement your own custom actions. Here are some of the more frequently used events:ScannedRobotEvent
. Handle the ScannedRobotEvent
by overriding the onScannedRobot()
method; this method is called when the radar detects a robot. HitByBulletEvent
. Handle the HitByBulletEvent
by overriding the onHitByBullet()
method; this method is called when the robot is hit by a bullet. HitRobotEvent
. Handle the HitRobotEvent
by overriding the onHitRobot()
method; this method is called when your robot hits another robot. HitWallEvent
. Handle the HitWallEvent
by overriding the onHitWall()
method; this method is called when your robot hits a wall.run()
method, as well as any other helper methods that you may create. run()
method is called by the battle manager to start the robot's life. It typically consists of two areas (designated Area 2 and Area 3 in Listing 1) where you can add code. Area 2 is where you will place code that will run only once per robot instance. It is often used to get the robot into a pre-determined state before starting repetitive action.run()
method implementation. Here, within an endless while
loop, we'll program the repetitive action that a robot may be involved in.run()
logic. It's also where you add any event handlers that you wish to override. For example, the code in Listing 1 handles the ScannedRobot
event and simply fires directly at the robot whenever one is detected by the radar.turnLeft(getHeading())
.ahead(1000)
. It will stop when it hits a wall or robot. Then we turn right using turnRight(90)
. As this is repeated, the robot will basically trace out the walls in a clockwise direction.ScannedRobot
event and firing at the robot that is found directly, we also detect the HitByBullet
event and turn 180 degrees (going clockwise and counterclockwise, alternately) when we get hit.Robot
and add new functionalities that can be used to build robots. Robocode supplies a subclass of Robot
, called AdvancedRobot
, which enables asynchronous API calls. A description of the AdvancedRobot
class is beyond the scope of this article, but I encourage you to experiment with this advanced class when you are comfortable with the operation of the basic Robot
class. robocode.Robot
class's methods). Physically, each robot is an independent Java thread, and the run()
method contains the logic that will be executed on the thread.robocoode.Robot
class. This will typically block the robot thread via an Object.wait()
call. SkippedTurnEvent
is generated into a robot's event queue to notify advanced robots.