1 / 43

Great teaching/ learning aid …

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.

Download Presentation

Great teaching/ learning aid …

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Teamwork • Sharing ideas & techniques • Participate internationally in leagues • You can create your own Robots • However can participate in a team • Excellent team builder

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

  4. Linux Robocode Installation From robocode.alphaworks.ibm.com download & run …java –jar robocode-setup.jar

  5. What you get … Heaps … - a runtime environment … Battlefield - a development IDE - javadocs covering the API - example robots - divorced

  6. Skillset needed … Various levels catered for … - from beginner - to intermediate - to advanced hacker !!

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

  8. 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 

  9. 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)

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

  11. Robot commands … • all documented in the Javadoc of the Robocode API … • public methods of the robocode.Robot class or derivations such as: robocode.AdvancedRobot

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

  13. Controlling 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.

  14. Yo !.... 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.

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

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

  17. 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)

  18. 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); } }

  19. 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); } }

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

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

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

  23. Battle simulator architecture • non-preemptive threading • coupled with the rendering capabilities provided by the JDK GUI and 2D graphics libraries

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

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

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

  27. AdvancedRobot 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

  28. 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:

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

  30. 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()

  31. Working with non-blocking method calls public class MultiMoveBot extends AdvancedRobot { ... public void run() { ... setTurnRight(fullTurn); setAhead(veryFar); setTurnGunLeft(fullTurn);

  32. The execute() method Giving control back to Robocode with a blocking method call: while(true) { waitFor(new TurnCompleteCondition(this)); toggleDirection(); }

  33. The toggleDirection() method private void toggleDirection() { if (clockwise) { setTurnLeft(fullTurn); setBack(veryFar); setTurnGunRight(fullTurn); } else { setTurnRight(fullTurn); setAhead(veryFar); setTurnGunLeft(fullTurn); } clockwise = ! clockwise; }

  34. 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); }; } );

  35. coordinates and direction

  36. 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); } }

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

  38. Looking at the DuckSeekerBot public class DuckSeekerBot extends AdvancedRobot implements DuckConstants { boolean targetLocked = false; Target curTarget = null;

  39. The Target member class class Target { ... public Target(String inname, boolean inalive, boolean inlocked) { ... } public boolean isAlive() { ... } public boolean isLocked() { ... } ... } // of Target

  40. Homing in on our target stop(); turnRight(evt.getBearing()); if (evt.getDistance() > safeDistance) ahead(evt.getDistance() - safeDistance);

  41. 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 

  42. Extra Resources • Strategies …. • Tutorials • Leagues • Forums • On-Line help • Web Sites

More Related