简体   繁体   中英

I do not understand why when I run this code it never goes into the onScannedRobot(ScannedRobot e) method?

I'm am using the environment Robocode in Java and am trying to create a robot to go up against the sample robot spinbot. I am calculating the center of the circle that the spinbot goes around and using that to aim at to get the best chances of hitting the spinbot. My code compiles fine but when I run it, it never goes into the onScannedRobot(ScannedRobot e) method. I tested it by changing the color of the robot at different points and I could tell it never entered.

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



}

It would be great if someone could tell what I did wrong/ how to fix it so that the code goes into this method, thanks.

I'm not sure if this has already been answered or solved ( I sure hope so after 10 months), but for future reference to anyone really. The ScannedRobotEvent will only trigger upon the execution of the Scan() method, or if the radar beam moves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM