0

System.out を使用してデバッグを試み、メソッドが実行されているかどうかを確認しました。run メソッドが正常に実行され、レーダーが回転し始め、ロボット コンソールに Hello が表示されます。onScannedRobot は呼び出されないようです。解決方法の手がかりがまったくありません。戦闘中、ロボットはゲームにうまく組み込まれ、間違いなくレーダーを他のボットに向けています。

package ke_shen;

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

//Oldest Scanned Radar
//Functions by spinning until all robots have been scanned
//then begins to scan in the opposite direction until 
//all robots have been scanned again
//this minimizes the time in between all robots in the battlefield
//can be scanned, maximizing speed of scanning
public class shen_robot extends AdvancedRobot {

    // the use of a linked hash map is deal here to store the enemy
    // robot's names (the key)and their respective absolute bearings (thevalue)

    static double scanDirection;
    static Object sought;
    static Object mostDanger = null;
    static double distance = 50000;
    static int tempindex = 0;
    static int mostDangerIndex;
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<Double> distanceArray = new ArrayList<Double>();
    ArrayList<Double> velocityArray = new ArrayList<Double>();
    ArrayList<Double> headingArray = new ArrayList<Double>();

    public void run() {
        setAdjustRadarForRobotTurn(true);
        setAdjustGunForRobotTurn(true);
        setAdjustRadarForGunTurn(true);
        setAllColors(Color.BLUE);
        System.out.println("Hello.");
        scanDirection = 1;

            // below, scanDirection will be become either negative or positive
            // this changes the direction of the scan from initially
            // clockwise to counterclockwise and vice versa;
            setTurnRadarRightRadians(scanDirection * Double.POSITIVE_INFINITY);
            scan();
            // linearTargeting();
            // execute();

    }

    // removes the robot from the hash map when it dies
    public void onRobotDeathEvent(RobotDeathEvent e) {
        int index = names.indexOf(e.getName());
        names.remove(e.getName());
        distanceArray.remove(index);
        velocityArray.remove(index);
        headingArray.remove(index);
    }

    public void onScannedRobot(ScannedRobotEvent e) {
        System.out.println("Helo.");
        // RADAR
        // the radar will spin in a full circle once in the beginning of the
        // battle
        // and add all the robots to the hash map
        // the second rotation, once it reaches the last robot in the hash map,
        // because the radar heading is now greater than the normalRelative
        // angle
        // scanDirection will become negative, resulting in the radar spinning
        // in the other
        // direction due to the code above in line 31

        // UPDATES PROPERTIES AFTER THE INITIAL 360 degree SCAN
        String name = e.getName();
        if (names.contains(name) == true) {
            tempindex = names.indexOf(name);
            headingArray.remove(tempindex);
            headingArray.add(tempindex, e.getHeadingRadians());
            velocityArray.remove(tempindex);
            velocityArray.add(tempindex, e.getVelocity());
            distanceArray.remove(tempindex);
            distanceArray.add(tempindex, e.getDistance());
        }

        // HEADING
        else {
        int index = names.size()-1;
        headingArray.add(e.getHeadingRadians());
        if (names.size() == getOthers()) {
            scanDirection = Utils.normalRelativeAngle(headingArray.get(index) - getRadarHeadingRadians());
        }

        // VELOCITY
        velocityArray.add(e.getVelocity());

        // DISTANCE & MOSTDANGEROUS
        distanceArray.add(e.getDistance());

        }

        while (distanceArray.iterator().hasNext()) {
            if (distanceArray.iterator().next() < distance) {
                distance = distanceArray.iterator().next();
            }
        }

        mostDangerIndex = distanceArray.indexOf(distance);
    }


    public void addInfo(String name, int number) {

    }

}
4

1 に答える 1

1

些細なテスト

OnScannedRobot をこれに変更すると、正常に実行できます。したがって、ロボットは on scan イベントをキャッチしています。

 public void onScannedRobot(ScannedRobotEvent e) {
    System.out.println("Helo.");
 }

問題を診断する

問題は、ロボットが割り当てられた時間内に自分のターンを完了できなかった場合、ターンがスキップされることです。問題は、OnScannedRobot メソッドのどの部分が時間効率が悪いかということです。

解像度

結局のところ、mostDangerIndex(while ループを含む) 計算が原因です。そこで、OnScannedRobot メソッドを修正するために、mostDangerIndex(while ループを含む) 計算を次のように置き換えました。

mostDangerIndex = distanceArray.indexOf(Collections.min(distanceArray));

今それは動作します! shen_robot が動いています!

于 2016-01-15T13:47:17.420 に答える