⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yngwie.java

📁 2007年robecode大赛高级段冠军代码1!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package emp;

import robocode.*;
import java.awt.Color;
import java.util.Vector;

/**
 *
 *    This robot Yngwie was designed for Robocode andcreated by Enno Peters
 *    If you re-use something for your own bot, please give the author
 *    proper credits in your code.
 *
 *    For Questions or donations contact me at e.peters@ai.rug.nl
 *
 *
 */

public class Yngwie extends AdvancedRobot implements Consts
{
   private int StatBulletsHit;
   private int StatBulletsMissed;
   private int StatSkippedTurns;
   private int StatTimeErrors;
   private static int StatTotalTime;
   private static int StatTotalTimeErrors;
   private static int StatTotalSkippedTurns;
   private static int StatTotalBulletsHit;
   private static int StatTotalBulletsMissed;
   private static long StatTotalWallCollisions;
   private static long StatTotalEnemyCollisions;
   private static long StatTotalExceptions;

   public static double BattleFieldHeight;
   public static double BattleFieldWidth;
   public double X;
   public double Y;
   public double LastX;
   public double LastY;
   private double LastEnemyX;
   private double LastEnemyY;

   public static Yngwie instance;

   public static boolean OneOnOne;

   public long LastTime;

   public Control control;
   public Motor motor;
   public Gunner gunner;
   public Scanner scanner;
   public static EnemyCollection EC;
   public Predictor predictor;
   public Vector bullettrackers;
   public Vector ScanEvents;

   public static long PeaceTime;
   public static boolean Melee;
   public boolean won;


   public void run()
   {
      setColors(Color.green,Color.green,Color.cyan);
      if (EC == null) // eerste ronde
      {
         instance = this;
         EC = new EnemyCollection(this);
         StatTotalTime              = 0;
         StatTotalTimeErrors        = 0;
         StatTotalSkippedTurns      = 0;
         StatTotalWallCollisions    = 0;
         StatTotalBulletsHit        = 0;
         StatTotalBulletsMissed     = 0;
         StatTotalEnemyCollisions   = 0;
         StatTotalExceptions        = 0;
         BattleFieldHeight = getBattleFieldHeight();
         BattleFieldWidth = getBattleFieldWidth();
         PeaceTime = GetCoolingDownTurns();
         Melee = (getOthers() != 1);
      }
      else
         EC.ResetDeaths();

      ScanEvents = new Vector(getOthers());
      bullettrackers = new Vector(10,5);
      control = new Control(this);
      motor = new Motor(this);
      gunner = new Gunner(this);
      scanner = new Scanner(this);
      predictor = new Predictor(this);
      out.println();

      while (true)
      {
         if (getTime() == 0)
            getInitialValues();
         else
         {
            if (LastTime != getTime()-1)
            {
               StatTimeErrors++;
               StatTotalTimeErrors++;
            }
            LastTime = getTime();
         }

         X = getX(); // to reduce calls to get-functions
         Y = getY(); // to reduce calls to get-functions
         try{
         OneOnOne = (getOthers() == 1);
         HandleScanEvents();
         HandleBulletTrackers();
         motor.Update();
         gunner.Update();
         scanner.Update();
         control.Update();
         }
         catch (Exception ex) {
            StatTotalExceptions++;
            out.println(getTime()+" catched it! : "+ex.getMessage());
         }

         if ((won) && (motor.Threats.size() == 0))
            VictoryDance();
         LastX = X;
         LastY = Y;
         execute();
      }
   }

   public void HandleScanEvents(){
      for(int i=0; i<ScanEvents.size();i++)
         EC.StoreScanEvent((ScannedRobotEvent) ScanEvents.elementAt(i));
      ScanEvents.clear();
   }

   public void HandleBulletTrackers(){
      if (bullettrackers.size() == 0)
         return;
      BulletTracker bt;
      Enemy en = null;
      int idx = 0;
      int i = 0;
      while (i < bullettrackers.size())
      {
         bt = (BulletTracker) bullettrackers.elementAt(i);
         if (bt.completed && bt.bulletDead)
         {
            bullettrackers.removeElementAt(i);
            continue;
         }

         bt.Counter -= 1;
         if (bt.Counter > 0)
         {
            i++;
            continue;
         }

         idx = EC.IndexOf(bt.enemy);
         if (idx == -1)
            bullettrackers.removeElementAt(i);
         else
         {
            en = (Enemy) EC.Enemies.elementAt(idx);

            if (!bt.completed)
               bt.completed = en.AddBulletItem(bt,getTime());
            i++;
         }
      }
   }

   public double getTargetField()
   {
      if (OneOnOne)
         return 50.0;
      else
         return 150.0;
   }


   public void onHitRobot(HitRobotEvent event) {
     motor.Collide = true;
     StatTotalEnemyCollisions++;
     scanner.CheckHeading(event.getBearing());
   }

   public void onHitByBullet(HitByBulletEvent e) {
      scanner.CheckHeading(My.AddDegrees(getHeading(),e.getBearing()));
      int Enemyidx = EC.IndexOf(e.getName());
      if (Enemyidx != -1){
         Enemy en = (Enemy) EC.Enemies.elementAt(Enemyidx);
         en.EnergyAdjust += My.getBulletGain(e.getPower());
         en.LastTimeHitMe = getTime();
         en.BulletDamage += My.getBulletDamage(e.getPower());
      }
   }

   public void onHitWall(HitWallEvent event) {
      StatTotalWallCollisions++;
   }

   public void onBulletHitBullet(BulletHitBulletEvent event) {
      Bullet bullet = event.getBullet();
      BulletTracker bt;
      Enemy en;
      Strategy strat;
      for (int i = 0; i < bullettrackers.size(); i++){
         bt = (BulletTracker) bullettrackers.elementAt(i);
         if (bt.bullet == bullet){
            int idx = EC.IndexOf(bt.enemy);
            if (idx > -1) {
               en = (Enemy) EC.Enemies.elementAt(idx);
               for (int j = 0; j < en.Strategies.size(); j++){
                  strat = (Strategy) en.Strategies.elementAt(j);
                  if (strat.ID == bt.Strategy){
                     strat.Failed(bullet.getPower());
                     break;
                  }
               }
            }
            bt.bulletDead = true;
            break;
         }
      }
   }

   public void onBulletHit(BulletHitEvent event) {
      StatBulletsHit++;
      StatTotalBulletsHit++;
      Bullet bullet = event.getBullet();
      BulletTracker bt;
      Enemy en;
      Strategy strat;
      for (int i = 0; i < bullettrackers.size(); i++){
         bt = (BulletTracker) bullettrackers.elementAt(i);
         if (bt.bullet == bullet){
            int idx = EC.IndexOf(bt.enemy);
            if (idx > -1) {
               en = (Enemy) EC.Enemies.elementAt(idx);
               en.EnergyAdjust -= My.getBulletDamage(event.getBullet().getPower());
               if (en.Name == bullet.getVictim()){
                  for (int j = 0; j < en.Strategies.size(); j++){
                     strat = (Strategy) en.Strategies.elementAt(j);
                     if (strat.ID == bt.Strategy){
                        strat.Success(bullet.getPower());
                        break;
                     }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -