Robocode 2

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Back to Extra Labs

Introduction

In this lab we will explore the Robot class more deeply and examine all of the different methods that the robot can call. You will become more familiar with the API for the Robot class. You will also explore and analyze robots that others have created as well as learning how to test your best creation against the world.

This lab will help get you comfortable using an Application Programming Interface (API). An API will have information about the way a prebuilt class can function. We will examine the robot class more closely than the first Robocode lab. With a short introduction, you will be able to read the API and start to understand how it works. Finally you will read the API and implement a few of the methods that it contains.

Image by Graeme Peters

Installation

Download and run the latest version of robocode-x.x.x.x-setup.jar file from: http://sourceforge.net/projects/robocode/files/

Building starting robot

Open the robot editor and create a new robot. If you need assistance in creating a robot, see the first Robocode lab at: Robocode part 1

Implement new methods

You can find a list of the methods that a robot can do by going to the API for the robot class at: http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html

I will go over a few of them now to give you detailed instructions on how to use them. After you have read about them, look them up in the API to see if you understand how those methods work.

ahead() and back()

Immediately moves your robot ahead or back by a distance measured in pixels.

The Robot will not return until it is complete, i.e. when the remaining distance to move is 0, collides with a wall or collides with another robot.

Both positive and negative values can be given as input, where negative values means that the robot is set to move opposite direction.

Example:

 ahead(100); // Move the robot 100 pixels forward
   
back(-50);  // move the robot 50 pixels forward 

turnGunLeft() and turnGunRight()

Immediately turns the robot's gun to the left by given degrees.

This will not return until it is complete, i.e. when the angle remaining in the gun's turn is 0.

Both positive and negative values can be given as input, where negative values means that the robot's gun is set to turn opposite direction.

Example:

 turnGunLeft(270); // Turn the robot's gun 270 degrees to the left
   
turnGunRight(-90);// Afterwards, turn the robot's gun 90 degrees to the left 

fire()

Immediately fires a bullet. The bullet will travel in the direction the gun is pointing.

The specified bullet power is an amount of energy that will be taken from the robot's energy.

The bullet will do (4 * power) damage if it hits another robot. If power is greater than 1, it will do an additional 2 * (power - 1) damage.

You will get (3 * power) back if you hit the other robot.

onScannedRobot()

This method is called when your robot's radar scans another robot. While the other methods listed are only called when you code them in specifically, this method is called when a specific event happens. This method is event driven.

Example:

 public void onScannedRobot(ScannedRobotEvent e) 
{
   fire(3);
} 

onHitByBullet()

This method is called when your Robot gets hit by a bullet. This method is also event driven. You can see the actual event being passed to the method as a parameter.

Example:

 public void onHitByBullet(HitByBulletEvent e) 
{
   back(10);
} 

onHitWall()

This method is called when your robot collides with a wall. This is another example of an event driven method.

Example:

 void onHitWall(HitWallEvent e) 
{
   back(20);
} 

Add your own event based method from the robot API

Revisit the robot API at: http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html

The rows listed under method summary are a list of the methods and information about them. It is important to note that most of the methods are self explanatory when you examine their names. The methods that we will be looking at for part of the lab are the ones that contain an event as part of their parameter list. Some of these events can be triggered by a user pressing a key on the keyboard or by the robot experiencing some specific act.

For example the method BulletHitBulletEvent is called whenever a robot has its bullet hit a bullet from another robot. If this method is not included in your code than nothing will happen, but if you wanted to add functionality to do something in that case, you could implement the method easily. If you wanted to back up 50 pixels when your robots' bullet hit another bullet the code would look like this:

 onBulletHitBullet(BulletHitBulletEvent event) 
{
	back(50);
} 

Your task as part of this lab is to implement 2 new event driven methods. Both of these methods will then call other action methods like back(), ahead() or fire(). Try to make your robot perform something unexpected. This could be something as easy as changing the colour of the gun barrel to performing moves to dodge incoming bullets.

Once you have implemented these two new methods, show your T.A. and test them in a battle to ensure they are doing what you intended them to.

Challenge!

Winning a battle 100% of the time is difficult as there are extra factors including robot placement that are done randomly. Try to battle the sample.fire robot to test the functionality you have created. The fire robot will sit still and shoot at your robot. If you hit the fire robot it will move in an attempt to not be hit again. You can look at the source code for this robot for ideas on how to implement similar ideas.

As part of the challenge component of this lab, try to beat the other sample robots including the walls robot, crazy robot and ramfire robot.