0

robocode環境でロボットを作ろうとしています。私の質問は、(たとえば) ロボット クラスの外でメソッド "fire()" を呼び出したい場合 (つまり、Robot を拡張し、run、onHitBybullet、... メソッドを持つクラス)、どうすればよいかということです。 ?

これは私が試したことの1つにすぎません(私の最新):

package sample;

import robocode.HitByBulletEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import sample.Interpretater;

public class MyFirstRobot extends Robot {

Interpretater inter;

public void run() {
    intel = new Interpretator();
    while (true) {
        ahead(50); // Move ahead 100
        //turnGunRight(360); // Spin gun around
        back(50); // Move back 100
        //turnGunRight(360); // Spin gun around
    }
}

public void onScannedRobot(ScannedRobotEvent e) {
    /*If I write fire() here, it will work, but I want to call it 
    from some other class (intel)*/
    inter.onScan();
}

public void onHitByBullet(HitByBulletEvent e) {
    turnLeft(90 - e.getBearing());
}
}   

インタプリタコード:

パッケージサンプル;

public class Interpretator extends MyFirstRobot
{
public Interpretator(){

}

public void onScan(){
    fire(1); //won't work, throws "you cannot call fire() before run()"
}
}

私はJavaの専門家ではないので、何か不足しているかもしれませんが、別のクラスを作成してロボットクラスを拡張しようとしました(したがって、ロボットメソッドを継承します)が、ロボットを拡張するクラスであるためJavaがエラーをスローしましたrun、onHitByBullet .. メソッドが必要です。

4

2 に答える 2

1

これは設計上の問題のようです。

ソリューションは機能しますが、onScan よりも多くのメソッドを追加すると、thisMyFirstRobot から行うすべての呼び出しに渡す必要があります

this代わりに、 Interpretater のコンストラクターで参照を渡します。

Interpretator が MyFirstRobot を拡張するため、エラーが発生します。ロボット参照なしで呼び出すfire(1)と、まだ実行されていない Interpretator で呼び出されますrun()。ロボットに基づいて意思決定を行うための参照としてのみ Interpretator を使用しているように見えるため、Interpretator はロボットではありません。

これらの変更を (フォーマットとともに) 行うと、次のようになります。

package sample;

import robocode.HitByBulletEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import sample.Interpretater;

public class MyFirstRobot extends Robot {

    Interpretater inter;

    public void run() {
        inter = new Interpretator(this); // intel looked like a typo
        while (true) {
            ahead(50); // Move ahead 100
            // turnGunRight(360); // Spin gun around
            back(50); // Move back 100
            // turnGunRight(360); // Spin gun around
        }
    }

    public void onScannedRobot(ScannedRobotEvent e) {
        /*
         * If I write fire() here, it will work, but I want to call it from some
         * other class (intel)
         */
        inter.onScan();
    }

    public void onHitByBullet(HitByBulletEvent e) {
        turnLeft(90 - e.getBearing());
    }
}

public class Interpretator {

    MyFirstRobot robot;

    public Interpretator(MyFirstRobot robot_arg) {
        // constructor sets instance variable
        robot = robot_arg;
    }

    public void onScan() {
        robot.fire(1); // use reference
    }
}
于 2016-03-27T14:19:31.550 に答える
0

私が見つけた可能な答えは、次のように Interpreter.onScan() を変更することです

public class Interpretator extends MyFirstRobot
{
public Interpretator(){

}

    public void onScan(MyFirstRobot robot){
        robot.fire(1); 
    }
}

そして、onScannedRobot では、単にパラメーターとして this を入れます

あなたが持っているなら、より良い答えを出してください。

于 2016-03-27T13:42:05.273 に答える