[英]I do not understand why when I run this code it never goes into the onScannedRobot(ScannedRobot e) method?
我正在使用Java中的環境Robocode,並試圖創建一個與示例機器人Spinbot相對應的機器人。 我正在計算旋轉機器人繞過的圓圈的中心,並以此為目標以獲得擊中旋轉機器人的最佳機會。 我的代碼可以很好地編譯,但是當我運行它時,它永遠不會進入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));
}
}
如果有人能告訴我我做錯了/如何修復它,以便代碼進入此方法,那就太好了,謝謝。
我不確定這是否已得到解決(我肯定希望在10個月后如此),但確實可供將來參考。 ScannedRobotEvent
僅在執行Scan()
方法或雷達波束移動時觸發。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.