0

我在 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));
    }



}

如果有人能告诉我做错了什么/如何修复它,以便代码进入这个方法,那就太好了,谢谢。

4

1 回答 1

1

我不确定这是否已经得到解答或解决(我当然希望在 10 个月后如此),但供将来参考。只有在执行该方法或雷达波束移动ScannedRobotEvent时才会触发。Scan()

于 2015-12-09T12:55:56.037 回答