DOC

Robocode.NETCreate a .NET robot with Visual Studio

By Eric Watson,2014-11-16 06:33
6 views 0
Robocode.NETCreate a .NET robot with Visual Studio

    This page is a tutorial describing how to create a .NET robot with Visual Studio C# 2008 Express Edition.

    Contents

    [hide]

    ; 1 Creating a .NET robot in Visual Studio

    o 1.1 Prerequisites

    o 1.2 Creating a VS solution for your robot

    o 1.3 Setting up Project References

    o 1.4 Creating your first robot class

    o 1.5 The robot naming convention used in Robocode

    o 1.6 Setting assembly name and default namespace

    o 1.7 Running the robot in Robocode

    o 1.8 Documentation

    o 1.9 Debugging

    o 1.10 Known issues and solutions

    ; 2 See also

    o 2.1 Related tutorials

    o 2.2 Robocode API

    o 2.3 Tutorials

    o 2.4 News and Releases

    o 2.5 Home pages

    Creating a .NET robot in Visual Studio

    From version 1.7.2.0, Robocode supports robots that run under Microsoft .NET framework CLR. Note that .NET robots will only be able to run on operating systems that support the .NET framework 2.0. For now, Mono is not supported due to current

    limitations of jni4net.

    This tutorial assumes that you are familiar with .NET programming. In addition, this tutorial is made for C# programmers, but it should be easy to use this tutorial for Visual Basic .NET or other .NET programming languages instead.

    Prerequisites

    1. Robocode and Java must be installed on your system (see System Requirements

    and Download).

    2. .NET plug-in for Robocode must be installed on top of your existing Robocode

    directory. The .NET plug-in is named like this: robocode.dotnet-xxxx-setup.jar,

    and you can install it by double-clicking on it, which will start the installer.

    3. Visual Studio 2005 or newer is required, but Visual Studio 2008 is strongly

    recommended. You can download one of the Express Edition, which comes for

    free.

    Note that you don't need Visual Studio for developing .NET robots. You can use Microsoft .NET SDK 2.0 if you wish. However, this tutorial will make use of Visual

    Studio C# 2008 Express Edition, and hence already have it downloaded and installed.

    Creating a VS solution for your robot

    Open Visual Studio and create a new project (File -> New Project... Ctrl+Shift+N), and

    select the template named 'Class Library'. Specify a name for your project that makes sense, e.g. 'MyProject', and press OK.

    Robocode is only able to load .NET robot from .dll file, so your project must be 'Class Library' that is assembled into a .dll. file. In this tutorial, we will use 'MyProject' as project name.

    Next, save the project into a new solution to any location of your choice. This way you know exactly where it is located, which will be important later when you want to run your robot inside Robocode. To do this you have to select 'MyProject' in Solution Explorer and select File -> Save MyProject as...

    C:\ and name our entire In this example, we will save our project MyProject under

    solution 'MySolution'. Note that a single solution can contain multiple projects, which is very useful if you want to develop more robots.

Setting up Project References

    Make sure that you have the Solution Explorer view open, which is typically located at the right side of VS. If it is not present, you can open it from the menu (View -> Solution Explorer Ctrl+W, S).

You need to add a reference to the robocode.dll file located under the \libs folder of

    your home folder for Robocode, e.g. C:\robocode (default home dir of Robocode).

    Otherwise, Visual Studio will not know anything about the robot API provided with Robocode, and hence will not be able to build a Robocode robot for you. Right-click the References in the Solution Explorer and select 'Add Reference...'. In the

    \libs Add Reference dialog that appears, select the Browse pane and browse into the folder of your robocode home folder.

Now you select the robocode.dll file and press OK.

    When this is done, 'robocode' should show up under the References of your Solution Explorer.

Creating your first robot class

    Now we are ready for creating our main robot class. In the Solution Explorer, a class named Class1.cs has already been provided and contains some example code. Delete this class by right-clicking on it in the Solution Explorer, select Delete and press OK on the dialog that follows.

    Now we create a new class for your robot called MyRobot.cs. Right-click on MyProject

    in the top of the Solution Explorer and select Add -> Class...

    On the Add New Item dialog that shows up, select 'Class' and set the name of this class in the buttom of the dialog to MyRobot.cs and press Add.

    Note: If you need more classes for your robot later, you can add these classes to your solution in the same way.

    After you have added your new class, MyRobot.cs, this source file will be automatically opened in Visual Studio and ready to be edited.

    Your new source file will contain this initial code:

     using System;

     using System.Collections.Generic;

     using System.Linq;

     using System.Text;

     namespace MyProject

     {

     class MyRobot

     {

     }

     }

    It contains some general 'using' declarations to standard system libraries, and it defines a class named MyRobot within the namespace named MyProject. We are now ready to put some additional code into our source file so our robot can make some action. First we need to add a new 'using' declaration for Robocode, which we can add right after the 'using System' declarations:

     using Robocode;

    This will give your robot access to the public Robocode API.

    Secondly, you should change the namespace so MyProject is replaced with your initials, nickname, handle or similar. In this example, we use Fnl's initials which is FNL:

     namespace FNL {

    Then we need to specify which robot type your class my inherit from. In this example we use the simple robot type named Robot:

     class MyRobot : Robot

    The main method of your robot is the Run method. In this method you control your robot

    by calling robot command like e.g., Ahead(), TurnLeft() or TurnGunRight() etc.

    Note that it is important that your Run method runs in a loop, meaning that it should have a loop likes while (true) while will make your robot run forever.

    If you do not have a loop, your robot will stop as soon as the Run method has finished the execution.

    If you need some initialization for your robot that should be executed when your robot is started, you should put this code before your loop.

     // Your Run method must be public, is void, and must override the Run()

    method from the super class

     public override void Run()

     {

     // Perform your initialization for your robot here

     while (true)

     {

     // Perform robot logic here calling robot commands etc.

     }

     }

    Here is an example of how the Run method could look like:

     // The main method of your robot containing robot logics

     public override void Run()

     {

     // -- Initialization of the robot --

     // Here we turn the robot to point upwards, and move the gun 90 degrees

     TurnLeft(Heading - 90);

     TurnGunRight(90);

     // Infinite loop making sure this robot runs till the end of the battle round

     while (true)

     {

     // -- Commands that are repeated forever --

     // Move our robot 5000 pixels ahead

     Ahead(5000);

     // Turn the robot 90 degrees

     TurnRight(90);

     // Our robot will move along the borders of the battle field

     // by repeating the above two statements.

     }

     }

    Your robot is also able to react on events. In order to react on a specific event, you must

    provide an event handler for the specific event. Here is an example of a simple event handler that is specific to the ScannedRobotEvent,

    which occur when your robot scans another robot:

     // Robot event handler, when the robot sees another robot

     public override void OnScannedRobot(ScannedRobotEvent e)

     {

     // We fire the gun with bullet power = 1

     Fire(1);

     }

    If this example, the robot fires a bullet as soon as it sees another robot.

    Let us put everything together. Our complete robot follows here, and you could copy &

    paste this code into your MyRobot.cs source file in order to try it out:

     // Access to standard .NET System

     using System;

     using System.Collections.Generic;

     using System.Linq;

     using System.Text;

     // Access to the public Robocode API

     using Robocode;

     // The namespace with your initials, in this case FNL is the initials

     namespace FNL

     {

     // The name of your robot is MyRobot, and the robot type is Robot

     class MyRobot : Robot

     {

     // The main method of your robot containing robot logics

     public override void Run()

     {

     // -- Initialization of the robot --

     // Here we turn the robot to point upwards, and move the gun 90 degrees

     TurnLeft(Heading - 90);

     TurnGunRight(90);

     // Infinite loop making sure this robot runs till the end of the battle round

     while (true)

     {

     // -- Commands that are repeated forever --

     // Move our robot 5000 pixels ahead

     Ahead(5000);

     // Turn the robot 90 degrees

     TurnRight(90);

     // Our robot will move along the borders of the battle field

     // by repeating the above two statements.

     }

     }

     // Robot event handler, when the robot sees another robot

     public override void OnScannedRobot(ScannedRobotEvent e)

     {

     // We fire the gun with bullet power = 1

     Fire(1);

     }

     }

     }

The robot naming convention used in Robocode

    In Robocode we use a naming convention, where you must put your robot into a namespace, and this namespace should be somewhat unique and identify you, e.g., your initials, nickname or handle.

Report this document

For any questions or suggestions please email
cust-service@docsford.com