25
niversity of Limerick How does Robocode work? In short it is a framework You write a Java class extending the robocode Your Robot is register for battle and may be inturn be invoked by the framework once registered via a callback You see the effect in the onscreen battlefield In general what is a Framework ? – A Framework specifies a reusable architecture for all or part of a system – May include reusable classes, patterns or templates – In simple terms it can be viewed as a partially developed program – ready for customization

University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

Embed Size (px)

Citation preview

Page 1: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 1

How does Robocode work? In short it is a framework

– You write a Java class extending the robocode– Your Robot is register for battle and may be

inturn be invoked by the framework once registered via a callback

– You see the effect in the onscreen battlefield In general what is a Framework ?

– A Framework specifies a reusable architecture for all or part of a system

– May include reusable classes, patterns or templates

– In simple terms it can be viewed as a partially developed program – ready for customization

Page 2: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 2

What others say? A framework is a reusable design of an

application or subsystem that is represented by a set of abstract classes and the way objects of these classes collaborate– [Johnson 1997].

A framework is a set of cooperating classes that make up a reusable design for a specific class of software– [GoF 1994].

A framework is the skeleton of an application that can be customized by an application developer– [Fayad et al 1999].

Page 3: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 3

Some Characteristics of a Framework

Frameworks exhibit inversion of control at run-time– i.e., the framework determines which objects and

methods to invoke in response to events Some say that a framework follows the job interview

principle– we will call you, don’t call us

This however is not always the case

Page 4: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 4

Framework versus Library

Application Software

Library Components

Application Software

Library Components

Library Implementation

Framework Implementation

Page 5: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 5

Framework implementation Framework components are loosely coupled via

“callbacks” Callbacks allow independently developed software

components to be connected together Callbacks provide a connection-point where generic

framework objects can communicate with application objects (in your case a robot)

The framework provides the common– template– Methods

The application code provides the variant– hook– methods

Page 6: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 6

Event-based architecture The Robocode framework is based on an

Event based architecture or an event driven system

Event driven programming can be considered a different paradigm from procedural or OOP programming– However the system is still usually made of objects

Page 7: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 7

What is an event driven system?

An event is an occurrence of interest– Events take place at a particular time

An event driven system is a software system where events are a driving force– Events from the user (mouse click)– Internal events (variable assignment, timers)– Events from other computing systems (any

message arriving across the network) Or for robocode

– HitByBulletEvent– HitWallEvent

Page 8: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 8

Examples of Event Driven Systems

Mobile phone TV The Windows GUI

What are some events associated with each of these?

Page 9: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 9

Basic Event Driven Programming Model

Event Source Event Handler

System Objects

notifies

modifies

Often a separate handler for each eventEvents generated here

Page 10: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 10

Properties• What are the properties of an event driven

system?• Loose Coupling between source and handler• Behavior of system depends on its state• System often spends much of its time in stasis

• Not often the case in Robocode

• Events occur that propagate through the system and then it returns to statis

Page 11: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 11

Some Robocode Events ScannedRobotEvent

– Handle the ScannedRobotEvent by overriding the onScannedRobot() method; this method is called when the radar detects a robot.

HitByBulletEvent– Handle the HitByBulletEvent by overriding the onHitByBullet()

method; this method is called when the robot is hit by a bullet. HitRobotEvent

– Handle the HitRobotEvent by overriding the onHitRobot() method; this method is called when your robot hits another robot.

HitWallEvent– Handle the HitWallEvent by overriding the onHitWall() method; this

method is called when your robot hits a wall.

» Let look at theses in the API

Page 12: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 12

Robocode: coding Each event class has its own set of member

functions that can be called to assess details about the event

ex. Calling e.getBearing() in onScannedRobot()

Full info on these is available via the API Any additional classes used by your robot

should be placed in the same file after your robot class

Page 13: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 13

Robocode: basics

Coords. are (x,y), with bottom left as (0,0) Heading: degrees, straight up = 0, pos.

clockwise (0 <= heading <= 360) Bearing: relative angle from your heading,

pos. clockwise (-180 <= bearing <= 180) Hitting a wall or another robot ends turn Energy: costs 1 to fire, receive energy

when one of your bullets hits enemy Radar is mounted on gun

Page 14: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 14

Robocode: numbers

Max velocity: 8 Accel: 1/frame, Decel: 2/frame Max turning rate = 10 -.75*getVelocity() Turret turn rate = 20 degrees/frame Radar turn rate = 45 degrees/frame Damage = 4 * pwr, if pwr>1 damage+=2*(pwr-1)

Page 15: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 15

Robocode: numbers

Power = .1 to 3 Bullet speed = 20 – 3 * pwr Heat = 1 + pwr/5 Heat dissipates at .1/frame

Page 16: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 16

Robocode: coding

public void run() {//setColors(Color.red,Color.blue,Color.green);while(true) { // Replace the next 4 lines with any behavior

ahead(100);turnGunRight(360);back(100);turnGunRight(360);

}}

Page 17: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 17

Robocode: coding

ahead(double dist) back(double dist) fire(double pwr) scan() turnGunLeft/Right(double degrees) turnLeft/Right(double degrees) turnRadarLeft/Right(double degrees) stop()/resume()

Page 18: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 18

Robocode: coding Each event class has its own set of member

functions that can be called to assess details about the event

ex. Calling e.getBearing() in onScannedRobot()

Page 19: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 19

Use of Events Determine what type of event object you need to handle Determine what method is used to register interest in the

event Determine what type of object needs to be passed to this

method Create an object of that type Implement the methods necessary to handle the event Add the code to the methods to handle the event

Page 20: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 20

Events Time to look at some code to get a little more

understanding about what an event is

Page 21: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 21

Example basic commands turnRight(double degree) and turnLeft(double degree)

turn the robot by a specified degree. ahead(double distance) and back(double distance)

move the robot by the specified pixel distance– These methods are exited if the robot hits a wall or another

robot turnGunRight(double degree) and turnGunLeft(double

degree) turn the gun– This is independent of the vehicle's direction.

turnRadarRight(double degree) and turnRadarLeft(double degree) turn the radar on top of the gun– This is also independent of both the gun's direction and the

vehicle's direction

Page 22: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 22

Example basic commands When the vehicle is turned, the direction of the gun

(and radar) will also move, unless indicate differently

by calling the following methods: setAdjustGunForRobotTurn(boolean flag)

– If the flag is set to true, the gun will remain in the same direction while the vehicle turns

setAdjustRadarForRobotTurn(boolean flag)– If the flag is set to true, the radar will remain in the same

direction while the vehicle (and the gun) turns setAdjustRadarForGunTurn(boolean flag)

– If the flag is set to true, the radar will remain in the same direction while the gun turns

– It will also act as if setAdjustRadarForRobotTurn(true) has been called

Page 23: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 23

Getting Info Methods exist for getting information

about the robot– getX() and getY() get the current coordinate of the

robot– getHeading(), getGunHeading(), and

getRadarHeading() get the current heading of the vehicle, gun, or radar in degrees

– getBattleFieldWidth() and getBattleFieldHeight() get the dimension of the battlefield for the current round

Page 24: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 24

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) and fireBullet(double

power) are used to fire a bullet with the specified energy (fire power)– the fireBullet() version of the call returns a

reference to a robocode Bullet object that can be used in advanced robots.

Page 25: University of Limerick1 How does Robocode work? u In short it is a framework –You write a Java class extending the robocode –Your Robot is register for

University of Limerick 25

USE THE WEB Lots of info available on building bots Remember once you have become an

experienced user you can then have a look at the actual source for the system– Available at source forge– You may even become a developer