Difference between revisions of "Robocode 1"

From CompSciWiki
Jump to: navigation, search
(Adding in Firing on Other Robots text and fixing numbering in Create your first Robot)
(Updating Firing on other Robots and adding in a code block there)
Line 99: Line 99:
 
Robot will fire. In the run method add in fire(1) underneath the ahead(100) method
 
Robot will fire. In the run method add in fire(1) underneath the ahead(100) method
 
call and underneath the back(100) method call.
 
call and underneath the back(100) method call.
 +
{{CodeBlock
 +
|Code=
 +
public void run() {
 +
  // Initialization of the robot should be put here
 +
 +
  // After trying out your robot, try uncommenting the import at the top,
 +
  // and the next line:
 +
 +
  // setColors(Color.red,Color.blue,Color.green); // body,gun,radar
 +
 +
  // Robot main loop
 +
  while(true) {
 +
      // Replace the next 4 lines with any behavior you would like
 +
      ahead(100);
 +
      fire(1);
 +
      turnGunRight(360);
 +
      back(100);
 +
      fire(1);
 +
      turnGunRight(360);
 +
  }
 +
}
 +
}}
 
<br><br>
 
<br><br>
Let's now test our firing on another robot. Select a new Battle. Add in your Robot
+
After compiling your Robot again lets test our firing on another robot. Select a new Battle. Add in your Robot from your package as you did earlier. Also add in, from the sample Package, SittingDuck. Click "Start Battle" to begin firing! Feel free to add in other places where you think you should fire on other Robots.
from your package as you did earlier. Also add in, from the sample Package,  
+
SittingDuck. Click "Start Battle" to begin firing! Feel free to add in other places
+
where you think you should fire on other Robots.
+
  
 
== Additional Methods ==
 
== Additional Methods ==
Line 109: Line 128:
  
 
== Battle your Robot ==
 
== Battle your Robot ==
Now it is time to show the TA the cool new things you have learned in this lab! Make a Robot that can defeat the Sample package Robot, MyFirstRobot. Demonstrate this to the TA, and obtain your bragging rights.
+
Now it is time to show the TA the cool new things you have learned in this lab! Test out your Robot you have made on other Robots. Feel free to add in more functionality to your Robot. For a more in depth look at this improved functionality, see the next lab [[Robocode_2|Robocode part 2]].
 
+
 
}}
 
}}

Revision as of 10:29, 4 April 2012

COMP 1010 Home > Back to Extra Labs

Introduction

In this lab you will learn and be able to play the exciting coding game called Robocode. Robocode is an event driven program that allows us to create, modify and test virtual warrior robots in a virtual battlefield. If you are not familiar with event driven programming you should consider completing the GUI Lab part 1 or at least read Step 3 from the lab. Completing this lab and Robocode part 2 will also give you a head start on understanding and using Application Programming Interfaces (APIs).

Installation

After Robocode is installed you can run it by either executing the supplied shortcut or navigating to your installation folder and running the robocode.bat file.

Upon first boot of the game you see a short battle demo.


RoboCodeIntroBattle.png


When the battle ends you will be presented with a scoreboard of the demo battle and the Main Menu. Close the scoreboard, we have no use for it. For now lets just focus on the main menu.

The Main Menu

At the top of the main menu you will find the toolbar which you will use to start virtual battles or design a robot.

The Battle menu will allow you to set up a virtual battle. Selecting New will prompt you to create a new virtual battle from scratch by presenting you with the New Battle Screen. Selecting Open will let you open a pre existing virtual battle setting where you can modify the settings or simply just run the battle.

The Robot menu provides robot development options. Selecting Editor within the Robot menu will open the Robot Editor. You will soon use the Robot Editor to design your first robot.

For now this is all you need to know to get started and move around using robocode. Don't worry about the other options just yet.

Sample battle

To better familiarize yourself with robocode and understand what the game is all about let us first look at setting up a sample battle using pre definied robots that came with the installation of robocode. Lets create a virtual battle with 3 robots and 5 rounds. To do so:

  1. Click on the Battle menu and select New. This will open the New Battle Screen.
  2. Double click on any 3 robots from the Available Robots pane. Upon adding robots you should be able to see them pop up on the Selected Robots pane on the right.
  3. Change the number of rounds value to 5.
    • Each battle consists of consecutive rounds. Since the robots are randomly dropped on the the battlefield we can have multiple rounds to make the game fair.
  4. Click on Start Battle and enjoy.

In robocode you do not have control over any robot during battle. It is up to your programming skills ahead of time to design a robot that will decide what to do in combat. So without further ado go ahead and read the next section which tells you how to create your first robot.


Create your first Robot

Now that you have the basics down, lets create your first Robot.

  1. From the Robot menu, select "Editor" (Or Press Ctrl+E). This opens up the Robot Editor where you will make, edit, and compile your Robot.
  2. From the "File" menu, select "New", then select Robot. (Or press Ctrl+N).
  3. Enter a name for your Robot (This name cannot contain any spaces, similar to java variables! Remember, Robocode is made in java). Click "Ok".
  4. Enter a short package name for your Robot. This should be somewhat different than your Robot's name.
  5. Voila! Your Robot has been created. As you can see the Robot code pops up and is ready to edit. Your Robot is what is known as an object in more advanced programming.
  6. Your Robot has some basic methods in its file. These include run(), onScannedRobot(ScannedRobotEvent e), onHitByBullet(HitByBulletEvent e), onHitWall(HitWallEvent e).


Making the Robot Move

Now that your code for your Robot has been created, lets add in some functionality. By default the robot has some movement functionality already in it. Let's take a look at the run method to examine this default movement.

 public void run() {
   // Initialization of the robot should be put here

   // After trying out your robot, try uncommenting the import at the top,
   // and the next line:

   // setColors(Color.red,Color.blue,Color.green); // body,gun,radar

   // Robot main loop
   while(true) {
      // Replace the next 4 lines with any behavior you would like
      ahead(100);
      turnGunRight(360);
      back(100);
      turnGunRight(360);
   }
} 

As you can see, there appears to be some movements called ahead(100) and back(100) in the main while loop. Lets test our these movements and see if our Robot actually does this.Compile your Robot by going under "Compiler", select "Compile" (Or press Ctrl+B). Once it successfully compiler click "Ok". Yay, your Robot is now compiled and ready to move around the map!

Go back to the main GUi and lets import your Robot. Click "Battle", and click "New" (Or press Ctrl+N). Locate your package under the Packages section. Click on it to see your Robot under the "Robots" section. Click on "Add ->" and add your Robots to the list of selected Robots. Now lets see if your Robot actually moves! Click on Start Battle. Unless you added any other Robots your Robot should just run around aimlessely for 10 rounds. But now we have verified that movement works! If you like, you can add in different values to the ahead, turnGunRight, back, and turnGunRight to experiment with it.

Firing on Other Robots

Now that we know our Robot can move around, let's make it fire on other Robots! By default, when your Robot was created it had a method called onScannedRobot that would return fire(by calling the method fire(1)) when a Robot scanned yours.

We're going to keep this functionality but also lets add in two more times when your Robot will fire. In the run method add in fire(1) underneath the ahead(100) method call and underneath the back(100) method call.

 public void run() {
   // Initialization of the robot should be put here

   // After trying out your robot, try uncommenting the import at the top,
   // and the next line:

   // setColors(Color.red,Color.blue,Color.green); // body,gun,radar

   // Robot main loop
   while(true) {
      // Replace the next 4 lines with any behavior you would like
      ahead(100);
      fire(1);
      turnGunRight(360);
      back(100);
      fire(1);
      turnGunRight(360);
   }
} 



After compiling your Robot again lets test our firing on another robot. Select a new Battle. Add in your Robot from your package as you did earlier. Also add in, from the sample Package, SittingDuck. Click "Start Battle" to begin firing! Feel free to add in other places where you think you should fire on other Robots.

Additional Methods

run(), onSeannedRobot() and onHitByBullet()

Battle your Robot

Now it is time to show the TA the cool new things you have learned in this lab! Test out your Robot you have made on other Robots. Feel free to add in more functionality to your Robot. For a more in depth look at this improved functionality, see the next lab Robocode part 2.