Advertisement
capncoolio

Robot!

Jan 7th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package tp;
  2. import robocode.*;
  3. import java.util.*;
  4. import java.awt.Color;
  5.  
  6. // API help : http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html
  7.  
  8. /**
  9.  * Killer3000 - a robot by (your name here)
  10.  */
  11. public class Killer3000 extends AdvancedRobot
  12. {
  13.     /**
  14.      * run: Killer3000's default behavior
  15.      */
  16.     Random r = new Random();
  17.    
  18.     public void run () {
  19.         // Initialization of the robot should be put here
  20.  
  21.         // After trying out your robot, try uncommenting the import at the top,
  22.         // and the next line:
  23.  
  24.         setColors(Color.red,Color.blue,Color.green); // body,gun,radar
  25.  
  26.         // Robot main loop
  27.         setAdjustGunForRobotTurn (true);
  28.         setAdjustRadarForGunTurn(true);
  29.         while (true) {
  30.             turnRadarLeft (90);
  31.         }
  32.     }
  33.  
  34.     /**
  35.      * onScannedRobot: What to do when you see another robot
  36.      */
  37.     public double normalize (double angle) {
  38.         while (angle >  180) angle -= 360;
  39.         while (angle < -180) angle += 360;
  40.         return angle;
  41.     }
  42.    
  43.     public void onScannedRobot(ScannedRobotEvent e) {
  44.         // Replace the next line with any behavior you would like
  45.         if (getGunHeat() == 0 && Math.abs(getGunTurnRemaining()) < 1)
  46.             fire(Math.min(600 / e.getDistance(), 3));
  47.         double a = (getHeading () - getGunHeading () + e.getBearing ());
  48.         turnGunRight (normalize(a));
  49.     }
  50.  
  51.     /**
  52.      * onHitByBullet: What to do when you're hit by a bullet
  53.      */
  54.     public void onHitByBullet (HitByBulletEvent e) {
  55.         int a = r.nextInt(100);
  56.         if (a < 50)
  57.         {
  58.             turnLeft(r.nextInt(30) + 30);
  59.         } else
  60.         {
  61.             turnRight(r.nextInt(30) + 30);
  62.         }
  63.         ahead (r.nextInt(70) + 30);
  64.     }
  65.    
  66.     /**
  67.      * onHitWall: What to do when you hit a wall
  68.      */
  69.     public void onHitWall(HitWallEvent e) {
  70.         // Replace the next line with any behavior you would like
  71.         turnLeft (r.nextInt(90) + 90);
  72.         ahead (100);
  73.     }  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement