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

📄 auseless.java

📁 一个短小的ROBOCODE源代码
💻 JAVA
字号:
package wind;
import robocode.*;
import robocode.util.Utils;
import java.awt.geom.Point2D; 
//import java.awt.Color;

/**
* AtestRobot - a robot by (your name here)
*/
public class Auseless extends AdvancedRobot
{
/**
* run: AtestRobot's default behavior
*/
int trackReward[]=new int[3];
int trackStra=0;//0是直接打(固定目标),1是打直线运行的坦克,2是打圆周运动的坦克
boolean getTarget=false;
double radarOffset=0.0;
double gunOffset=0.0;
Target preB=new Target();
Target B=new Target();
Target nextB=new Target();
boolean isahead=false;
double bulletPower=1.0;
double safeDis;
public void run() {
   // After trying out your robot, try uncommenting the import at the top,
   // and the next line:
   //setColors(Color.red,Color.blue,Color.green);
   safeDis=this.getBattleFieldHeight()/25+30;       //通过获得战场高度计算出认为的安全距离(避免撞墙造成能量损失)
   for(int i=0;i<3;++i)
    trackReward[i]=0;

    //将炮管和雷达独立转动

   this.setAdjustGunForRobotTurn(true);   
   this.setAdjustRadarForGunTurn(true);  
   //this.setTurnLeftRadians(this.getHeadingRadians()-Math.PI/2.0);
   while(true) {
	
	//如果没有发现敌人则将雷达无限像右继续转动扫描
	
    if(getTarget==false)           
    {
     this.setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
     this.execute();
    }

 //如果第一次扫描对方和当前扫描对方的能量值之差在0.1-3.0范围,则说明该坦克刚才发射过炮弹

 //执行躲避及开火语句(更适用于1VS1,因为当群战时所扫描到的坦克能量减少很可能并不是针对自己发射,当这种情况下自己的坦克可能撞到别的火炮上)

 //并执行火力方法的调用

    else
    {
     MyScan();
     if((preB.Energy-B.Energy)>0.1||(preB.Energy-B.Energy)<3.0)        
     {
     MyMove();
     }
     MyFire();
     getTarget=false;
     this.execute();
    }
    
    // Replace the next 4 lines with any behavior you would like
   }
}
public void MyScan()
{
   radarOffset=this.format(B.direction-this.getRadarHeadingRadians());         //计算雷达偏移量
   this.setTurnRadarRightRadians(radarOffset*1.2);       //加大每一时间内雷达的扫描范围

}
public void MyFire()
{
   if(nextB.X>-this.safeDis&&nextB.Y>-this.safeDis&&nextB.X<(this.getBattleFieldWidth()+this.safeDis)&&nextB.Y<(this.getBattleFieldHeight()+this.safeDis))
   {
   gunOffset=this.format(nextB.direction-this.getGunHeadingRadians());

   this.setTurnGunRightRadians(gunOffset);
   if(this.getGunHeat()==0.0)
   this.setFire(bulletPower);
   }
}

   /********          移动算法       ********/

public void MyMove()
{
   if(Math.abs(this.getDistanceRemaining())<1)   // ????
   {
     double nextX;
     double nextY;

    //产生两个随机坐标作为下一次运行的目的地
      
     nextX=Math.random()*(this.getBattleFieldWidth()-safeDis*2.0)+safeDis;     /**********   和数据结构种的“OFFSET”算法类似,而感觉上比OFFSET算法随机性更强
                                                                               (在参看了一些机器人运动代码后得出的结论,这类随机数运行的算法效果还不错)**************/
     nextY=Math.random()*(this.getBattleFieldHeight()-safeDis*2.0)+safeDis;
     double dis=Point2D.distance(this.getX(), this.getY(), nextX, nextY);
     double headingArg=Math.acos((nextY-this.getY())/dis);
     if(nextX<this.getX())
     {
         headingArg=2.0*Math.PI-headingArg;
     }
     if(Math.abs(headingArg-this.getHeadingRadians())>Math.PI/2.0)
     {
         headingArg+=Math.PI;
     if(headingArg>2.0*Math.PI)
     {
         headingArg-=2.0*Math.PI;
     }
     this.setTurnRightRadians(headingArg-this.getHeadingRadians());
     this.setAhead(-dis);
   }
   else
   {
      this.setTurnRightRadians(headingArg-this.getHeadingRadians());
      this.setAhead(dis);
   }
   }
}


public double format ( double arg )//make arg into range [-180,180],using to turn
{
   while(arg>Math.PI)
    arg=arg-Math.PI*2.0;
   while(arg<-Math.PI)
    arg=arg+Math.PI*2.0;
   return arg;
}
public void sittingPre(Target preB,Target B,Target nextB,double time)
{
   nextB.assign(B);
}
public void linePre(Target preB,Target B,Target nextB,double time)
{
   double s=0.0;
   if(B.V>=(Rules.MAX_VELOCITY-0.1))
    s=Rules.MAX_VELOCITY*time;
   else if(B.V>0.0)
   {
    s=(Rules.MAX_VELOCITY*Rules.MAX_VELOCITY-B.V*B.V)/(2.0*Rules.ACCELERATION)+(time-(Rules.MAX_VELOCITY-B.V)/Rules.ACCELERATION)*Rules.MAX_VELOCITY;
   }
   else
   {
    s=0.0;
   }
   nextB.Y=B.Y+s*Math.cos(B.heading);
   nextB.X=B.X+s*Math.sin(B.heading);
   nextB.distance=Point2D.distance(this.getX(), this.getY(), nextB.X, nextB.Y);
   nextB.direction=Math.acos((nextB.Y-this.getY())/nextB.distance);
   if(nextB.X<this.getX())
   {
    nextB.direction=2.0*Math.PI-nextB.direction;
   }
}
public void circlePre(Target preB,Target B,Target nextB,double power)
{
   double Boffset=B.heading-preB.heading;
   //if(Math.abs(Boffset)<0.0001)
//   Boffset+=0.001;
   double w=Boffset/(B.gettime-preB.gettime);
   if(Math.abs(w)<0.00001)
    w+=0.00001;
   double r=B.V/w;
   double bulletSpeed=20-3*power;
   nextB.distance=B.distance;
   for(int i=0;i<8;++i)
   {
    double time=nextB.distance/bulletSpeed;
       nextB.heading=B.heading+w*time;
       nextB.X = B.X - r * Math.cos( nextB.heading ) + r * Math.cos( B.heading ); 
       nextB.Y = B.Y + r * Math.sin( nextB.heading ) - r * Math.sin( B.heading );
       nextB.distance=Point2D.distance(this.getX(), this.getY(), nextB.X, nextB.Y);
       nextB.direction=Math.acos((nextB.Y-this.getY())/nextB.distance);
   }
   if(nextB.X<this.getX())
   {
    nextB.direction=2.0*Math.PI-nextB.direction;
   }
}
/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
   getTarget=true;
   preB.assign(B);
   B.update(e,this);
/* for(int i=0;i<3;++i)
    if(this.trackReward[i]>this.trackReward[trackStra])
     this.trackStra=i;*/
   bulletPower=Math.min(1000/this.getEnergy(),3.0);
   double bulletTime=nextB.distance/Rules.getBulletSpeed(bulletPower);
/* switch(this.trackStra)
   {
   case 0:
    this.sittingPre(preB, B, nextB, time);
    break;
   case 1:*/
   // this.linePre(preB, B, nextB, bulletTime);
//   break;
// case 2:*/
    this.circlePre(preB, B, nextB, bulletPower);
// break;
// }
}
public void onHitRobot()
{
   this.trackReward[this.trackStra]+=3;
}
/**
* onHitByBullet: What to do when you're hit by a bullet
*/
public void onHitByBullet(HitByBulletEvent e) {
   //turnLeft(90 - e.getBearing());
}
public void onHitWall(HitWallEvent e)
{
   this.setTurnRightRadians(Math.PI/2.0 - e.getBearingRadians());
}
public void onBulletHit(BulletHitEvent e)
{

}
public void onBulletHitBullet(BulletHitBulletEvent e)// - Method in class robocode.Robot
{

}
//This method is called when one of your bullets hits another bullet. 
public void onBulletMissed(BulletMissedEvent e) //- Method in class robocode.Robot 
//This method is called when one of your bullets misses, i.e. hits a wall. 
{
   this.trackReward[this.trackStra]-=1;
}
public void onCustomEvent(CustomEvent e)// - Method in class robocode.AdvancedRobot 
//This method is called when a custom condition is met.
{

}
public void onDeath(DeathEvent e) //- Method in class robocode.AdvancedRobot 
//This method is called if your robot dies.
{

}

public void onHitRobot(HitRobotEvent e)// - Method in class robocode.Robot 
//This method is called when your robot collides with another robot.
{

}


}


class Target
{
public double X=0.0;
public double Y=0.0;
public double distance=3000;
public double heading=0.0;
public double direction=0.0;//0 2.0*PI
public double bearing=0.0;
public double preEnergy=100.0;
public double Energy=100.0;
public double V=8.0;
public double gettime=0.0;
public void update(ScannedRobotEvent e,AdvancedRobot me)
{
   gettime=e.getTime();
   preEnergy=Energy;
   Energy=e.getEnergy();
   bearing=e.getBearingRadians();
   heading=e.getHeadingRadians();
   Energy=e.getEnergy();
   distance=e.getDistance();
   V=e.getVelocity();
   direction=me.getHeadingRadians()+bearing;
   if(this.direction<0.0)
    this.direction+=2.0*Math.PI;
   if(this.direction>=2.0*Math.PI)
    this.direction-=2.0*Math.PI;
   Y=me.getY()+distance*Math.cos(direction);
   X=me.getX()+distance*Math.sin(direction);
   
}
public void assign(Target another)
{
   this.gettime=another.gettime;
   this.bearing=another.bearing;
   this.direction=another.direction;
   this.distance=another.distance;
   this.Energy=another.Energy;
   this.heading=another.heading;
   this.preEnergy=another.preEnergy;
   this.V=another.V;
   this.X=another.X;
   this.Y=another.Y;
}
}

⌨️ 快捷键说明

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