No description available.
Java TrainingRobocode: Java Can Be Fun: Java Training Robocode: Java Can Be Fun Written by Jeff Smith (portions borrowed from RoboCode website) Introduction: Introduction Java Libraries A common task for a Java programmer is to identify a library, download it, and then learn how to use it Javadocs are useful here Google is too! For example, if I wanted to use JDBC (Java Database Connectivity) and needed a code example, I could Google this: “JDBC Java example” Or “JDBC Java tutorial” RoboCode: What is It -1: RoboCode: What is It -1 Robocode is a Java library and is free Was originally developed by Mathew A. Nelson in 2001- 2005 Now been taken over by Flemming N. Larsen in 2006 Was created as a tool for teaching Java You can either extend a simple Robot class or an AdvancedRobot class You guessed it, AdvancedRobot extends Robot We’ll use the simpler class, Robot, for our exerciseRoboCode: What is It -2: RoboCode: What is It -2 A Robot/Tank battle simulator Basically, you can extend a Robot class and implement a few methods (like fire()). In a few lines of code, you can define how your Tank will track and fire on enemies You can compete in the Battle Simulator You define a simple (or not so simple, if you choose) Robot class and the RoboCode Simulator does all the graphics and time steps for you You start out with a certain amount of energy, and you lose a lot of energy when you get hit by another robot some energy when you fire your gun a little energy as time ticks byRoboCode: What is It -3: RoboCode: What is It -3RoboCode Intro -1: RoboCode Intro -1 Body - Can move ahead and back, as well as turn left or right. Gun - Mounted on the body. Can turn left or right, and fire energy bullets. Radar - Mounted on the gun. Can turn left or right. Generates events when other robots are detected. RoboCode Intro -2: RoboCode Intro -2 run() method The “main” method of your Robot class This method gets called by the Battle Simulator in turns or time slices your robot gets to “run”, then your opponent gets to “run” Some things to do in the run() method Move ahead or back Turn Scan FireRoboCode Intro -3: RoboCode Intro -3 How do I know what Robot methods I can call? Javadocs, of course! I’ve put a link to the Javadoc for Robot.java on the “Fun Stuff” page http://www-ad.fsl.noaa.gov/ac/javazone/FunStuff.htmlRoboCode Intro -4: RoboCode Intro -4My First Robot Class -1: My First Robot Class -1 package sample; import robocode.*; /** * MyFirstRobot by Mathew Nelson. Moves in seesaw * motion, and spins the gun around at each end */ public class MyFirstRobot extends Robot { public void run() { while (true) //run this loop forever { ahead(100); // Move ahead 100 turnGunRight(360); // Spin gun around, scan back(100); // Move back 100 turnGunRight(360); // Spin gun around, scan } }My First Robot Class -2: My First Robot Class -2 /** * Fire when we see a robot * our gun & radar are pointed in same direction */ public void onScannedRobot(ScannedRobotEvent e) { fire(1); //fire with power=1 } /** * We were hit! Turn perpendicular to the bullet, * so our seesaw might avoid a future shot. */ public void onHitByBullet(HitByBulletEvent e) { turnLeft(90 - e.getBearing()); } } My First Robot Class -3: My First Robot Class -3 Basically, my first robot goes forward 100 units and then backwards 100 units (some kind of movement is important so you can avoid being hit by another Robot’s gun!) It also turns its gun (and radar) around in circles, so it can detect enemy robots When an enemy is detected by this radar rotation/scan, the onScannedRobot() method is invoked automatically by the Battle Simulator Since the gun and radar are in sync (pointing in the same direction), all you need is a call to the fire() method when you detect the enemy robotRobot Class Events Methods -1: Robot Class Events Methods -1 In OOP, event methods are generally invoked by some external piece of code (perhaps another class) You just write the code to respond to the “event” onScannedRobot() - called when the radar detects a robot onHitByBullet() - called when the robot is hit by a bullet onHitRobot() - called when your robot hits another robot onHitWall() - called when your robot hits a wallRobot Class Events Methods -2: Robot Class Events Methods -2 You generally don’t need to invoke these event methods directly (in your own code)—they are invoked on your behalf by the Battle Simulator You can choose whether or not to override these event methods in your own Robot subclass For example, if you want to execute some special code when you hit a wall, implement an onHitWall() method. If you don’t care about hitting walls, don’t implement the methodOther Robot Class Methods -1: Other Robot Class Methods -1 turnRight(double degree) turnLeft(double degree) turn robot by a specified degree ahead(double distance) back(double distance) move robot by the specified distance Note: ahead() and back() returns early if robot hits a wall or another robot. So if you made a call to ahead(9999999), the call would return once your robot hit either a wall or another robot (it wouldn’t go ahead forever). Other Robot Class Methods -2: Other Robot Class Methods -2 turnGunRight(double degree) turnGunLeft(double degree) turns the gun, independent of the vehicle's direction turnRadarRight(double degree) turnRadarLeft(double degree) turns the radar on top of the gun, independent of the gun's direction (and the vehicle's direction) getHeading() returns your current heading in degreesOther Robot Class Methods -2: Other Robot Class Methods -2 turnGunRight(double degree) turnGunLeft(double degree) turns the gun, independent of the vehicle's direction turnRadarRight(double degree) turnRadarLeft(double degree) turns the radar on top of the gun, independent of the gun's direction (and the vehicle's direction) getHeading() returns your current heading in degreesWhat Does This Robot Do? -1: What Does This Robot Do? -1 public class MysteryRobot extends Robot { public void run() { turnLeft(getHeading()); for (;;) { ahead(99999); turnRight(90); } } public void onScannedRobot(ScannedRobotEvent e) { fire(1); } public void onHitByBullet(HitByBulletEvent e) { turnLeft(180); } } What Does This Robot Do? -2: What Does This Robot Do? -2 This robot gets its initial heading, then turns left by that amount so that it faces straight North turnLeft(getHeading()); Next it starts moving North ahead(99999); Whenever it runs into something, the ahead() method returns and this code turns it right by 90 degrees and starts moving in this direction turnRight(90); Whenever it detects an enemy in front of it, it fires fire(1); //fires with powerlevel=1 Whenever it is hit by an enemy bullet, it goes in the opposite direction turnLeft(180); Installing Robocode: Installing Robocode You download the Robocode JAR (~3 Mb) from here http://robocode.sourceforge.net/ You install it by “running” the JAR java –jar robocode-setup-1.2-Beta.jar (or whatever the name is of the JAR file you download)Running Robocode -1: Running Robocode -1 Go To Battle Menu, “New” to choose robots for your battleRunning Robocode -2: Running Robocode -2 After you select your robots, click the “Start Battle” button, and then the battle begins!Robocode Editor: Robocode Editor Go to the Robot Menu/Editor, to open the Editor window. Select File Menu/New to create a new robot. You can also use Eclipse to create your robots.Robocode and Eclipse: Robocode and Eclipse Go to the Options Menu, Preferences, and then go to the “Development Options” tab. Add you Eclipse project to the list. When you create a New Battle, remember to hit F5 (refresh)Debugging Your Robot: Debugging Your Robot You can watch your robot in action by creating a New Battle You might want to compete against “Sitting Duck” in order to test some of your code To see how your robot does against a sample robot, you can create a new battle and watch the action. If you want to see the final results (for example, how many times out of 10 did you win), just minimize the robocode window. The game keeps going, but MUCH faster. You can generate messages to the robocode console window by calling out.println like so out.println(“Here is my message…”); To view the messages, click on your robot name (button) in the upper right of the Robocode windowExercise: Exercise Create a Robot subclass that can defeat the sample robot, MyFirstRobot You can look at the other sample robot classes to get ideas on how to do this You can either use the built in RoboCode editor or Eclipse. Put your code in a package with your name in it For example robocode.jeff or gov.noaa.robocode.jeff This way, we won’t have package/class name conflicts when we have tournaments
cucurbits vad..
By: vadivel0..
Character Tra..
By: ttravis
06 ORBs
By: Toni
OSH Act and R..
By: Tommaso
4G Overview v..
By: Tomasina
Indian Space ..
By: TechGuy
lect4
By: Taddeo
Introducción ..
By: Sweet
Mercantilism
By: Susann
Sor Juana "Ho..
By: sraward
logging in or signing up