introduction to a robocode robot

As I'm going to be illustrating some code for a robot using the robocode library I thought I'd illustrate a few main things about how robots are structured in the .NET robocode integration assembly (robocode is in java).
There is also some good literature on the wiki:
http://robowiki.net/wiki/Robocode/.NET/Create_a_.NET_robot_with_Visual_Studio#Robocode_API

There are many members to the robocode robot, but here are the main ones:
(a full list available from this well formatted API page robocode robot api )

Run()
This virtual method is the first thing called when the robot is activated. Here I can hook up the first commands for the robot and introduce a loop or similar way of entering commands.

OnScannedRobot() - informs when the radar detects another robot, giving information on that robot's position

OnHitRobot(), OnHitWall(), OnHitByBullet() - a few of many events that enable you to react to certain situations

Ahead(pixels), Back(pixels), TurnLeft(), TurnRight() - moves the robot

Scan() - enables you to scan after you've stopped moving or to interrupt the current OnScannedEvent

Fire(power), FireBullet(power) - fires using current gun position

TurnGunLeft(), TurnGunRight() - moves the robot's gun independently of the body

TurnRadarLeft(), TurnRadarRight() - moves the robot's radar independently of the other parts

Energy, GunHeat, Heading, GunHeading, RadarHeading, Velocity, X, Y - various properties on the robot

So there's lots of stuff to work with and this is just the most relevant ones worth posting in this quick introduction.

Here's an example of a robot in action, taken from the C# samples provided with robocode:

    public class MyFirstRobot : Robot
    {
        public override void Run()
        {
            while (true)
            {
                Ahead(100); // Move ahead 100
                TurnGunRight(360); // Spin gun around
                Back(100); // Move back 100
                TurnGunRight(360); // Spin gun around
            }
        }

        public override void OnScannedRobot(ScannedRobotEvent e)
        {
            Fire(1);
        }

        public override void OnHitByBullet(HitByBulletEvent e)
        {
            TurnLeft(90 - e.Bearing);
        }
    }



0 comments: