Title: Robocode
1(No Transcript)
2Great teaching/ learning aid
- OO
- Threading
- Packaging
- Inheritance
- Polymorphism
- Calling API code
- Event handling
- Inner classes
- Java docs
- How to duck
- Consume time
- Even more time...
3Teamwork
- Sharing ideas techniques
- Participate internationally in leagues
- You can create your own Robots
- However can participate in a team
- Excellent team builder
4Windows Robocode Installation
Two ways . via robocode.alphaworks.ibm.com -
download run robocode-setup.jar (autosetup)
5Linux Robocode Installation
From robocode.alphaworks.ibm.com download run
java jar robocode-setup.jar
6What you get
Heaps - a runtime environment
Battlefield - a development IDE - javadocs
covering the API - example robots - divorced
7Skillset needed
Various levels catered for - from beginner
- to intermediate - to advanced hacker !!
8The battlefield ..
- houses the main simulation engine
- allows you to create, save and open new or
existing battles - you can pause or resume a battle
- terminate the battle
- obtain statistics on any robot using controls
available in main arena - activate the IDE
-
-
-
9The IDE
- the Robocode editor is a customised text editor
for Java source files that make up a robot. - it is integrated with the Java compiler
- customised Robot packager
- you can use any other editor Eclipse is very
useful and . FREE ? -
-
-
10What is a Robot ?
- consists of one or more Java classes
- can be archived into a JAR package
- packager available from the Battlefield GUI
- they can range from blind ram em, shoot em
types to devious little buggers - they can work in teams
- they can control droid robots (these can act as
decoys, battering rams etc but they last longer) -
-
-
11Robot anatomy 101
- Just like the real thing
- it has a gun that rotates
- a radar on top that also rotates
- tank, gun and radar can rotate independently
- default, all aligned
- sorry no sound .
-
12Robot commands
- all documented in the Javadoc of the Robocode API
- public methods of the robocode.Robot class or
derivations such as robocode.AdvancedRobot -
13moving your Robot
Some 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 two methods are completed if the robot hits
a wall or another robot. - turnGunRight(double degree) and
turnGunLeft(double degree) turn the gun,
independent of the vehicle's direction. - turnRadarRight(double degree) and
turnRadarLeft(double degree) turn the radar on
top of the gun, independent of the gun's
direction (and the vehicle's direction). - None of these commands will return control to the
program until they are completed. -
14Controlling the Gun, Radar Tank
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.
15Yo !....
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.
16Firing ....
- 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.
17Events ....
- Here are some of the more frequently used 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. - That's all we need to know to create some pretty
complex robots
18Creating a robot ....
- start the Robot Editor
- select File-gtNew-gtRobot
- when prompted, name your robot (this will become
the Java class name e.g (DWStraight) - when prompted, enter an initial (used for the
name of the package e.g dw)
19Generated code
package dw import robocode. / DWStraight
- a robot by (developerWorks) / public class
DWStraight extends Robot ... // ltltArea
1gtgt / run DWStraight's default behavior
/ public void run() ... // ltltArea
2gtgt while(true) ... // ltltArea 3gtgt
... // ltltArea 4gtgt public void
onScannedRobot(ScannedRobotEvent e)
fire(1)
20Adding functionality
package dw import robocode. public class
DWStraight extends Robot public void run()
turnLeft(getHeading()) while(true)
ahead(1000) turnRight(90)
public void onScannedRobot(ScannedRobotEvent e)
fire(1) public void onHitByBullet(HitByBu
lletEvent e) turnLeft(180)
21Compiling testing your robot
- From the Robot Editor menu
- select Compiler-gtCompile to compile your robot
code. - We are now ready to try our first battle.
- switch back to the battlefield and select menu
Battle-gtNew
22Robot support classes
- Design of Robots rapidly gets complex.
- Modularity is a good aim
- Decompose into separate Java classes
- Bundle into a single JAR file (using supplied
packager) - Robocode will automatically handle class
dependencies
23Battle simulator features
- sophisticated simulation engine
- high performance (in order to render the battle
at realistic speed) - flexible (enabling the creation of complex
robotics logic without getting in the way) - design that leverages the Java platform
24Battle simulator architecture
- non-preemptive threading
- coupled with the rendering capabilities provided
by the JDK GUI and 2D graphics libraries
25Battle simulator features
- sophisticated simulation engine
- high performance (in order to render the battle
at realistic speed) - flexible (enabling the creation of complex
robotics logic without getting in the way) - design that leverages the Java platform
26Battle simulator psuedo code logic
while (round is not over) do call the
rendering subsystem to draw robots, bullets,
explosions for each robot do wake up
the robot wait for it to make a blocking
call, up to a max time interval end for
clear all robot event queue move bullets, and
generate event into robots' event queue if
applicable move robots, and generate event
into robots' event queue if applicable do
battle housekeeping and generate event into
robots' event queue if applicable
delay for frame rate if necessary end do
27Getting more sophisticated
- so far the Robots are relatively simple
- restrictive Robot class ... blocking
- methods do not return control to our code until
they finish operation - we are essentially passing up on the ability to
make decisions on every turn
enter AdvancedRobot
28AdvancedRobot
while (round is not over) do call the
rendering subsystem to draw robots, bullets,
explosions for each robot do wake up
the robot wait for it to make a blocking
call, up to a max time interval end for
clear all robot event queue move bullets, and
generate event into robots' event queue if
applicable move robots, and generate event
into robots' event queue if applicable do
battle housekeeping and generate event into
robots' event queue if applicable
delay for frame rate if necessary end do
29Inheritance relationships
If we want to create a robot called MultiMoveBot
that inherits from AdvancedRobot, we'd use the
following code public class MultiMoveBot extends
AdvancedRobot Note AdvancedRobot is actually
a subclass of Robot, and our own MultiMoveBot is
in turn a subclass of AdvancedRobot, as
illustrated in the class hierarchy shown
30Exploring AdvancedRobot
- has non blocking API calls
- can change the robot's action on every turn
- a turn in Robocode is called a tick (as in a
clock tick), and relates to a graphical frame
that is displayed on the battlefield
31Blocking vs non-blocking methods
- Robot class
- turnRight()
- turnLeft()
- turnGunRight()
- turnGunLeft()
- turnRadarRight()
- turnRadarLeft()
- ahead()
- back()
- AdvancedRobot class
- setTurnRight()
- setTurnLeft()
- setTurnGunRight()
- setTurnGunLeft()
- setTurnRadarRight()
- setTurnRadarLeft()
- setAhead()
- setback()
32Working with non-blocking method calls
public class MultiMoveBot extends AdvancedRobot
... public void run() ... setTurnRight(fu
llTurn) setAhead(veryFar) setTurnGunLeft(ful
lTurn)
33The execute() method
Giving control back to Robocode with a blocking
method call while(true) waitFor(new
TurnCompleteCondition(this)) toggleDirection()
34The toggleDirection() method
private void toggleDirection() if (clockwise)
setTurnLeft(fullTurn) setBack(veryFar) s
etTurnGunRight(fullTurn) else
setTurnRight(fullTurn) setAhead(veryFar)
setTurnGunLeft(fullTurn) clockwise !
clockwise
35Custom events
public class CustomEventBot extends
AdvancedRobot ... public void run()
... addCustomEvent( new
Condition("LeftLimit") public boolean
test() return (getHeading() lt
quarterTurn) )
addCustomEvent( new Condition("RightLimit")
public boolean test() return
(getHeading() gt threeQuarterTurn)
)
36coordinates and direction
37Handling custom events
public void onCustomEvent(CustomEvent ev)
Condition cd ev.getCondition()
System.out.println("event with "
cd.getName()) if (cd.getName().equals("RightLimi
t")) setTurnLeft(fullTurn) setTurnGunRigh
t(fullTurn) else setTurnRight(fullTurn)
setTurnGunLeft(fullTurn)
38Interfaces and inner classes
- The three key new features provided by
AdvancedRobot are the ability to - Carry out multiple movements simultaneously
- Decide on the robot's action or strategy at every
clock tick - Define and handle custom events
39Looking at the DuckSeekerBot
public class DuckSeekerBot extends AdvancedRobot
implements DuckConstants boolean
targetLocked false Target curTarget
null
40The Target member class
class Target ... public Target(String
inname, boolean inalive, boolean inlocked)
... public boolean isAlive() ... public
boolean isLocked() ... ... // of Target
41Homing in on our target
stop() turnRight(evt.getBearing()) if
(evt.getDistance() gt safeDistance)
ahead(evt.getDistance() - safeDistance)
42Ok Lets wrap up for now
Getting the big picture on the battlefield
Vector, polymorphism, and java.MathThe
DuckSeekerBot Scans for a duck target Zooms
in and roasts the target Repeats until the
entire flock is gone An alternative approach to
the same problem is this Scan for all the ducks
that can be seen in the battlefield and build an
"intelligence map" Zoom in on the flock one at
a time to eliminate them Update the "map"
constantly from scan information This second
approach achieves the same result as the first,
but uses more intelligence. Most advanced robots
use this sort of "big picture" information in
formulating an instantaneous strategic decision.
Learning how to maintain such a map will allow us
to create robots with more sophisticated
intelligence. Get the idea ? Robocode really
does expand your Java knowledge ?
43Extra Resources
- Strategies .
- Tutorials
- Leagues
- Forums
- On-Line help
- Web Sites