Beruflich Dokumente
Kultur Dokumente
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
3.1 Turning the gun to face the enemy, determine its bearing . . . . 22
iii
Listings
iv
Chapter 1
Introduction
1
CHAPTER 1. INTRODUCTION 2
• Rapid reaction to being hit and appropriate avoidance and attack strate-
gies
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.
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.
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
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.
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.
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.
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
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:
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.6 Scoring
When a battle is complete a score board for the battle is displayed
• 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.
• Ram Damage - Robots score 2 points for each point of damage they cause
by ramming enemies.
• 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
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.
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.)
1. Minimize (or close) the Robot Editor and return to the original Robocode
program.
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
2.2.1 Exercise
1. Identify 3 good 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
37 }
38
39 }
§ ¦
There are several sections to MyFirstRobot and we will now examine each
one.
• 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 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.
The robot will continue doing this over and over and over, until it dies, due to
the while(true) statement.
• 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.
• 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?
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
• 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 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?
5. Change the while(true) section of the run method of your robot so that
it performs the following.
Intermediate Concepts
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.
• Line 7 set the colours for the body, gun and radar.
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.
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
¨ 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
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
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
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 finalize() - Called by the system to ’clean up’ after your robot.
-i
CHAPTER 4. A REVIEW OF THE SAMPLE ROBOTS -ii
• 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.