Sie sind auf Seite 1von 38

The SchoolBots Manual

Dr Liam Noonan

ICT Dept,
Tipperary Institute.

August 2008
Contents

1 Introduction 1
1.1 What makes a good robot? . . . . . . . . . . . . . . . . . . . . . 2
1.2 Robot Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Coordinate Geometry . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3.1 Heading . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.2 Bearing . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.3 Frames Vs Ticks . . . . . . . . . . . . . . . . . . . . . . 4
1.3.4 Movement and acceleration . . . . . . . . . . . . . . . . 5
1.4 Robot Senses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4.1 Sense of Touch . . . . . . . . . . . . . . . . . . . . . . . 5
1.4.2 Sense of Sight - Scan event . . . . . . . . . . . . . . . . . 5
1.4.3 Miscellaneous Senses . . . . . . . . . . . . . . . . . . . . 6
1.5 Firing commands . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5.1 Firing Physics . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5.2 Disabled . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5.3 How fast does the radar and gun turn? . . . . . . . . . . 7
1.5.4 Can we detect when the enemy has fired? . . . . . . . . . 7
1.5.5 When to fire? . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5.6 What to fire? . . . . . . . . . . . . . . . . . . . . . . . . 7
1.6 Scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.7 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2 My First Robot 10
2.1 Creating a basic robot using the Robot Editor . . . . . . . . . . 10
2.2 Running a Battle . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2.1 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3 Code Analysis of the basic robot . . . . . . . . . . . . . . . . . . 13
2.3.1 import and run method . . . . . . . . . . . . . . . . . . 14
2.3.2 onScannedRobot method . . . . . . . . . . . . . . . . . . 15
2.3.3 onHitByBullet . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4 Improving the Robot’s movement . . . . . . . . . . . . . . . . . 17
2.4.1 onHitWall method . . . . . . . . . . . . . . . . . . . . . 17

3 Intermediate Concepts 20
3.1 Colours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2 Turning the radar independently of the gun . . . . . . . . . . . 20
3.2.1 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.3 Distance from enemy . . . . . . . . . . . . . . . . . . . . . . . . 24

i
CONTENTS ii

4 A Review of the Sample Robots 25


4.1 WallKiller Clockwise and Anti-Clockwise . . . . . . . . . . . . . 25

Appendix B - Robocode API -i


4.2 Solution to Chapter 3 Exercise . . . . . . . . . . . . . . . . . . . -iii
List of Figures

1.1 Screen shot of robocode . . . . . . . . . . . . . . . . . . . . . . 1


1.2 Robot Anatomy . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Coordinate Geometry for Robocode . . . . . . . . . . . . . . . . 3
1.4 Bearing Coordinate System . . . . . . . . . . . . . . . . . . . . 4
1.5 Score Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.1 Robocode Robot Editor . . . . . . . . . . . . . . . . . . . . . . 10


2.2 Screen shot of a robocode battle . . . . . . . . . . . . . . . . . . 12
2.3 Enemy robot at 45 degrees bearing to your robot . . . . . . . . 17
2.4 Robot hits the wall, its bearing is -135 and its heading is 225,
it turns right 90 degrees, its heading changes to 315 degrees and
moves ahead 100 pixels . . . . . . . . . . . . . . . . . . . . . . . 18

3.1 Turning the gun to face the enemy, determine its bearing . . . . 22

iii
Listings

2.1 MyFirstRobot.java code listing . . . . . . . . . . . . . . . . . . 13


2.2 import and run listing . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 onScannedRobot . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4 onScannedRobot . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.5 onHitWall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.1 Setting the colours for the robot . . . . . . . . . . . . . . . . . . 20
3.2 Enabling independent movement of radar from gun and turning
of radar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3 Determining how much the gun needs to turn so as to shoot a
the detected tank . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.4 Using distance to determine what strength shot to fire . . . . . 24
3.5 Closing down the distance between your tank and the enemy . . 24
4.1 WallKiller Clockwise code . . . . . . . . . . . . . . . . . . . . . 25
4.2 WallKiller AntiClockwise code . . . . . . . . . . . . . . . . . . . 28

iv
Chapter 1

Introduction

Welcome to SchoolBots. SchoolBots is a computer game programming com-


petition aimed at transition year students and above at secondary schools in
Ireland. The idea behind the game is to build a software robot and destroy
the rest of your opponents. The game environment used is called robocode and
was developed originally by IBM.

Figure 1.1: Screen shot of robocode

Tipperary Institute hosted the second SchoolBots competition on Thursday


the 13th March 2009. Cashel Community School emerged victorious.

1
CHAPTER 1. INTRODUCTION 2

1.1 What makes a good robot?


A good robot will have (at least) the following abilities:

• Detection of enemies and acquisition of targets in a short period of time

• Rapid reaction to being hit and appropriate avoidance and attack strate-
gies

• Agile mobility around the battle arena

1.2 Robot Basics


A robot consists of three parts.

1. Body - Carries the gun with the radar on top. The body is used for
moving the robot ahead and back as well as turning left and right.

2. Gun - Mounted on the body and is used for firing energy bullets, The
gun can turn left or right.

3. Radar - Mounted on the gun and is used to scan for other robots when
moved. The radar can turn left or right. The radar generates on-
ScannedRobot events when robots are detected.

Figure 1.2: Robot Anatomy


CHAPTER 1. INTRODUCTION 3

Note that the robot has a rotating gun, and on top of the gun is a rotating
radar. The robot vehicle, the gun, and the radar can all rotate independently:
at any moment in time, the robot’s vehicle, the gun, and radar can be turned
in different directions. By default, these items are aligned, facing the direction
of the vehicle movement. It is worth noting that the tank moves pretty slow,
the gun turret moves faster, and the radar moves faster still.

1.3 Coordinate Geometry

Figure 1.3: Coordinate Geometry for Robocode

The Coordinate system in Robocode are (x,y):

• bottom left is (0,0)

• top left is (0, battlefield height)

• top right is (battlefield width, battlefield height)

• bottom right is (battlefield width, 0)

The battlefield width and battlefield height is set to 800 for SchoolBots. This
means that the battlefield is 800 pixels wide and 800 pixels tall. Movement
around the battlefield is expressed in pixels. If you instruct your robot to move
ahead(100), the robot will move ahead 100 pixels.
CHAPTER 1. INTRODUCTION 4

1.3.1 Heading
The direction that the robot travels in is known as the heading and is expressed
in degrees. As depicted in figure ??:

• North - 0 degrees

• East - 90 degrees

• South - 180 degrees

• West - 270 degrees

1.3.2 Bearing
An angle relative to the bearing of the enemy. It is most often used in Targeting
and will represent your offset from the enemy.

Figure 1.4: Bearing Coordinate System

By using the bearing, you can easily turn toward something else like so:
turnRight(event.getBearing());
Which is a pretty common strategy. Note that because a bearing is a value
from -180 to 180, calling turnRight() will actually make you turn left if the
bearing is negative – this is by design and it is what you want to have happen.

1.3.3 Frames Vs Ticks


A tick (also known as a turn) refers to a discrete unit of time in Robocode
physics. During one turn, you may perform one action as a Robot. A frame
is a unit of drawing to the Robocode client interface. If you are processing
turns slowly, you will get one frame per turn. However, if you up the turns
CHAPTER 1. INTRODUCTION 5

per second beyond your computer’s ability to render the frames, you will miss
some frames of animation. This won’t affect the robots’ behavior.

1.3.4 Movement and acceleration


Your robot can move at a maximum speed of 8.0 units/tick. You can modify
(down) your maximum velocity by using setMaxVelocity(...). Note that your
bot will always accelerate to reach it’s maximum velocity.
Your robot will accelerate at 1 unit/tick, and you decelerate at 2 units/tick.
For example, if you are moving at an speed of 8.0 and reverse your direction
your velocities will be [6.0, 4.0, 2.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0].

1.4 Robot Senses


Your robot has only has a few basic senses.

1.4.1 Sense of Touch


Your robot knows when it’s:
1. hit a wall (onHitWall)

2. been hit by a bullet (onHitBullet)

3. or hit another robot (onHiRobot)


These senses use methods so as to notify your robot that something has oc-
curred and pass on the information to the robot as to what exactly happened.

1.4.2 Sense of Sight - Scan event


Your robot knows when it’s seen another robot, but only if it scans it (on-
ScannedRobot).
Scan events are arguably the most important of all events. Scan events
give you information about the other robots on the battlefield. (Some robots
put 90+% of their code in the onScannedRobot method.) The only way scan
events can be generated (practically speaking) is if you move your radar. (If
an enemy robot wanders in front of your radar it will generate a scan event,
but you should really take a more proactive stance.)
The radar scanner is the fastest moving part of your robot, so don’t be
stingy about moving it. Some robots keep the radar aligned with the gun
which means that when the radar detects the enemy the gun is pointing at the
enemy. But the gun does turn slower than the radar on its own so this can be
time consuming. Other robots turn the radar independently of the gun which
is much faster but when the enemy is detected the gun has to be turned around
to face the enemy.
If you want to, you can make the robots’ scan arcs visible by selecting
Options Menu - Preferences - View Options Tab and click on the ”Visible Scan
Arcs“ checkbox. This is useful when debugging.
CHAPTER 1. INTRODUCTION 6

1.4.3 Miscellaneous Senses


Your robot also knows when he’s died (onDeath), when another robot has died
(onRobotDeath), or when he’s won the round (onWin – this is where you write
the code for your victory dance).
Your robot also is aware of his bullets and knows when a bullet has hit an
opponent (onBulletHit), when a bullet hits a wall (onBulletMissed), when a
bullet hits another bullet (onBulletHitBullet).

1.5 Firing commands


Your robot fires energy bullets to inflict damage on the enemy. Once you have
mastered how to move the robot and its associated weaponry, it’s a good time
to consider the tasks of firing and controlling damage. Each robot starts out
with a default ”energy level”, and is considered destroyed when its energy level
falls to zero. When firing, the robot can use up to three units of energy. The
more energy supplied to the bullet, the more damage it will inflict on the target
robot. fire(double power) is used to fire a bullet with the specified energy (fire
power)

1.5.1 Firing Physics


The power of a bullet can be a value between 1 (minimum) and 3 (maximum).
A bullet will travel at a speed of between 11 and 19.7 depending on the power.
The more powerful the bullet, the slower. The formula to calculate it is

V elocity = 20 − (3 ∗ power) (1.1)


A bullet has no range and is not affected by the speed of the robot.

Damage
You lose energy every time you hit a wall, you are hit by an enemy bullet, ram
an enemy, or you fire your gun. The amount of energy you lose by being hit is

4 ∗ bulletpower + 2 ∗ max(bulletpower − 1, 0) (1.2)


So the maximum amount is 16.0. When you fire, you spend a quantity of
energy equal to the power of the bullet fired. When one of your bullets hits an
enemy, you collect back

3 ∗ bulletpowerenergy (1.3)
When you hit an enemy bot, each bot takes 0.6 damage.
After firing, a robot’s gun heats up to a value of:

1 + (bulletP ower/5). (1.4)

As the default gun cooling rate of 0.1, this means a bot has to wait ceiling((1
+ (bulletPower / 5)) * 10) before it can fire again.
CHAPTER 1. INTRODUCTION 7

1.5.2 Disabled
When your robot’s energy drops to zero, your bot gets disabled. It will not be
able to move nor fire. If you are lucky enough and one of your bullets in the air
hits an enemy, you will get some energy back and recover from disabled status.

1.5.3 How fast does the radar and gun turn?


The gun turns at 20 degrees per tick and the radar turns 45 degrees per tick.
Radar therefore turns at least twice as fast as the gun.

1.5.4 Can we detect when the enemy has fired?


There is no direct way to detect when an enemy fired, but you can deduce it by
monitoring the enemy energy drop. A drop between 0.1 and 3 usually means
that it fired a bullet (there can be other reasons, such as a low energy bullet hit
or a wall hit). Wall hits are (more or less) detectable as well. A deceleration ¿
2 means the bot hit a wall (or another bot). A deceleration ¡= 2 may be simple
a bot hitting the brakes, or hitting a wall at velocity = 2, but since hitting a
wall at that speed won’t cause any damage, you can ignore that. There is no
method for detecting the position of enemy bullets.

1.5.5 When to fire?


There are several key issues to consider when deciding whether or not your bot
should fire, including:
• Is my gun aimed? If your gun hasn’t finished turning before you fire,
then your targeting algorithm is being crippled.
• How much energy do I have left? Firing yourself disabled is usually not
worth it.
• Am I able to hit the enemies accurately enough to make it worth firing
at them?

1.5.6 What to fire?


There are numerous power selection schemes, but there are some basic ideas
that many (but nowhere near all) bots use:
• In 1-vs-1, many bots use a fire power around 1.9 or 2.0.
• In melee, many bots simply use maximum fire power, 3.0.
• Many bots reduce fire power when the opponent is very far away or
increase it when the enemy is very close.
• Many bots reduce fire power when they are low on energy.
• It is a waste of energy to fire with a power greater than what’s needed to
kill the enemy (especially in 1-vs-1).
CHAPTER 1. INTRODUCTION 8

1.6 Scoring
When a battle is complete a score board for the battle is displayed

Figure 1.5: Score Board

• Total Score - This is everything else added up, and determines each
robot’s rank in this battle.

• Survival Score - Each robot that’s still alive scores 50 points every time
another robot dies

• Last Survivor Bonus - The last robot alive scores 10 additional points for
each robot that died before it.

• Bullet Damage - Robots score 1 point for each point of damage they do
to enemies.

• Bullet Damage Bonus - When a robot kills an enemy, it scores an addi-


tional 20% of all the damage it did to that enemy.

• Ram Damage - Robots score 2 points for each point of damage they cause
by ramming enemies.

• Ram Damage Bonus - When a robot kills an enemy by ramming, it scores


an additional 30% of all the damage it did to that enemy.

• 1sts, 2nds, 3rds - These do not actually contribute to score, but are there
to show how long the robot survived, i.e. the number of rounds the robot
was placed 1st, 2nd, and 3rd.

Your robot will not necessarily win, just by being the last robot left on the
battlefield (i.e. last survivor). A robot that does not fire much, but ”just”
saves its energy is getting a lesser score than a robot that hits other robots
with a lot of bullets. So, do not save your bullets forever just to survive. Make
sure you hit with as many of your bullets as possible. The better your robot is
at hitting other robots, the better your score will become.
CHAPTER 1. INTRODUCTION 9

1.7 Exercise
1. Describe the three basic parts of the robot

2. Describe the coordinate geometry system that the robot utilises

3. Describe the bearing geometry that the robot utilises

4. Form teams of three in your class.

5. Decide on a name for your robot.

6. Ask your teacher for the package name for your robot.

7. Decide on the colours for your robot e.g. red, yellow, blue, white, pink,
black
Chapter 2

My First Robot

In this chapter we will build a basic robot using the robocode editor and ex-
amine its simple behaviour
The following directions assume you have Robocode installed and running.
If this is not the case, you need to fix that before reading on.

2.1 Creating a basic robot using the Robot Ed-


itor
Robocode code includes a robot editor which allows you to create a basic robot
using a wizard. This is accomplished as follows:

Figure 2.1: Robocode Robot Editor

10
CHAPTER 2. MY FIRST ROBOT 11

1. With the Robocode program open, click on the ’Robot’ menu and the
’Editor’ item.

2. It should give you a little dialog saying it’s found a JDK to work with.
Click ’OK’ to tell it to use the one it found. (If it didn’t find anything,
you need to install the JDK. You can get it from java.sun.com, or possibly
my server.)

3. With the Robot Editor open, click on the ’File’ menu, the ’New’ submenu,
and the ’Robot’ item.

4. Type in a name for your bot. It does not have to be the name for the bot
you will use for the showdown, just [YourName]Bot will work fine. (i.e.
JimBot, JulioBot, RufusBot, BuffyBot, etc.)

5. Next you will be prompted for a package name. Enter your initials. You
can make as many bots as you like, but they should all belong to the
same package (i.e. use your initials here (and the same set of initials)
every single time).

6. It may give you a message that the directory does not exist. Click ’OK’
to tell it to create the directory for you.

7. You should now be staring at some boilerplate code. Give it a look see
and check out what’s going on. If you want an explanation of some of
the methods, consult the Robocode API.

8. Compile the code by clicking on the ’Compiler’ menu and the ’Compile’
option. It may offer to save the file for you, tell it ’OK’. Unless you’ve
made any changes, it should compile successfully. (If it doesn’t compile,
you get to figure out what you broke and fix it.)

2.2 Running a Battle


Now that you have created a basic robot you can run a battle to see how good
(or bad) your robot is.

1. Minimize (or close) the Robot Editor and return to the original Robocode
program.

2. Click the ’Battle’ menu and the ’New’ item.

3. If the bot you just made doesn’t show up in the list of bots in the dialog,
hit ’F5’ and it will refresh the list. (If your bot does show up, hitting ’F5’
with neither hurt nor help the situation.)

4. When your bot shows up, double-click on it to add it to the battle, along
with several other of the ’sample’ bots (include some from the tippinst
folder)

5. Click on start battle and assess how well your robot performs
CHAPTER 2. MY FIRST ROBOT 12

Figure 2.2: Screen shot of a robocode battle

2.2.1 Exercise
1. Identify 3 good aspects of your robot.

2. Identify 3 negative aspects of your robot.

3. What changes would you make to your robot so as to improve its survival
chances?
CHAPTER 2. MY FIRST ROBOT 13

2.3 Code Analysis of the basic robot


The basic robot that we created derives its instructions from a file called My-
FirstRobot.java

¨ Listing 2.1: MyFirstRobot.java code listing ¥


1 import robocode . ∗ ;
2 // import j a v a . awt . Color ;
3
4 /∗ ∗
5 ∗ MyFirstRobot − a r o b o t by ( your name h e r e )
6 ∗/
7 public c l a s s MyFirstRobot extends Robot
8 {
9 /∗ ∗
10 ∗ run : MyFirstRobot ’ s d e f a u l t b e h a v i o r
11 ∗/
12 public void run ( ) {
13 // A f t e r t r y i n g o u t your r o b o t , t r y
uncommenting t h e import a t t h e top ,
14 // and t h e n e x t l i n e :
15 // s e t C o l o r s ( Color . red , Color . b l u e , Color .
green ) ;
16 while ( true ) {
17 // Repl a c e t h e n e x t 4 l i n e s w i t h
any b e h a v i o r you would l i k e
18 ahead ( 1 0 0 ) ;
19 turnGunRight ( 3 6 0 ) ;
20 back ( 1 0 0 ) ;
21 turnGunRight ( 3 6 0 ) ;
22 }
23 }
24
25 /∗ ∗
26 ∗ onScannedRobot : What t o do when you s e e
another robot
27 ∗/
28 public void onScannedRobot ( ScannedRobotEvent e )
{
29 f i r e (1) ;
30 }
31
32 /∗ ∗
33 ∗ o n H i t B y B u l l e t : What t o do when you ’ r e h i t by
a bullet
34 ∗/
35 public void o n H it B yB u l l e t ( HitByBulletEvent e ) {
36 t u r n L e f t ( 90 − e . g e t B e a r i n g ( ) ) ;
CHAPTER 2. MY FIRST ROBOT 14

37 }
38
39 }
§ ¦
There are several sections to MyFirstRobot and we will now examine each
one.

2.3.1 import and run method

¨ Listing 2.2: import and run listing ¥


1 import robocode . ∗ ;
2 // import j a v a . awt . Color ;
3
4 /∗ ∗
5 ∗ MyFirstRobot − a r o b o t by ( your name h e r e )
6 ∗/
7 public c l a s s MyFirstRobot extends Robot
8 {
9 /∗ ∗
10 ∗ run : MyFirstRobot ’ s d e f a u l t b e h a v i o r
11 ∗/
12 public void run ( ) {
13 // A f t e r t r y i n g o u t your r o b o t , t r y
uncommenting t h e import a t t h e top ,
14 // and t h e n e x t l i n e :
15 // s e t C o l o r s ( Color . red , Color . b l u e , Color .
green ) ;
16 while ( true ) {
17 // Repl a c e t h e n e x t 4 l i n e s w i t h
any b e h a v i o r you would l i k e
18 ahead ( 1 0 0 ) ;
19 turnGunRight ( 3 6 0 ) ;
20 back ( 1 0 0 ) ;
21 turnGunRight ( 3 6 0 ) ;
22 }
23 }
§ ¦

• Line 1 instructs robocode to import the robocode library so we can make


use of its functions and methods.

• Line 2 is commented out the use of ‘//’ means ignore this line, if we
wanted to change the colour of the tank we would have to remove the
‘//’.

• Line 7 instructs robocode to create a basic robot which is the requirement


for this competition.

• Line 12 is the start of the run method or main body of the robot.
CHAPTER 2. MY FIRST ROBOT 15

• Line 16 instructs the robot to keep repeating the following steps forever.

• Lines 18 - 21 describes the actions that the robot will perform:

– line 18 - ahead(100); - move ahead 100 pixels


– line 19 - turnGunRight(360); - turn the gun right by 360 degrees
– line 20 - back(100); - move back 100 pixels
– line 21 - turnGunRight(360); - turn the gun right by 360 degrees

The robot will continue doing this over and over and over, until it dies, due to
the while(true) statement.

2.3.2 onScannedRobot method


When the radar detects another robot it calls the onScannedRobot method.
This event can relay information about the enemy robot, such as its name,
how much life it has, where it is, where it’s heading, how fast it’s going, etc.
However, since this is a simple robot, it is programmed to fire.

¨ Listing 2.3: onScannedRobot ¥


1 /∗ ∗
2 ∗ onScannedRobot : What t o do when you s e e
another robot
3 ∗/
4 public void onScannedRobot ( ScannedRobotEvent e )
{
5 f i r e (1) ;
6 }
§ ¦

• Lines 1 - 3 are comments that help the programmer understand what


operation the code performs

• Line 4 provides the name of the method which is onScannedRobot. All in-
formation about the enemy robot will be returned through the ScannedRobotEvent
using the variable e.

• Line 5 instructs the robot to fire a bullet of strength 1. This will cause
damage of 4.

Because this robot’s radar and gun are aligned it can fire a bullet as it
knows that once the radar detects the enemy it’s gun is pointing in the same
direction. Turning the gun to detect the enemy is not the most efficient way
to detect an enemy tank. The radar can turn twice as fast as the gun, but the
robot will then have to turn its gun to match the radar’s bearing of the enemy.
CHAPTER 2. MY FIRST ROBOT 16

2.3.3 onHitByBullet
Your robot needs to know what its strategy should be when it is hit by a bullet.
Does it stand its ground and fight? or does it run away? The onHitByBullet
method handles this event. The method is able to provide you with information
such as the direction the bullet came from i.e. its bearing.

¨ Listing 2.4: onScannedRobot ¥


1 /∗ ∗
2 ∗ o n H i t B y B u l l e t : What t o do when you ’ r e h i t by
a bullet
3 ∗/
4 public void o n H it B yB u l l e t ( HitByBulletEvent e ) {
5 turnLeft (90 − e . getBearing ( ) ) ;
6 }
§ ¦

• Lines 1 -3 are comments provided for the programmers benefit.

• Line 4 provides the name of the method which is onHitByBullet. All infor-
mation about the bullet will be returned through the HitByBulletEvent
using the variable e.

• Line 5 the robot will TurnLeft (90 - the direction the bullet came from)
e.g. if the bearing was 45 degrees body of the robot will turn left (90
-45) i.e 45 degrees. Note the gun will not turn just the body. (see figure
below)

• Line 6 the right curly brace ‘}’ indicates that this the end of the onHit-
ByBulletEvent

When the robot is hit by a bullet it turns 90 degrees left from the direction
that the bullet came from i.e the bearing. The main run method will then
take over and the tank will continue to move forward and back 100 pixels while
turning the gun 360 degrees.

Exercise
1. Add code to your onHitByBullet method so the robot moves forward 200
pixels after it has turned left.

2. Test your robot against the sample robot TrackFire, has this change
improved the robot?

3. Add code to your robot onHitByBullet method so that it returns fire


with a bullet of strength 1. Hint you will need to use the turnGunLeft or
turnGunRight command and utilise e.getBearing()

This simple robot has not strategy for dealing with hitting a wall. Its scan
and fire strategy is very basic and its movement is very predicatable. In the
next section we will examine how we can add a onHitWall method to the robot.
CHAPTER 2. MY FIRST ROBOT 17

Figure 2.3: Enemy robot at 45 degrees bearing to your robot

2.4 Improving the Robot’s movement


Our simple robot posseses a very simple movement strategy, in this section we
will improve its movement.

2.4.1 onHitWall method


The robot’s strategy for dealing with a wall collision is to turn 90 degrees to
the left or right depending on its bearing and move ahead 100 pixels.

¨ Listing 2.5: onHitWall ¥


1 public void onHitWall ( HitWallEvent e ) {
2 double l o c ;
3 l o c=e . g e t B e a r i n g ( ) ;
4 i f ( l o c >0){
5 turnLeft (90) ;
6 } // c l o s e i f
7 else {
8 turnRight (90) ;
9 } // c l o s e e l s e
10 ahead ( 1 0 0 ) ;
11 } // c l o s e s onHitWall
§ ¦

• Line 1 provides the name of the method which is onHitWall. All infor-
mation about the HitWallEvent will be returned through the variable
e.
• Line 2 declares a variable called loc of type double which will store the
location
CHAPTER 2. MY FIRST ROBOT 18

Figure 2.4: Robot hits the wall, its bearing is -135 and its heading is 225, it
turns right 90 degrees, its heading changes to 315 degrees and moves ahead 100
pixels

• Line 3 will store the bearing of the wall with reference to the robot. In
figure ?? the wall is at a bearing of -135 degrees. loc will store the value
-135.

• Line 4 If loc is > 0 execute the statement inside the following curly
brackets { }. Line 5 will be executed if loc > 0 and instructs the robot
turnLeft 90 degrees.

• Line 7 the else statement will be executed if loc ¡0. Line 8 instructs the
robot to turn right 90 degrees. This is illustrated in figure ??.

• Line 10 the robot will move ahead 100 pixels.

• Line 11 the right curly brace ‘}’ indicates that this the end of the onHit-
ByBulletEvent

exercise
1. Add the onHitWall code to your robot and compile.
CHAPTER 2. MY FIRST ROBOT 19

2. Test your improved robot against the sample robot sitting duck, has its
movement improved?

3. Test your robot against the sample robot trackfire, how well does it per-
form?

4. What improvements would you make to your robots movement?

5. Change the while(true) section of the run method of your robot so that
it performs the following.

• Moves ahead 200 pixels


• Turns the Gun Right 360 degrees
• Turns the robot right 45 degrees

6. Does the new movement improve or hinder the robot?


Chapter 3

Intermediate Concepts

In this chapter we will examine some intermediate concepts regarding your


robot’s movement and firing strategy. The more complex a robot becomes, the
longer it takes to execute all the steps and instructions. A balance has to be
struck between a complex yet elegant robot.

3.1 Colours
The default robot is coloured blue, this can be changed by importing the
java.awt.Color library as depicted in code listing ??. The command setCol-
ors(Color robotColor, Color gunColor, Color radarColor) allows you to cus-
tomise the colour for the robot’s body, gun and radar respectively.
Colours available are: black, blue, cyan, darrkGray, gray, green, lightGray,
orange, pink, red, white and yellow.

¨ Listing 3.1: Setting the colours for the robot ¥


1 import robocode . ∗ ;
2 import j a v a . awt . Color ;
3
4 public c l a s s MyFirstRobot extends Robot
5 {
6 public void run ( ) {
7 s e t C o l o r s ( Col or . red , Co l or . blue , Col or .
green ) ;
§ ¦

• Line 2 import the java.awt.Color library

• Line 7 set the colours for the body, gun and radar.

3.2 Turning the radar independently of the gun


The radar turns twice as fast as the gun, turning the radar independently of
the gun will result in faster detection. The position of the enemy will have to
be determined and the gun turned towards that position.

20
CHAPTER 3. INTERMEDIATE CONCEPTS 21

Listing 3.2: Enabling independent movement of radar from gun and turning of
¨radar ¥
1 public void run ( ) {
2 s e t C o l o r s ( Col or . red , Co l or . blue , Col or .
green ) ;
3 setAdjustRadarForRobotTurn ( true ) ;
4 while ( true ) {
5 ahead ( 1 0 0 ) ;
6 turnRadarRight ( 3 6 0 ) ;
7 back ( 1 0 0 ) ;
8 turnRadarRight ( 3 6 0 ) ;
9 }
10 }
§ ¦

• Line 3 instructs the robot to turn its’ radar independently of the gun.

• Lines 6 and 8 turn the radar right 360 degrees.

Because the radar is turning twice as fast as the gun it will be necessary
to determine where the enemy is and turn the gun towards the enemy. This is
accomplished in two steps.

1. Calculate the absolute bearing of the enemy tank relative to your own
tank. This is determined by adding your tanks’ heading to the bearing
of the enemy robot you scanned. In figure ?? we see that Tank A has
a heading of 0 degrees and the enemy tank has a bearing of -90 degrees
relative to Tank A. The absolute bearing is therefore 0+(-90) i.e. -90
degrees.

2. Determine the position of the enemy tank relative to your gun. This cal-
culation uses the absolute bearing of the enemy tank and the gunheading
of your tank as outlined in the code below. The gunheading is subtracted
from the absolute bearing i.e -90 - 180 = -270 degrees. The value is then
normalised and this converts the value from -270 to +90. The turnGun-
Right command will turn the tank 90 degrees to the right and fire. The
values are normalised so the tank will not attempt to turn -270 degrees
to the right (i.e left) instead it turns only 90 degrees to the right.
CHAPTER 3. INTERMEDIATE CONCEPTS 22

Figure 3.1: Turning the gun to face the enemy, determine its bearing
CHAPTER 3. INTERMEDIATE CONCEPTS 23

Listing 3.3: Determining how much the gun needs to turn so as to shoot a the
¨detected tank ¥
1 public void onScannedRobot ( ScannedRobotEvent e ) {
2 double a b s o l u t e B e a r i n g = getHeading ( ) + e . g e t B e a r i n g ( ) ;
3
4 double bearingFromGun = n o r m a l R e l a t i v e A n g l e (
a b s o l u t e B e a r i n g − getGunHeading ( ) ) ;
5
6 turnGunRight ( bearingFromGun ) ;
7
8 f i r e (3) ;
9 }
§ ¦

• Line 2 calculates the absolute bearing of the enemy tank relative to your
own tank. This is determined by adding your tanks’ heading to the
bearing of the enemy robot you scanned.

• Line 4 determine the position of the enemy tank relative to your gun.
This calculation uses the absolute bearing of the enemy tank and the
gunheading. The gunheading is subtracted from the absolute bearing.
The values are normalised so the tank will select the fastest way to turn
e.g turnRight(-270) or turnRight(90), turning right 90 degrees would be
the most effective.

3.2.1 Exercise
1. If the tank heading is 120 degrees, its gun heading is 90 degrees and the
enemy tank is at a bearing of 45 degrees to the tank. How many degrees
will the tank have to turn its gun right to face the enemy?

2. Draw the two tanks described above on a piece of paper. Ensure that
you have the heading and gun position plotted correctly.
CHAPTER 3. INTERMEDIATE CONCEPTS 24

3.3 Distance from enemy


If an enemy tank is a considerable distance from your tank, then it may be a
waste of valuable energy to fire a shot of strength 3. Conversely if an enemy tank
is quiet close it would make tactical sense to fire a bullet of maximum strength
(3). The distance of a tank can be calculated using the distance method. In
this example the distance method is used to determine what strength bullet
should be used.

¨ Listing 3.4: Using distance to determine what strength shot to fire ¥


1 public void onScannedRobot ( ScannedRobotEvent e ) {
2 double a b s o l u t e B e a r i n g = getHeading ( ) + e . g e t B e a r i n g ( ) ;
3 double bearingFromGun = n o r m a l R e l a t i v e A n g l e (
a b s o l u t e B e a r i n g − getGunHeading ( ) ) ;
4 double ra nge ;
5
6 turnGunRight ( bearingFromGun ) ;
7
8 ra nge = e . g e t D i s t a n c e ( ) ;
9
10 i f ( range > 400) {
11 f i r e (1) ;
12 }
13 else {
14 f i r e (3) ;
15 }
16 }
§ ¦
A more aggressive approach is to get closer to the enemy and then fire.

¨ Listing 3.5: Closing down the distance between your tank and the enemy ¥
1 public void onScannedRobot ( ScannedRobotEvent e ) {
2 double a b s o l u t e B e a r i n g = getHeading ( ) + e . g e t B e a r i n g ( ) ;
3 double bearingFromGun = n o r m a l R e l a t i v e A n g l e (
a b s o l u t e B e a r i n g − getGunHeading ( ) ) ;
4 double ra nge ;
5
6 turnGunRight ( bearingFromGun ) ;
7
8 ra nge = e . g e t D i s t a n c e ( ) ;
9 turnRight ( e . getBearing ( ) ) ;
10 i f ( range > 400) {
11 ahead ( range − 20 0 ) ;
12 }
13 f i r e (3) ;
14 }
§ ¦
Chapter 4

A Review of the Sample Robots

In chapter we review two robots that may be used in the SchoolBots melee
matches as sentry bots. These sentry bots will attempt to destroy all robots
on the playing field. They are:

• WallKiller Clock

• WallKiller Anti Clock

4.1 WallKiller Clockwise and Anti-Clockwise


WallKiller Clockwise and Anti-Clockwise are aggressive but considerate robots.
The robots will disable themselves if there are three enemies or less left on the
battlefield. These robots patrol the edges of the wall looking for robots that
occupy the edges of the arena. It checks to make sure that the robot is not a
sentry robot. If the robot is not a sentry robot and the energy is greater than
30 it fires a bullet of strength 3, then a bullet of strength 2. If the energy is
less than 30 it fires one bullet of strength 3. If the robots hit another robot
they will attack them if they are not a sentry robot. If the enemy is in front
of them, they fire a bullet of strength 3 and then move ahead so as to ram the
enemy. If the enemy is behind they perform a reverse ram.

¨ Listing 4.1: WallKiller Clockwise code ¥


1 package s e n t r y ;
2 import j a v a . awt . Color ;
3 import robocode . ∗ ;
4 public c l a s s S e n t r y W a l l K i l l e r C l o c k extends Robot {
5 boolean peek ; // Don ’ t t u r n i f t h e r e ’ s a r o b o t t h e r e
6 double moveAmount ; // How much t o move
7
8 /∗ ∗
9 ∗ run : Move around t h e w a l l s
10 ∗/
11 public void run ( ) {
12 // S e t c o l o r s
13 s e t C o l o r s ( Color . white , Colo r . white , Co l or . w h ite ) ;

25
CHAPTER 4. A REVIEW OF THE SAMPLE ROBOTS 26

14
15 // I n i t i a l i z e moveAmount t o t h e maximum p o s s i b l e f o r
this battlefield .
16 moveAmount = Math . max( g e t B a t t l e F i e l d W i d t h ( ) ,
getBattleFieldHeight () ) ;
17 // I n i t i a l i z e peek t o f a l s e
18 peek = f a l s e ;
19
20 // t u r n L e f t t o f a c e a w a l l .
21 // g e t H e a d i n g ( ) d i v i d e d by 9 0 .
22 t u r n L e f t ( getHeading ( ) \% 9 0 ) ;
23 ahead ( moveAmount ) ;
24 // Have t h e gun p o i n t a l o n g t h e w a l l
25 peek = true ;
26 turnGunRight ( 0 ) ;
27 //Turn R i g h t so we t r a v e l c l o c k w i s e around t h e e d ge o f
t h e arena
28 turnRight (90) ;
29 while ( true ) {
30 // Look b e f o r e we t u r n when ahead ( ) c o m p l e t e s .
31 i f ( g e t O t h e r s ( ) >3) {
32 peek = true ;
33 // Move up t h e w a l l
34 ahead ( moveAmount ) ;
35 // Don ’ t l o o k now
36 peek = f a l s e ;
37 // Turn R i g h t t o move t o t h e n e x t w a l l
38 turnRight (90) ;
39 }
40 }
41 }
42
43 /∗ ∗
44 ∗ onHitRobot : Move away a b i t i f i t s a n o t h e r s e n t r y ,
o t h e r w i s e f i r e i f he i s i n f r o n t o f us
45 ∗ and t h e n RAM ! I f t h e r o b o t i s b e h i n d perform a
r e v e r s e RAM
46 ∗/
47 public void onHitRobot ( HitRobotEvent e ) {
48 // I f he ’ s i n f r o n t o f us , s e t ba ck up a b i t .
49 i f ( e . getName ( ) . s t a r t s W i t h ( ” s e n t r y ” ) | | e . getName ( ) .
startsWith ( ” Sentry ” ) ) {
50 i f ( e . g e t B e a r i n g ( ) > −90 && e . g e t B e a r i n g ( ) < 9 0 )
{
51 back ( moveAmount ) ;
52 } // e l s e he ’ s i s b e h i n d us , so s e t ahead a b i t .
53 else {
CHAPTER 4. A REVIEW OF THE SAMPLE ROBOTS 27

54 ahead ( moveAmount ) ;
55 }
56 }
57 // i f t h e r o b o t i s not w a l l k i l l e r a t t a c k
58 else {
59 i f ( e . g e t B e a r i n g ( ) > −90 && e . g e t B e a r i n g ( ) < 9 0 )
{
60 f i r e (3) ;
61 ahead ( moveAmount ) ;
62 } // e l s e he ’ s b e h i n d us , so r e v e r s e RAM
63 else {
64 back ( moveAmount ) ;
65 }
66 }
67 }
68
69 /∗ ∗
70 ∗ onScannedRobot : F i r e !
71 ∗/
72 public void onScannedRobot ( ScannedRobotEvent e ) {
73 S t r i n g name ;
74 name= e . getName ( ) ;
75 // I f t h e r o b o t i s w a l l k i l l e r a n t i −c l o c k w i s e b ac k away
76 i f ( name . s t a r t s W i t h ( ” s e n t r y ” ) ) {
77 back ( 1 0 0 ) ;
78 }
79 // i f t h e r o b o t i s not w a l l k i l l e r a t t a c k
80 else {
81 i f ( e . getEnergy ( ) > 3 0) {
82 f i r e (3) ;
83 f i r e (2) ;
84 }
85 else {
86 f i r e (3) ;
87 }
88 i f ( peek ) {
89 scan ( ) ;
90 }
91 }
92 }
93 }
§ ¦
CHAPTER 4. A REVIEW OF THE SAMPLE ROBOTS 28

¨ Listing 4.2: WallKiller AntiClockwise code ¥


1 package s e n t r y ;
2 import j a v a . awt . Color ;
3 import robocode . ∗ ;
4 public c l a s s S e n t r y W a l l K i l l e r A n t i C l o c k extends Robot
{
5 boolean peek ; // Don ’ t t u r n i f t h e r e ’ s a r o b o t t h e r e
6 double moveAmount ; // How much t o move
7
8 /∗ ∗
9 ∗ run : Move around t h e w a l l s
10 ∗/
11 public void run ( ) {
12 // S e t c o l o r s
13 s e t C o l o r s ( Color . white , Colo r . white , Co l or . w h ite ) ;
14
15 // I n i t i a l i z e moveAmount t o t h e maximum p o s s i b l e f o r
this battlefield .
16 moveAmount = Math . max( g e t B a t t l e F i e l d W i d t h ( ) ,
getBattleFieldHeight () ) ;
17 // I n i t i a l i z e peek t o f a l s e
18 peek = f a l s e ;
19
20 // t u r n L e f t t o f a c e a w a l l .
21 // g e t H e a d i n g ( ) d i v i d e d by 9 0 .
22 t u r n L e f t ( getHeading ( ) \% 9 0 ) ;
23 ahead ( moveAmount ) ;
24 // Have t h e gun p o i n t a l o n g t h e w a l l
25 peek = true ;
26 turnGunRight ( 0 ) ;
27 //Turn L e f t so we t r a v e l a n t i −c l o c k w i s e around t h e e dg e
o f t h e arena
28 turnLeft (90) ;
29 while ( true ) {
30 // Look b e f o r e we t u r n when ahead ( ) c o m p l e t e s .
31 i f ( g e t O t h e r s ( ) >2) {
32 peek = true ;
33 // Move up t h e w a l l
34 ahead ( moveAmount ) ;
35 // Don ’ t l o o k now
36 peek = f a l s e ;
37 // Turn R i g h t t o move t o t h e n e x t w a l l
38 turnLeft (90) ;
39 }
40 }
41 }
42
CHAPTER 4. A REVIEW OF THE SAMPLE ROBOTS 29

43 /∗ ∗
44 ∗ onHitRobot : Move away a b i t i f i t s a n o t h e r s e n t r y ,
o t h e r w i s e f i r e i f he i s i n f r o n t o f us
45 ∗ and t h e n RAM ! I f t h e r o b o t i s b e h i n d perform a
r e v e r s e RAM
46 ∗/
47 public void onHitRobot ( HitRobotEvent e ) {
48 // I f he ’ s i n f r o n t o f us , s e t ba ck up a b i t .
49 i f ( e . getName ( ) . s t a r t s W i t h ( ” s e n t r y ” ) | | e . getName ( ) .
startsWith ( ” Sentry ” ) ) {
50 i f ( e . g e t B e a r i n g ( ) > −90 && e . g e t B e a r i n g ( ) < 9 0 )
{
51 back ( moveAmount ) ;
52 } // e l s e he i s b e h i n d us , so s e t ahead a b i t .
53 else {
54 ahead ( moveAmount ) ;
55 }
56 }
57 // i f t h e r o b o t i s not w a l l k i l l e r a t t a c k
58 else {
59 i f ( e . g e t B e a r i n g ( ) > −90 && e . g e t B e a r i n g ( ) < 9 0 )
{
60 f i r e (3) ;
61 ahead ( moveAmount ) ;
62 } // e l s e he i s b e h i n d us , so perform a r e v e r s e
RAM.
63 else {
64 back ( moveAmount ) ;
65 }
66 }
67 }
68
69 /∗ ∗
70 ∗ onScannedRobot : F i r e !
71 ∗/
72 public void onScannedRobot ( ScannedRobotEvent e ) {
73 S t r i n g name ;
74 name= e . getName ( ) ;
75 // I f t h e r o b o t i s w a l l k i l l e r a n t i −c l o c k w i s e b ac k away
76 i f ( name . s t a r t s W i t h ( ” s e n t r y ” ) ) {
77 back ( moveAmount ) ;
78 }
79 // i f t h e r o b o t i s not w a l l k i l l e r a t t a c k
80 else {
81 i f ( e . getEnergy ( ) > 3 0) {
82 f i r e (3) ;
83 f i r e (2) ;
CHAPTER 4. A REVIEW OF THE SAMPLE ROBOTS 30

84 }
85 else {
86 f i r e (3) ;
87 }
88 i f ( peek ) {
89 scan ( ) ;
90 }
91 }
92 }
93 }
§ ¦
Appendix A -Robocode API

• void ahead(double distance) - Moves your robot forward.

• void back(double distance) - Moves your robot backward.

• void doNothing() - Do nothing this turn.

• void finalize() - Called by the system to ’clean up’ after your robot.

• void fire(double power) - Fires a bullet.

• Bullet fireBullet(double power) - Fires a bullet.

• double getBattleFieldHeight() - Get height of the current battlefield.

• double getBattleFieldWidth() - Get width of the current battlefield.

• double getEnergy() - Returns the robot’s current energy.

• double getGunCoolingRate() - Returns the rate at which the gun will


cool down.

• double getGunHeading() - Returns gun heading in degrees.

• double getGunHeat() - Returns the current heat of the gun.

• double getHeading() - Returns the direction the robot is facing, in degrees.

• double getHeight() - Returns the height of the robot

-i
CHAPTER 4. A REVIEW OF THE SAMPLE ROBOTS -ii

• String getName() - Returns the robot’s name

• int getNumRounds() - Returns the number of rounds in the current battle

• int getOthers() - Returns how many opponents are left

• double getRadarHeading() - Returns radar heading in degrees.

• int getRoundNum() - Returns the number of the current round (1 to get-


NumRounds()) in the battle

• long getTime() - Returns the current game time Note: 1 battle consists
of multiple rounds Time is reset to 0 at the beginning of every round.

• double getVelocity() - Returns the velocity of the robot.

• double getWidth() - Returns the width of the robot

• double getX() - Returns the X position of the robot.

• double getY() - Returns the Y position of the robot.

• void onBulletHit(BulletHitEvent event) - This method will be called when


one of your bullets hits another robot.

• void onBulletHitBullet(BulletHitBulletEvent event) - This method will


be called when one of your bullets hits another bullet.

• void onBulletMissed(BulletMissedEvent event) - This method will be


called when one of your bullets misses (hits a wall).

• void onDeath(DeathEvent event) - This method will be called if your


robot dies You should override it in your robot if you want to be in-
formed of this event.

• void onHitByBullet(HitByBulletEvent event) - This method will be called


when your robot is hit by a bullet.
CHAPTER 4. A REVIEW OF THE SAMPLE ROBOTS -iii

• void onHitRobot(HitRobotEvent event) - This method will be called


when your robot collides with another robot.

• void onHitWall(HitWallEvent event) - This method will be called when


your robot collides with a wall.

• void onRobotDeath(RobotDeathEvent event) - This method will be called


if another robot dies You should override it in your robot if you want to
be informed of this event.

• void onScannedRobot(ScannedRobotEvent event) - This method will be


called when your robot sees another robot.

• void onWin(WinEvent event) - This method will be called if your robot


wins a battle.

4.2 Solution to Chapter 3 Exercise


absoluteBearing = getHeading() + e.getBearing();
bearingFromGun = normalRelativeAngle(absoluteBearing - getGunHead-
ing());
Heading = 120, EnemyBearing = 45
absoluteBearing = 120+45 = 165 degrees
Bearing from Gun = normaRelativeAngle(165 - 90) = normaRelativeAn-
gle(75)
Gun Will have to turn right 75 degrees.

Das könnte Ihnen auch gefallen