43

Great teaching/ learning aid …

Embed Size (px)

DESCRIPTION

Great teaching/ learning aid …. Event handling Inner classes Java docs How to duck… Consume time… Even more time. OO Threading Packaging Inheritance Polymorphism Calling API code. Teamwork. Sharing ideas & techniques Participate internationally in leagues. - PowerPoint PPT Presentation

Citation preview

Page 1: Great teaching/ learning aid …
Page 2: Great teaching/ learning aid …

Great 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...

Page 3: Great teaching/ learning aid …

Teamwork

• You can create your own Robots

• However can participate in a team

• Excellent team builder

• Sharing ideas & techniques

• Participate internationally in leagues

Page 4: Great teaching/ learning aid …

Windows Robocode Installation

Two ways ….– via robocode.alphaworks.ibm.com- download & run robocode-setup.jar (autosetup)

Page 5: Great teaching/ learning aid …

Linux Robocode InstallationFrom robocode.alphaworks.ibm.com download & run …

java –jar robocode-setup.jar

Page 6: Great teaching/ learning aid …

What you get …

Heaps …- a runtime environment … Battlefield- a development IDE - javadocs covering the API- example robots- divorced

Page 7: Great teaching/ learning aid …

Skillset needed …

Various levels catered for …- from beginner - to intermediate- to advanced hacker !!

Page 8: Great teaching/ learning aid …

The 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

Page 9: Great teaching/ learning aid …

The 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

Page 10: Great teaching/ learning aid …

What 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)

Page 11: Great teaching/ learning aid …

Robot 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 ….

Page 12: Great teaching/ learning aid …

Robot commands …

• all documented in the Javadoc of the Robocode API …

• public methods of the robocode.Robot class or derivations such as: robocode.AdvancedRobot

Page 13: Great teaching/ learning aid …

moving 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.

Page 14: Great teaching/ learning aid …

Controlling the Gun, Radar & Tank

• 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.

When the vehicle is turned, the direction of the gun (and radar) will also

move, unless indicate differently by calling the following methods:

Page 15: Great teaching/ learning aid …

Yo !....

• 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.

Methods exist for getting information about the robot:

Page 16: Great teaching/ learning aid …

Firing ....

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 17: Great teaching/ learning aid …

Events ....

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…

Page 18: Great teaching/ learning aid …

Creating a robot ....

• start the Robot Editor • select File->New->Robot • 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)

Page 19: Great teaching/ learning aid …

Generated code …

package dw;import robocode.*;

/** * DWStraight - a robot by (developerWorks) */public class DWStraight extends Robot{

... // <<Area 1>>/** * run: DWStraight's default behavior */public void run() {

... // <<Area 2>>while(true) {... // <<Area 3>>}

} ... // <<Area 4>>

public void onScannedRobot(ScannedRobotEvent e) {

fire(1);}

}

Page 20: Great teaching/ learning aid …

Adding 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(HitByBulletEvent e) {

turnLeft(180);}

}

Page 21: Great teaching/ learning aid …

Compiling & testing your robot

From the Robot Editor menu:• select Compiler->Compile to compile your

robot code. • We are now ready to try our first battle. • switch back to the battlefield and select

menu Battle->New

Page 22: Great teaching/ learning aid …

Robot 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

Page 23: Great teaching/ learning aid …

Battle 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

Page 24: Great teaching/ learning aid …

Battle simulator architecture

•non-preemptive threading

•coupled with the rendering capabilities provided by the JDK GUI and 2D graphics libraries

Page 25: Great teaching/ learning aid …

Battle 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

Page 26: Great teaching/ learning aid …

Battle 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

Page 27: Great teaching/ learning aid …

Getting 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

Page 28: Great teaching/ learning aid …

AdvancedRobotwhile (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

Page 29: Great teaching/ learning aid …

Inheritance 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:

Page 30: Great teaching/ learning aid …

Exploring 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

Page 31: Great teaching/ learning aid …

Blocking vs non-blocking methods

Robot class:•turnRight() •turnLeft() •turnGunRight()•turnGunLeft() •turnRadarRight() •turnRadarLeft() •ahead() •back()

AdvancedRobot class:•setTurnRight() •setTurnLeft() •setTurnGunRight() •setTurnGunLeft() •setTurnRadarRight() •setTurnRadarLeft() •setAhead() •setback()

Page 32: Great teaching/ learning aid …

Working with non-blocking method

calls public class MultiMoveBot extends AdvancedRobot {

...

public void run() {

...

setTurnRight(fullTurn);

setAhead(veryFar);

setTurnGunLeft(fullTurn);

Page 33: Great teaching/ learning aid …

The execute() method

Giving control back to Robocode with a blocking method call:

while(true) {

waitFor(new TurnCompleteCondition(this));

toggleDirection();

}

Page 34: Great teaching/ learning aid …

The toggleDirection() method

private void toggleDirection() {

if (clockwise) {

setTurnLeft(fullTurn);

setBack(veryFar);

setTurnGunRight(fullTurn);

} else {

setTurnRight(fullTurn);

setAhead(veryFar);

setTurnGunLeft(fullTurn);

}

clockwise = ! clockwise;

}

Page 35: Great teaching/ learning aid …

Custom events …public class CustomEventBot extends AdvancedRobot{

...public void run() {

...addCustomEvent(

new Condition("LeftLimit") { public boolean test() {

return (getHeading() <= quarterTurn);};

});

addCustomEvent(new Condition("RightLimit") { public boolean test() {

return (getHeading() >= threeQuarterTurn);};

});

Page 36: Great teaching/ learning aid …

coordinates and direction

Page 37: Great teaching/ learning aid …

Handling custom events

public void onCustomEvent(CustomEvent ev) {

Condition cd = ev.getCondition(); System.out.println("event with " + cd.getName());

if (cd.getName().equals("RightLimit")) { setTurnLeft(fullTurn);

setTurnGunRight(fullTurn);

} else {

setTurnRight(fullTurn);

setTurnGunLeft(fullTurn);

}

}

Page 38: Great teaching/ learning aid …

Interfaces 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

Page 39: Great teaching/ learning aid …

Looking at the DuckSeekerBot

public class DuckSeekerBot extends AdvancedRobot implements DuckConstants

{

boolean targetLocked = false;

Target curTarget = null;

Page 40: Great teaching/ learning aid …

The Target member class

class Target {

...

public Target(String inname, boolean inalive, boolean inlocked) {

... }

public boolean isAlive() { ... }

public boolean isLocked() { ... }

...

} // of Target

Page 41: Great teaching/ learning aid …

Homing in on our target

stop();

turnRight(evt.getBearing());

if (evt.getDistance() > safeDistance) ahead(evt.getDistance() - safeDistance);

Page 42: Great teaching/ learning aid …

Ok 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

Page 43: Great teaching/ learning aid …

Extra Resources 1. Strategies ….2. Tutorials3. Leagues4. Forums5. On-Line help6. Web Sites