2015-02-24 140 views
0

我正在使用Java中的環境Robocode,並且正在嘗試創建一個機器人以對抗示例機器人spinbot。我正在計算spinbot繞過的圓圈中心,並用它來達到擊中spinbot的最佳機會。我的代碼編譯得很好,但是當我運行它時,它永遠不會進入onScannedRobot(ScannedRobot e)方法。我通過改變機器人在不同點的顏色來測試它,並且我可以告訴它永遠不會進入。我不明白爲什麼當我運行這段代碼時,它永遠不會進入onScannedRobot(ScannedRobot e)方法?

package LaurasRobot; 
import robocode.*; 
import java.awt.Color; 

// API help : http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html 

/** 
* LaurasRobot - a robot by (Laura) 
*/ 
public class LaurasRobot extends Robot 
{ 
    private double x1; 
    private double x2; 
    private double x3; 
    private double y1; 
    private double y2; 
    private double y3; 
    private int count; 
    private double centerX; 
    private double centerY; 
    /** 
    * run: LaurasRobot's default behavior 
    */ 
    public void run() { 
     setColors(Color.red,Color.white,Color.blue); // body,gun,radar 
     // Robot main loop, moves the robot forward and back 
     while(true) { 
      ahead(100); 
      back(100); 
     } 
    } 

    /** 
    * onScannedRobot: What to do when you see another robot 
    */ 
    public void onScannedRobot(ScannedRobotEvent e) { 
     setBodyColor(Color.yellow);//sets body color 
     //lets the gun, radar and body of the robot move indipendently 
     setAdjustGunForRobotTurn(true); 
     setAdjustRadarForGunTurn(true); 
     setAdjustRadarForRobotTurn(true); 

     if (count == 3)//creates to sample points to calculate the center of the cirlce with 
     { 
      count = 1; 
      x3 = e.getDistance()*(Math.cos(e.getBearing())); 
      y3 = e.getDistance()*(Math.sin(e.getBearing())); 
     } 
     if (count == 2) 
     { 
      count = 3; 
      x2 = e.getDistance()*(Math.cos(e.getBearing())); 
      y2 = e.getDistance()*(Math.sin(e.getBearing())); 
     } 
     else 
     { 
      count = 2; 
      x1 = e.getDistance()*(Math.cos(e.getBearing())); 
      y1 = e.getDistance()*(Math.sin(e.getBearing())); 
     } 

     while(y3 != 0.0) 
     { 
      setBodyColor(Color.blue); 
      if (count == 3)//creates to sample points to have an updated center 
      { 
       count = 1; 
       x3 = e.getDistance()*(Math.cos(e.getBearing())); 
       y3 = e.getDistance()*(Math.sin(e.getBearing())); 
      } 
      if (count == 2) 
      { 
       count = 3; 
       x2 = e.getDistance()*(Math.cos(e.getBearing())); 
       y2 = e.getDistance()*(Math.sin(e.getBearing())); 
      } 
      else 
      { 
       count = 2; 
       x1 = e.getDistance()*(Math.cos(e.getBearing())); 
       y1 = e.getDistance()*(Math.sin(e.getBearing())); 
      } 

      centerPoint(); 

      double angle = angleGun(e); 
      turnGunRight(angle);//points the gun at the center of the circle that spinbot is makeing 

      fire(2);//fires one bullet at power 2 
      setBodyColor(Color.red); 
     } 
    } 

    /** 
    * onHitByBullet: What to do when you're hit by a bullet 
    */ 
    public void onHitByBullet(HitByBulletEvent e) { 
     back(10); 
    } 

    /** 
    * onHitWall: What to do when you hit a wall 
    */ 
    public void onHitWall(HitWallEvent e) { 
     // Replace the next line with any behavior you would like 
     back(20); 
    } 

    //returns the midpoint of two numbers 
    public double midPoint(double x1 , double x2) 
    { 
     double midx1; 
     if (x1 > x2) 
     { 
      midx1 = ((x1 - x2)/2)+x1; 
     } 
     else 
     { 
      midx1 = ((x2-x1)/2)+x1; 
     } 
     return midx1; 
    } 

    //saves the center points in the instance variables 
    public void centerPoint() 
    { 
     double midx1 = midPoint(x1,x2); 
     double midx2 = midPoint(x3,x2); 
     // double midx3 = midPoint(x1,x3); 
     double midy1 = midPoint(y1,y2); 
     double midy2 = midPoint(y3,y2); 
     // double midy3 = midPoint(y1,y3); 


     centerX = (midy2- (newSlope2())*midx2-midy1+(newSlope1())*midx1)/(newSlope1() - newSlope2()); 

     centerY = midy1 - (newSlope1())*midx1+(newSlope1())*(centerX); 

    } 

    //get the angle to move the gun and make it stay their 
    public double angleGun(ScannedRobotEvent e) 
    { 
     double meToCenter = Math.sqrt(((centerX - getX()) * (centerX - getX())) +((centerY - getY()) * (centerY - getY()))); 
     double himToCenter = Math.sqrt(((centerX - x1) * (centerX - x1)) +((centerY - y1) * (centerY - y1))); 
     double angle = e.getBearing() - Math.cosh(((e.getDistance())*(e.getDistance())+(meToCenter)*(meToCenter)-(himToCenter)*(himToCenter))/(2*(e.getDistance())*(meToCenter))); 
     return angle; 
    } 

    //gets the perpendicular reciprocal of the lines connecting the first two points 

    public double newSlope1() 

    { 

     return (-1)/((y1-y2)/(x1-x2)); 

    } 


    //gets the perpendicular reciprocal of the lines connecting the second two points 

    public double newSlope2() 

    { 

     return (-1)/((y3-y2)/(x3-x1)); 
    } 


    //gets the perpendicular reciprocal of the lines connecting the third two points 

    public double newSlope3() 

    { 
     return (-1)/((y1-y3)/(x1-x3)); 
    } 



} 

這將是巨大的,如果有人能告訴我做錯了什麼/如何解決它,這樣的代碼進入這個方法,謝謝。

+1

常識告訴我沒有其他機器人被觸發事件?爲什麼你覺得onScannedRobot應該被調用? – CKing 2015-02-24 10:30:23

+0

你似乎也只是開着前進 - 也許旋轉一點? – 2015-02-24 10:52:02

+0

我的猜測是僅僅調用'new LauraRobot()'並且線程無法運行。還使用'@ Override'。這將找到未被覆蓋的方法,但會引入一個未稱爲重載的方法,因爲方法簽名是錯誤的。 – 2015-02-24 10:52:20

回答

1

我不確定這個問題是否已經得到解答或解決(我確信希望在10個月後),但是爲了將來真正參考任何人。 ScannedRobotEvent只會在執行Scan()方法時觸發,或雷達波束移動。

相關問題