When you first start Robocode you will see the following screen:

 

 

The menu options are as follows:

 

 

The two main menus that you will be using are shown below:

 

New – starts new battles

Open – allows you to open saved, previous battles

Save – saves battles

Exit – to leave the program

 

Editor – for creating code to instruct the robots

Import – to  download robots from the web

 

Package for upload – packaging robots up robots so that they can be uploaded to the web

Team – for creating robot teams

 

 

 

 

Starting A Battle

 

 

Robocode comes with a number of ready-made robots that you can use.

Your first task will be to use some of the ready-made robots, to help you get used to the robocode environment.

 

 

To start your very first battle:

 

  • From the Battle menu select the New option

    The New Battle window will appear (as shown below)


 

 

 

 

 

 

 

Available Robots:

 

 

The robots available to you will depend on the Packages that have been selected. Note that, once you start creating your own robots, a package that relates to you will also appear in the packages listing.

 

 

.

 

 

  • Click on (All)

    This will display All available of the robots available to you.

 

 

 

 

 

Note the dot notation in the Robots section i.e. sample.Corners

 

 

The word before the dot identifies which of the packages the robot belongs to – in this example, the robot belongs to the sample package.

 

The word following the dot provides the actual name of the robot – e.g. crazy

 

  • In the Robots list, select the sample.Crazy robot
  • click the Add -> button

    The sample.Crazy robot is added to the Selected Robots listing on the right.

 

 

You now need to add another robot so that a battle can commence.

 

  • In the Robots list, select another robot from the list of the other ready-made robots. Their names should give some indication as to their actions)
  •  
  • Click the Add -> button

 

 

 

 

 

The Buttons:

A summary of the functions of the buttons:

 

The Add -> button lets the user add the currently selected robot to the battle – robots are added to the battle one at a time.

The Add All -> button enables the user to add all robots from the currently selected package to the battle.

 

  • Now change the Number of Rounds to be fought to 3

 

 

The <- Remove and <- Remove All enable you to remove a single selected robot or to remove all robots from the battle.

 

The Back and Next buttons will take you from tab to tab i.e. from the Robots to Battlefield to Rules tabs.

 

  • Click Start Battle to start your battle!

 

The robots will fight for ten rounds unless you have changed this setting.

 

If you wish to Pause or Stop the fight at any point, you can click those  buttons in the bottom left corner.

 

 

 


 

It is possible to keep track of a robot’s performance.

 

Simply click the button for the required robot (shown on the right) and the following information window will be displayed.

 

This will be especially important to you once you start creating your own robots with their own behaviours.

 

 

 

By the end of the battle the information window will display something similar to the following:

 

 

 

 

 

 

 

Note the Kill Robot button. Clicking this will ‘self destruct’ your robot.

 

 

Note that a terminal window always runs in the background when you are running Robocode. It is actually the first window that opens when you start Robocode. Do not close this window.

 

Note that the terminal window also provides feedback as to the state of battle. The two images shown below illustrate the content of the terminal window at different stages of the battle.

 

 

 

 

 

 

Finally, when the battle finishes, you will see a window similar to the one below. The results of the battle, in this example, held over 10 rounds, can then be analysed. Look at Robot Name to identify your own robot and then check the Total Score.

 

Note that there are a number of further options that you can also check.

 

 

 

 

In this particular example RamFire defeated Crazy by 1225 points to 875!

 

 

So much for the comeback king!!!

 

 

 


Looking at Existing Robots

 

The best way to learn how to construct robots with certain behaviours is to look at how other robots have been constructed.

 

This is where the Java programming language comes into play. As you will see, the robots are defined with certain ‘behaviours’ – characteristics, actions that they take or things that they do. These behaviours are parceled up into small blocks of code that in Java are called methods.

 

 

  • From the Robot menu select the Editor option

    This will open the Robocode Editor window as shown below

 

  • From the File menu select the Open option


  • The Open window will be displayed. Note that all packages that have been created within the robots directory will be displayed

 

 

 

  • Select the sample directory and click Open
  • Select MyFirstRobot.java and again click Open
     

 

 

The code for MyFirstRobot.java will be displayed.

 

  • Have a look at the code and see if you can work it out

 

 

 

 

Creating A Robot

 

When you are ready, you can create your own robot and send it in to battle to see how it fares against other robots.

 

  • From the Robot menu select the Editor option

    This will open the Robocode Editor window as before

 

  • In the Editor, from the File menu select New and then Robot

 

You will now be asked to enter the name of your robot.  Ensure that it starts with a capital letter.

 

 

 

  • Enter the name for your robot and then click OK

    You will see the following dialog that prompts you for a name for the package

 

Note that you would ideally add all of the robots that you create to the same package.  So you can stick to the same initials each time.

 

  • Enter your initials and then press OK

    You will see the code for your very own Robocode robot

 

  • Have a look at the code – you will see that you have been provided with a very simple robot to start with

    A version of the code is shown below

Note that // in Java denotes a single line comment. A comment provides information to the user - i.e. by outlining what is happening in the code - but means nothing to the computer and is ignored.

 

 


 

 

package kas;

import robocode.*;

//import java.awt.Color;

 

/**

 * Teacherbot - a robot by (your name here)

Rectangular Callout: This part of the code is to set up your robot’s behaviours. It will go ahead 100, turn the gun around 360 degrees, go back 100 and turn the gun right 360 degrees. */

public class Teacherbot extends Robot

{

            /**

             * run: Teacherbot's default behavior

             */

            public void run() {

                        // After trying out your robot, try uncommenting the import at the top,

                        // and the next line:

                        //setColors(Color.red,Color.blue,Color.green);

                        while(true) {

                                    // Replace the next 4 lines with any behavior you would like

                                    ahead(100);

                                    turnGunRight(360);

                                    back(100);

                                    turnGunRight(360);

                        }

            }

 

            /**

             * onScannedRobot: What to do when you see another robot

Rectangular Callout: This dictates what happens when another robot is scanned              */

            public void onScannedRobot(ScannedRobotEvent e) {

                        fire(1);

            }

 

            /**

             * onHitByBullet: What to do when you're hit by a bullet

             */

   
Public void onHitByBullet(HitByBulletEvent e) {

                        turnLeft(90 - e.getBearing());           

}

           

}

 

 

The code shown below will be repeated for the life of the robot because of the while(true) code.  This line should never be changed but the lines shown below can be changed.

 

 

ahead(100);

turnGunRight(360);

back(100);

turnGunRight(360);

 

To understand the above code, the diagram below shows the robot’s anatomy.  This diagram is fromfrom Rock 'em, sock 'em Robocode!

 

 



The radar is used to scan for robots.  Once a robot has been scanned, it can be attacked.

 

 

 

 


 

Here are some more basic commands.  The text in italics must be replaced by an appropriate number or degree.

 

ahead(degrees) - Moves your robot forwards.


back(degrees) - Moves your robot backwards.


fire(number) - Makes your robot fire a bullet.


turnLeft(degrees) - Makes your robot turn left.


turnRight(degrees) - Makes your robot turn right.


turnGunLeft(degrees) - Makes your robots gun rotate to the left.


turnGunRight(degrees) - Makes your robots gun rotate to the right.


turnRadarLeft(degrees) - Makes your robots radar rotate to the left.


turnRadarRight(degrees) - Makes your robots radar rotate to the right.

 

  • Try out some of these codes by adding them to the public void run() section of the code as shown below

 

 

public void run() {

                        // After trying out your robot, try uncommenting the import at the top,

                        // and the next line:

                        //setColors(Color.red,Color.blue,Color.green);

                        while(true) {

                                    // Replace the next 4 lines with any behavior you would like

This is where you need to add new commands for the robot’s general behaviour.  You can also delete any of the ones that are there

 
 


                                    ahead(100);

                                    turnGunRight(360);

                                    back(100);

                                    turnGunRight(360);

                        }

            }

 

                                                  

Before you can run a robot, you must compile the code.  This will check the code for errors and report them to you. In order to do this you must follow these instructions :

 

 

  • From the Compiler menu select the Compile option


 

 

  • You may be asked to save your robot, so click yes

 

  • You may be presented with an option to create a new folder, choose yes

 

 

  • Save as you normally would in any other piece of software
  • A successfully compiled message should come up (if not, ask for help)

Now try out your new robot in a battle as shown previously

 

 

Once you have made changes to your robot through the addition of code you will need to compile the robot so that the changes take effect.

 

 

 

Event driven behaviors

 

Certain actions will only take place when an event has happened (such as a robot has been hit by a bullet or has hit a wall or another robot.  There are many more other events that could occur)

 

To set the actions for a particular even, you must add that event to your code.  An example is:

 

public void onScannedRobot(ScannedRobotEvent e)

{

  fire(1);

}

This code takes care of what happens once the radar has scanned another  robot.

 

The above code basically tells the robot that it should fire when it detects an enemy robot. The 1 in this case is to instruct the the robot to fire with a strength of 1. The alternative would be to fire with a strength of 2 or 3.

 

·         Try changing this value to 2 or 3 then send your robot into battle against a regular MyFirstRobot!

 

 

You can also add new events, for example

 

onHitByBullet(HitByBulletEvent e) - This method will be called when your robot is hit by a bullet

 

Any action can go here, for example:

 

turnLeft(90));

 

this will turn the robot left 90 degrees

 
Any events  can be used by creating a section like so

 

Public void onHitByBullet(HitByBulletEvent e

{

 

}

 

 

 

 

 

Here are some other events that can be used.


onHitRobot(HitRobotEvent e) - This method will be called when your robot collides with another robot.
onHitWall(HitWallEvent e) - This method will be called when your robot collides with a wall.
onScannedRobot(ScannedRobotEvent e) - This method will be called when your robot sees another robot.
onWin(WinEvent e) - This method will be called if your robot wins a battle

 

  • Try adding a new event with any of the previous commands.

 

 

 

 

 

 

 

 

 

 

More Features

The best way to learn how to construct robots with certain behaviours is to look at how other robots have been constructed.

 

  • Try to copy some of the tricks used by two of the existing robots.

 

  • Look at Crazy.java: first try putting it in a battle with another robot to get a feel for how it works, then open up its code in the editor.

 

  • The most important part to look at is the public void run() method. Notice how it repeatedly turns left and then right, while moving forward all the time. This makes it move in its crazy way, so making it hard to hit.

 

  • Now let’s look at MyFirstRobot again. Note how it spins its gun and finds the enemy more effectively than Crazy.

 

  • See if you can make a new robot which copies most of Crazy.java, but incorporate this gun spinning in the run() routine.

 


Uploading and Downloading Robots

 

 

Now that you know how to write sophisticated robots, you may want to upload them so that they can do battle with others’ robots. You may also want to download other robots to test them against yours.

 

Upload it

Go to this link:

 

http://www.robocoderepository.com/Categories.jsp

 

Click Upload New Bot, it is on the right-hand side, close to the top.

 

As it is your first time, you will have to create a new account. Click on the New Account button, which now appears where Upload New Bot had been before. Fill in your details and then you will get onto the uploading section.

 

Fill in your Bot Name and the Package, and leave Derived From blank. Enter a short Description too.

Under the categories at the bottom pick Level-Newbie

This is the area for bots written by beginners.

Now click Save & Upload.

Now navigate to where your source .java file is, and your .jar package file.

Download it

Your Bot will now appear on the main page (URI above) and you can download it, or any other Bot you choose.

 

 Try competing with one of the really good Bots, just to see how good they are. For example, pick the most popular Bot ;save it somewhere on the computer, and then go to Robocode’s Robot menu and pick Import downloaded robot.

 

Now start a battle with the newly downloaded robot against one of your own