Difference between revisions of "Robocode 1"

From CompSciWiki
Jump to: navigation, search
(Updating Firing on other Robots and adding in a code block there)
(Added comic credit)
 
(32 intermediate revisions by 5 users not shown)
Line 4: Line 4:
 
|Introduction=
 
|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_I|GUI Lab part 1]] or at least read [[GUI_I#Step_3:_Listen_carefully.21|Step 3]] from the lab. Completing this lab and [[Robocode_2|Robocode part 2]] will also give you a head start on understanding and using Application Programming Interfaces (APIs).
+
Want to see why Robocode is awesome? [http://www.youtube.com/watch?v=vVmf5cA8Rh8 Check this video out!]<br/>In this lab you will learn 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_I|GUI Lab Part 1]] or at least read [[GUI_I#Step_3:_Listen_carefully.21|Step 3]] from that lab. Completing this lab and [[Robocode_2|Robocode Part 2]] will also give you a head start in understanding and using Application Programming Interfaces (APIs).
 
+
  
 +
[[Image:Robocode_I.png|800px|Image by Graeme Peters]]
 
|Body=
 
|Body=
 
== Installation ==
 
== Installation ==
  
*Download and run the latest version of robocode-x.x.x.x-setup.jar file from: http://sourceforge.net/projects/robocode/files/
+
*Download and run the latest version of <i>robocode-x.x.x.x-setup.jar</i> file from: http://sourceforge.net/projects/robocode/files/
  
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.
+
After Robocode is installed you can run it by either executing the supplied shortcut or by navigating to your installation folder and running the <i>robocode.bat</i> file.
  
Upon first boot of the game you see a short battle demo.
+
Upon first boot of the game you will see a short battle demo.
  
  
Line 20: Line 20:
  
  
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.
+
When the demo ends you will be presented with a scoreboard of the demo battle as well as the <b>Main Menu</b>. Close the scoreboard as we have no use for it. Let us focus on the main menu for now.
 +
<br><br>
  
 
== 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. The main menu is also used to design Robots.
  
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.
+
[[Image:Robo1toolbar.png]]
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.
+
<hr>
  
== 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:
+
Selecting <i>Battle</i> from the toolbar allows you to setup a virtual battle. You can start from scratch by selecting <i>New</i>, this creates a new virtual battle. Selecting <i>Open</i> lets you open a pre-existing battle. The <i>Open</i> option also allows you to make modifications to the settings if so desired, or you can simply run the battle as is.
  
#Click on the Battle menu and select New. This will open the New Battle Screen.
 
#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.
 
#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.
 
#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.  
+
[[Image:Robo1Battle.png]]
 +
 
 +
 
 +
<hr>
 +
 
 +
 
 +
The <i>Robot</i> menu provides Robot development options. Selecting <i>Editor</i> within the Robot menu will open the <b>Robot Editor</b>. You will soon use the Robot Editor to design your first Robot.
 +
 
 +
 
 +
[[Image:Robo1Robot.png]]
 +
 
 +
 
 +
<hr>
 +
 
 +
 
 +
For now this is all you need to know to get started and move around using Robocode. Do not worry about the other options just yet.
 +
<br><br>
 +
 
 +
== 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 some of the predefined Robots that came with the installation of Robocode. Let us create a virtual battle with 3 Robots that goes for 5 rounds. To do so:<br>
 +
 
 +
#Click on the <i>Battle</i> menu and select <i>New</i> (Or Press Ctrl+N). This will open the <b>New Battle Screen</b>.<br>[[Image:Robo1MainMenu.png]]<br><br>
 +
#Double click on any 3 Robots from the <b>Available Robots</b> pane. Upon adding Robots you should be able to see them pop up on the <b>Selected Robots</b> pane on the right.<br>[[Image:Robo1Availablerobotspane.png]]    [[Image:Robo1selectedrobotspane.png]]<br><br>
 +
#Change the number of rounds value to 5.<br>[[Image:Robo1numRounds.png]]<br><br>
 +
#*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.<br><br>
 +
#Click on <b>Start Battle</b> and enjoy.
 +
 
 +
In Robocode you do not have control over any Robot during the battle. Your programming skills in designing your Robot will determine how well your Robot does 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 ==
 
== Create your first Robot ==
 +
Now that you have the basics down, let us create your first Robot.
  
Now that you have the basics down, lets create your first Robot.
+
#From the Robot menu, select <i>Editor</i> (Or Press Ctrl+E). This opens up the Robot Editor where you will make, edit, and compile your Robot. It should bring up a window with the following menu items available:<br>[[Image:Robo1EditorMainMenu.png]]<br><br>
#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.
+
#From the <i>File</i> menu, select <i>New</i>, then select <i>Robot</i>. (Or press Ctrl+N). <br>[[Image:Robo1EditorNewRobot.png]]<br><br>
#From the "File" menu, select "New", then select Robot. (Or press Ctrl+N).
+
#Enter a name for your Robot. This name cannot contain any spaces and is similar to Java variables in this respect. Click <i>Ok</i>.<br>[[Image:Robo1NewRobotName.png]]<br><br>
#Enter a name for your Robot (This name cannot contain any spaces, similar to java variables! Remember, Robocode is made in java). Click "Ok".
+
#Enter a short package name for your Robot. This should be somewhat different than your Robot's name. <br>[[Image:Robo1NewRobotPackage.png]]<br><br>
#Enter a short package name for your Robot. This should be somewhat different than your Robot's name.
+
#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 [http://en.wikipedia.org/wiki/Object_(computer_science) object] in more advanced programming.
+
#Your Robot has some basic methods in its file. These include run(), onScannedRobot(ScannedRobotEvent e), onHitByBullet(HitByBulletEvent e), onHitWall(HitWallEvent e).
+
  
 +
There you have it, your Robot has been created. As you can see the Robot code pops up and is ready to edit. Your Robot is essentially an [http://en.wikipedia.org/wiki/Object_(computer_science) <i>object</i>] as defined in object oriented programming. Robots have some basic methods in their source code. These include run(), onScannedRobot(ScannedRobotEvent e), onHitByBullet(HitByBulletEvent e), and onHitWall(HitWallEvent e).
 +
<br><br>
  
 
== Making the Robot Move ==
 
== 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
+
Now that your code for your Robot has been created, it is time to 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.
+
movement functionality already in it. Now we will take a look at the run method to examine this default movement.
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
 
public void run() {
 
public void run() {
   // Initialization of the robot should be put here
+
   // Initialization of the Robot should be put here
  
   // After trying out your robot, try uncommenting the import at the top,
+
   // After trying out your Robot, try uncommenting the import at the top,
 
   // and the next line:
 
   // and the next line:
  
Line 81: Line 100:
 
}
 
}
 
}}
 
}}
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!
+
As you can see there appears to be some movements called ahead(100) and back(100) in the main while loop. Let's test out these movements and see if our Robot actually does this.  
  
Go back to the main GUi and lets import your Robot. Click "Battle", and click "New" (Or press Ctrl+N). Locate your
+
#Let's increase ahead(100) to ahead(150).
package under the Packages section. Click on it to see your Robot under the "Robots" section. Click on "Add ->"
+
#Compile your Robot by going to <i>Compiler</i>, select <i>Compile</i> (Or press Ctrl+B).<br>[[Image:Robo1EditorCompile.png]]<br><br>
and add your Robots to the list of selected Robots. Now lets see if your Robot actually moves!
+
#Once it successfully compiles click <i>Ok</i>. Your Robot is now compiled and ready to move around the map.
Click on Start Battle. Unless you added any other Robots your Robot should just run around aimlessely for 10
+
#Go back to the main GUI and import your Robot.
rounds. But now we have verified that movement works! If you like, you can add in different values to the
+
#Click <i>Battle</i>.
ahead, turnGunRight, back, and turnGunRight to experiment with it.
+
#Click <i>New</i> (Or press Ctrl+N).
 +
#Locate your package under the <i>Packages</i> section.
 +
#Click your package to see your Robot under the Robots section.
 +
#Click <i>Add -></i> to add your Robot to the list of selected Robots. Now it is time to see if your Robot actually moves.
 +
#Click on <i>Start Battle</i>.
  
== Firing on Other Robots ==
+
Unless you added other Robots, your Robot should just run around aimlessly for 10 rounds, but we have now verified that movement works. If you like, you can start experimenting with different values to ahead(150), turnGunRight(360), back(100), and turnGunRight(360).
 +
<br><br>
  
Now that we know our Robot can move around, let's make it fire on other Robots!
+
== Firing on Other Robots ==
By default, when your Robot was created it had a method called onScannedRobot
+
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. This method will automatically fire on any robots that your robot detects.
that would return fire(by calling the method fire(1)) when a Robot scanned yours.
+
 
<br><br>
 
<br><br>
We're going to keep this functionality but also lets add in two more times when your
+
We are going to keep this functionality but also add two more.
Robot will fire. In the run method add in fire(1) underneath the ahead(100) method
+
#Add in fire(1) underneath ahead(100).
call and underneath the back(100) method call.
+
#Add in fire(1) underneath back(100).
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
 
public void run() {
 
public void run() {
   // Initialization of the robot should be put here
+
   // Initialization of the Robot should be put here
  
   // After trying out your robot, try uncommenting the import at the top,
+
   // After trying out your Robot, try uncommenting the import at the top,
 
   // and the next line:
 
   // and the next line:
  
Line 121: Line 144:
 
}
 
}
 
}}
 
}}
 +
After recompiling your Robot, let's test our new firing functionalities on another Robot.
 +
#Select a new Battle.
 +
#Add in your Robot from your package as you did earlier.
 +
#Add in, from the sample Package, SittingDuck.
 +
#Click <i>Start Battle</i> to begin firing!
 +
Feel free to add in other scenarios when you want your Robot to fire.
 
<br><br>
 
<br><br>
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 ==
 
== Additional Methods ==
run(), onSeannedRobot() and onHitByBullet()
+
Now that we have covered the run() method, let's add in some more functionality to three other methods: onScannedRobot(), onHitByBullet(), and onHitWall().
 
+
<br>
 +
==== onScannedRobot() ====
 +
By default, all you have in this method is:
 +
{{CodeBlock
 +
|Code=
 +
public void onScannedRobot(ScannedRobotEvent e) {
 +
  // Replace the next line with any behavior you would like
 +
  fire(1);
 +
}
 +
}}
 +
#Add in a turnGunLeft(360) just above your fire(1) method's call.
 +
#Increase the fire to 2 instead of 1.
 +
<br>
 +
==== onHitByBullet() ====
 +
By default, all you have in this method is:
 +
{{CodeBlock
 +
|Code=
 +
public void onHitByBullet(HitByBulletEvent e) {
 +
  // Replace the next line with any behavior you would like
 +
  back(10);
 +
}
 +
}}
 +
Let's change this up a bit.
 +
#Insert a turnRight(90) before the back(10).
 +
#Change back(10) to ahead(20) .
 +
<br>
 +
==== onHitWall() ====
 +
By default, all you have in this method is:
 +
{{CodeBlock
 +
|Code=
 +
public void onHitWall(HitWallEvent e) {
 +
  // Replace the next line with any behavior you would like
 +
  back(20);
 +
}
 +
}}
 +
#Add in a turnLeft(180) above the back(20).
 +
<br>
 
== Battle your Robot ==
 
== 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_2|Robocode part 2]].
+
Now it is time to show the TA the cool new things you have learned in this lab. Your Robot is now ready for battle. Feel free to add in more functionality to your Robot. For a more in depth look at improved functionality, see the next lab [[Robocode_2|Robocode Part 2]].
 
}}
 
}}

Latest revision as of 17:33, 5 April 2012

COMP 1010 Home > Back to Extra Labs

Introduction

Want to see why Robocode is awesome? Check this video out!
In this lab you will learn 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 that lab. Completing this lab and Robocode Part 2 will also give you a head start in understanding and using Application Programming Interfaces (APIs).

Image by Graeme Peters

Installation

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

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


RoboCodeIntroBattle.png


When the demo ends you will be presented with a scoreboard of the demo battle as well as the Main Menu. Close the scoreboard as we have no use for it. Let us focus on the main menu for now.

The Main Menu

At the top of the main menu you will find the toolbar which you will use to start virtual battles. The main menu is also used to design Robots.


Robo1toolbar.png




Selecting Battle from the toolbar allows you to setup a virtual battle. You can start from scratch by selecting New, this creates a new virtual battle. Selecting Open lets you open a pre-existing battle. The Open option also allows you to make modifications to the settings if so desired, or you can simply run the battle as is.


Robo1Battle.png




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.


Robo1Robot.png




For now this is all you need to know to get started and move around using Robocode. Do not 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 some of the predefined Robots that came with the installation of Robocode. Let us create a virtual battle with 3 Robots that goes for 5 rounds. To do so:

  1. Click on the Battle menu and select New (Or Press Ctrl+N). This will open the New Battle Screen.
    Robo1MainMenu.png

  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.
    Robo1Availablerobotspane.png Robo1selectedrobotspane.png

  3. Change the number of rounds value to 5.
    Robo1numRounds.png

    • 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 the battle. Your programming skills in designing your Robot will determine how well your Robot does 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, let us 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. It should bring up a window with the following menu items available:
    Robo1EditorMainMenu.png

  2. From the File menu, select New, then select Robot. (Or press Ctrl+N).
    Robo1EditorNewRobot.png

  3. Enter a name for your Robot. This name cannot contain any spaces and is similar to Java variables in this respect. Click Ok.
    Robo1NewRobotName.png

  4. Enter a short package name for your Robot. This should be somewhat different than your Robot's name.
    Robo1NewRobotPackage.png

There you have it, your Robot has been created. As you can see the Robot code pops up and is ready to edit. Your Robot is essentially an object as defined in object oriented programming. Robots have some basic methods in their source code. These include run(), onScannedRobot(ScannedRobotEvent e), onHitByBullet(HitByBulletEvent e), and onHitWall(HitWallEvent e).

Making the Robot Move

Now that your code for your Robot has been created, it is time to add in some functionality. By default the Robot has some movement functionality already in it. Now we will 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. Let's test out these movements and see if our Robot actually does this.

  1. Let's increase ahead(100) to ahead(150).
  2. Compile your Robot by going to Compiler, select Compile (Or press Ctrl+B).
    Robo1EditorCompile.png

  3. Once it successfully compiles click Ok. Your Robot is now compiled and ready to move around the map.
  4. Go back to the main GUI and import your Robot.
  5. Click Battle.
  6. Click New (Or press Ctrl+N).
  7. Locate your package under the Packages section.
  8. Click your package to see your Robot under the Robots section.
  9. Click Add -> to add your Robot to the list of selected Robots. Now it is time to see if your Robot actually moves.
  10. Click on Start Battle.

Unless you added other Robots, your Robot should just run around aimlessly for 10 rounds, but we have now verified that movement works. If you like, you can start experimenting with different values to ahead(150), turnGunRight(360), back(100), and turnGunRight(360).

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. This method will automatically fire on any robots that your robot detects.

We are going to keep this functionality but also add two more.

  1. Add in fire(1) underneath ahead(100).
  2. Add in fire(1) underneath back(100).
 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 recompiling your Robot, let's test our new firing functionalities on another Robot.

  1. Select a new Battle.
  2. Add in your Robot from your package as you did earlier.
  3. Add in, from the sample Package, SittingDuck.
  4. Click Start Battle to begin firing!

Feel free to add in other scenarios when you want your Robot to fire.

Additional Methods

Now that we have covered the run() method, let's add in some more functionality to three other methods: onScannedRobot(), onHitByBullet(), and onHitWall().

onScannedRobot()

By default, all you have in this method is:

 public void onScannedRobot(ScannedRobotEvent e) {
   // Replace the next line with any behavior you would like
   fire(1);
} 
  1. Add in a turnGunLeft(360) just above your fire(1) method's call.
  2. Increase the fire to 2 instead of 1.


onHitByBullet()

By default, all you have in this method is:

 public void onHitByBullet(HitByBulletEvent e) {
   // Replace the next line with any behavior you would like
   back(10);
} 

Let's change this up a bit.

  1. Insert a turnRight(90) before the back(10).
  2. Change back(10) to ahead(20) .


onHitWall()

By default, all you have in this method is:

 public void onHitWall(HitWallEvent e) {
   // Replace the next line with any behavior you would like
   back(20);
} 
  1. Add in a turnLeft(180) above the back(20).


Battle your Robot

Now it is time to show the TA the cool new things you have learned in this lab. Your Robot is now ready for battle. Feel free to add in more functionality to your Robot. For a more in depth look at improved functionality, see the next lab Robocode Part 2.