Robocode(坦克大战)

来源:互联网 发布:post传递数组 编辑:程序博客网 时间:2024/04/29 17:06

20090519

本来我们学校有一个Java机器人比赛,后来因为参赛人数过少就取消了。这个代码写的并不是很好,等以后闲下来再继续完善。

package my;

import java.awt.Color;
import java.awt.geom.Point2D;
import robocode.*;

public class RoundSheep9 extends AdvancedRobot  
{
    RStarget enemy = new RStarget();
    RSpoint [] point=new RSpoint[5];
   
    double previousEnergy = 100;
    int movementDirection = 1;
    int radarDirection = 1;
    double degree=0;
    double distance=0;
    boolean noTarget = true;
    double RadarOffset;
    int curP=0;
    double firePower=1;
    double preHeading=-1;
    boolean fireStatus=false;
    int missCount=0;
    int gunOffdir=1;
    
    public void run()
    {   
        doInit();
       
        while(true)
        {
                if( noTarget )
        {
            setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
        }
        else
        {
            doScan();
            doMove();
            doFire();
            noTarget = true;
        }
       
        execute();
        fireStatus=false;
        }
    }
   
    public void doInit()
    {
        setBodyColor(Color.black);
     setGunColor(Color.black);
      setRadarColor(Color.black);
        setScanColor(Color.black);
     setBulletColor(Color.black);
       
        setAdjustGunForRobotTurn(true);
        setAdjustRadarForGunTurn(true);
        setAdjustRadarForRobotTurn(true);
       
        for(int i=0;i<5;i++)
            point[i]=new RSpoint();
    }
   
    public void doScan()
    {
    RadarOffset=format(enemy.direction-getRadarHeadingRadians());
    setTurnRadarRightRadians(RadarOffset*2.0);
    }
   
    public void doMove()
    {
        if( Math.abs( getDistanceRemaining() ) < 1 )   
    {
        double myX = getX();
        double myY = getY();
        double nextX, nextY;    

        nextX = Math.random() * ( getBattleFieldWidth() - 100 ) + 50;
        nextY = Math.random() * ( getBattleFieldHeight() - 100 ) + 50;
        
        double turnAngle = enemy.getAngle( myX, myY, nextX, nextY );
        turnAngle = enemy.normalizeBearing( turnAngle - getHeadingRadians() );
        double moveDistance = Point2D.distance( myX, myY, nextX, nextY );
        double moveDirection = 1;
        if ( Math.abs( turnAngle ) > Math.PI/2 )
        {        
            turnAngle = enemy.normalizeBearing( turnAngle + Math.PI );    
            moveDirection = -1;
        }
        setTurnRightRadians( turnAngle );
        setAhead( moveDirection * moveDistance );
    }
    }
   
    public void doFire()
    {
        //System.out.println("Fire");
       
    }
   
    public void onScannedRobot(ScannedRobotEvent e)
    {
        /*for(int i=0;i<5;i++)
        {
            System.out.println(point[i].x);
            System.out.println(point[i].y);
        }*/
        //System.out.println(Math.acos(0.5));
        //System.out.println(Math.acos(0));
        //System.out.println(Math.acos(1));
        //System.out.println(Math.round(3.1));
        //System.out.println(Math.round(3.5));
        //System.out.println(Math.round(3.8));
        //System.out.println(Math.sqrt(3.8));
        //System.out.println("random:"+(int)(Math.random()*1000%2));
       
           
        firePower=300/e.getDistance();
           
        getTarget(e);
            
        noTarget = false;
    enemy.update(e, this);
       
        distance=e.getDistance();
        degree=getGunHeading()-getRadarHeading();
        double changeInEnergy =previousEnergy-e.getEnergy();
       
        if(getOthers()==2)
        {
            setTurnRight(e.getBearing()+90-30*movementDirection);
            
            //System.out.println("changeInEnergy:"+changeInEnergy);
           
            if (changeInEnergy>0 &&changeInEnergy<=3)
            {
                //if(0==(int)(Math.random()*1000%2))
                {        
                    movementDirection =-movementDirection;
                     
                setAhead((distance/4+25)*movementDirection);
                }
                //else
                // setBack((distance/4+25));
            }     
           
           
            radarDirection = -radarDirection;
           
        }
       
        //if(fireStatus==false)
        // setTurnGunLeft(normalRelativeAngle(degree));
       
        //System.out.println("getVelocity():"+e.getVelocity() );
        //System.out.println("getGunHeading():"+getGunHeading());
        //System.out.println("getRadarHeading():"+getRadarHeading());
     
    if(Math.abs(degree)<distance/50+10)
    {
        //System.out.println("getDistance():"+distance);
        //if (getGunHeat() == 0)
        // fire(200/distance);
    }    
     
        previousEnergy = e.getEnergy();
        preHeading=(getHeading() + e.getBearing()) % 360;
           
    }
   
    public void onBulletMissed(BulletMissedEvent event)
    {
        missCount++;
       
        if(7>=missCount)
        {
            gunOffdir=-gunOffdir;
            missCount=0;
        }  
    }
   
    public void onBulletHit(BulletHitEvent event)
    {
        missCount=0;
    }
   
    public double normalRelativeAngle(double angle)
    {
      if (angle > -180 && angle <= 180)
         return angle;

     while (angle <= -180)
          angle += 360;
           
       while (angle > 180)
            angle -= 360;
       
       return angle;
}
   
    public double format(double arg)
    {
       while(arg>Math.PI)
        arg=arg-Math.PI*2.0;
       
       while(arg<-Math.PI)
        arg=arg+Math.PI*2.0;
       
       return arg;
    }
   
    public void getTarget(ScannedRobotEvent e)
    {
        double maxDegree=0;
       
        degree=getGunHeading()-getRadarHeading();
        double bearing = (getHeading() + e.getBearing()) % 360;
        double distance = e.getDistance();
       
        System.out.println("bearing"+bearing);
       
        bearing = Math.toRadians(bearing);
       
       
       
        point[curP].x=Math.round(getX() + Math.sin(bearing) * distance);
        point[curP].y=Math.round(getY() + Math.cos(bearing) * distance);
       
        System.out.println("e.getBearing()"+e.getBearing());
            System.out.println("heading "+getHeading());
            System.out.println("getRadarHeading()"+getRadarHeading());
       
       
        if(point[curP].x!=-1&&
           point[curP].y!=-1&&
           point[prevP()].x!=-1&&
           point[prevP()].y!=-1&&
           (point[prevP()].x!=point[curP].x||
           point[prevP()].y!=point[curP].y))
        {
            double timeB=e.getDistance()/(20-3*firePower);
            double t=timeB;
            double v0=Math.abs(e.getVelocity());
            double moveD=0;
           
            if(v0+1*timeB>8)
            {
                moveD=(8*8-v0*v0)/2/1+8*(timeB-(8-v0)/1);
            }
            else
            {
                moveD=v0*timeB+1*timeB*timeB/2;
            }
           
            double moveS=getD(point[prevP()].x,point[prevP()].y,point[curP].x,point[curP].y);
           
            long maxX=Math.round(moveD/moveS*(point[curP].x-point[prevP()].x)+point[curP].x);
            long maxY=Math.round(moveD/moveS*(point[curP].y-point[prevP()].y)+point[curP].y);
           
            maxDegree=Math.acos((e.getDistance()*e.getDistance()+getD(maxX,maxY,Math.round(getX()),Math.round(getY()))*getD(maxX,maxY,Math.round(getX()),Math.round(getY()))-moveD*moveD)/2/e.getDistance()/getD(maxX,maxY,Math.round(getX()),Math.round(getY())));

            /*double minDegree=-v0*v0/4+8*8/2/1;
           
            if(v0/2>t)
                minDegree=0;
            else if(v0/2+8/1>t)
            {
                minDegree=-v0*v0/4+1*(t-v0/2)*(t-v0/2)/2;
               
                if(minDegree<0)
                    minDegree=0;
                else
                {
                    minDegree=getCos();
                }
            }
            else
            {
                minDegree+=(t-v0/2-8/1)*8;
               
                minDegree=getCos();
            }*/
            //System.out.println("maxDegree:"+maxDegree/Math.PI*180);
            //System.out.println("getRadarHeading()"+getRadarHeading());
            //System.out.println("preHeading"+preHeading);
           
           
            maxDegree=maxDegree/Math.PI*180*Math.abs(e.getVelocity())/8*(firePower/5+1)/1.1764;
            //System.out.println("maxDegree:"+maxDegree);  
               
            double curHeading=(getHeading() + e.getBearing()) % 360;
           
            if(preHeading<0)
                ;
            else
            {
                if(curHeading-preHeading>0||curHeading-preHeading<-180)
                    gunOffdir=-1;
                else
                    gunOffdir=1;
            }
                /*if(e.getVelocity()<0)
                {//getRadarHeading()-preHeading>0
                    maxDegree=-Math.abs(maxDegree);
                   
                }
                else
                    maxDegree=Math.abs(maxDegree);*/
               
        }
       
        setTurnGunLeft(normalRelativeAngle(degree)+maxDegree*gunOffdir);
        setFire(firePower);
       
        curP=nextP();
    }
   
    public int nextP()
    {
        if(4==curP)
            return 0;
        else
            return curP+1;
    }
   
    public int prevP()
    {
        if(0==curP)
            return 4;
        else
            return curP-1;
    }
   
    public double getD(long x1,long y1,long x2,long y2)
    {
        return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    }
   
    /*public double getCos(double minDegree,double moveS)
    {
        long minX=Math.round(point[curP].x-minDegree/moveS*(point[curP].x-point[prevP()].x));
        long minY=Math.round(point[curP].y-minDegree/moveS*(point[curP].y-point[prevP()].y));
       
       
    }*/
}

class RSpoint
{
    public long x=-1;
    public long y=-1;
}

class RStarget
{
    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)
        direction+=2.0*Math.PI;
       if(this.direction>=2.0*Math.PI)
        direction-=2.0*Math.PI;
       Y=me.getY()+distance*Math.cos(direction);
       X=me.getX()+distance*Math.sin(direction);
      
    }
   
    public static double normalizeBearing( double angle )
{
      if ( angle < -Math.PI )
          angle += 2*Math.PI;
      if ( angle > Math.PI )
          angle -= 2*Math.PI;
      return angle;
}
  
public static double getAngle(double x1, double y1, double x2, double y2)
{
      return Math.atan2( x2 - x1, y2 - y1 );
}
}


原创粉丝点击