如何在我的robocode代码中添加墙避免?

时间:2014-05-20 13:02:22

标签: java robocode

我正在编写一个在Robocode中运行的机器人。我的代码目前正常运行,我的坦克运行就像我希望它运行和移动一样。唯一的问题是它在战斗中多次撞到墙上,每次发生都会失去健康。

我试图在run()方法中使用getX()和getY()与我的网格的长度和宽度,getBattleFieldWidth()和getBattleFieldHeight()来比较我的主题位置,但是,似乎没有用。我也试过把这个代码放在我的onScannedRobot()方法中,但是效果不好。我试图,当我的主体从墙的每一侧接近50个单位时,将我的主体的方向改变为平行于墙壁。但是,这还没有奏效。我也尝试过做同样的事情,但是反转了我的主题的方向,但这也不起作用。我已经在run()和onScannedRobot()方法中尝试了所有这些并且还没有成功。

我试图使用RoboWiki Wall Smoothing/Implementations page中描述的算法:简单迭代墙平滑(通过PEZ),快速墙平滑(通过Voidious),非迭代墙平滑(由David Alves)和非 - Iterative Wall Hugging(由Simonton提供),并且尚未占上风。

我正在使用Robocode V1.7.3.6来运行此代码。

如何在我的代码中添加墙面避免,或者最好是墙面平滑?这是我的代码:

package stephen.tanks;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import robocode.*;
import robocode.util.Utils;


public class AimmaKickeraskTank extends AdvancedRobot {
    static int currentEnemyVelocity;
    static int aimingEnemyVelocity;
    double velocityToAimAt;
    boolean fired;
    Rectangle grid = new Rectangle(0, 0, 800, 600);
    double oldTime;
    int count;
    int averageCount;
    static double enemyVelocities[][] = new double[400][4];
    static double turn = 2;
    int turnDir = 1;
    int moveDir = 1;
    double oldEnemyHeading;
    double oldEnergy = 100;


    @Override
    public void run() {
        setBodyColor(Color.blue);
        setGunColor(Color.blue);
        setRadarColor(Color.black);
        setScanColor(Color.yellow);
        setAdjustGunForRobotTurn(true);
        setAdjustRadarForGunTurn(true);

        while (true) {
            turnRadarRightRadians(Double.POSITIVE_INFINITY);
        }
    }


    @Override
    public void onScannedRobot(ScannedRobotEvent e) {
        double absBearing = e.getBearingRadians() + getHeadingRadians();
        Graphics2D g = getGraphics();
        turn += 0.2 * Math.random();
        if (turn > 8) {
            turn = 2;
        }

        if(oldEnergy - e.getEnergy() <= 3 && oldEnergy - e.getEnergy() >= 0.1) {
            if (Math.random() > .5) {
                turnDir *= -1;
            }
            if(Math.random() > .8) {
                moveDir *= -1;
            }
        }

        setMaxTurnRate(turn);
        setMaxVelocity(12 - turn);
        setAhead(90 * moveDir);
        setTurnLeft(90 * turnDir);
        oldEnergy = e.getEnergy();

        if (e.getVelocity() < -2) {
            currentEnemyVelocity = 0;
        }
        else if (e.getVelocity() > 2) {
            currentEnemyVelocity = 1;
        }
        else if (e.getVelocity() <= 2 && e.getVelocity() >= -2) {
            if (currentEnemyVelocity == 0) {
                currentEnemyVelocity = 2;
            }
            else if (currentEnemyVelocity == 1) {
                    currentEnemyVelocity = 3;
            }
        }
        if (getTime() - oldTime > e.getDistance() / 12.8 && fired == true) {
            aimingEnemyVelocity=currentEnemyVelocity;
        }
        else {
            fired = false;
        }

        enemyVelocities[count][aimingEnemyVelocity] = e.getVelocity();
        count++;

        if(count==400) {
            count=0;
        }

        averageCount = 0;
        velocityToAimAt = 0;

        while(averageCount < 400) {
            velocityToAimAt += enemyVelocities[averageCount][currentEnemyVelocity];
            averageCount++;
        }

        velocityToAimAt /= 400;

        double bulletPower = Math.min(2.4, Math.min(e.getEnergy()/3.5, getEnergy()/9));
        double myX = getX();
        double myY = getY();
        double enemyX = getX() + e.getDistance() * Math.sin(absBearing);
        double enemyY = getY() + e.getDistance() * Math.cos(absBearing);
        double enemyHeading = e.getHeadingRadians();
        double enemyHeadingChange = enemyHeading - oldEnemyHeading;
        oldEnemyHeading = enemyHeading;
        double deltaTime = 0;
        double battleFieldHeight = getBattleFieldHeight();
        double battleFieldWidth = getBattleFieldWidth();
        double predictedX = enemyX, predictedY = enemyY;

        while ((++deltaTime) * (20.0 - 3.0 * bulletPower) < Point2D.Double.distance( myX, myY, predictedX, predictedY)) {      
            predictedX += Math.sin(enemyHeading) * velocityToAimAt;
            predictedY += Math.cos(enemyHeading) * velocityToAimAt;
            enemyHeading += enemyHeadingChange;
            g.setColor(Color.red);
            g.fillOval((int)predictedX - 2,(int)predictedY - 2, 4, 4);

            if (predictedX < 18.0 || predictedY < 18.0 || predictedX > battleFieldWidth - 18.0 || predictedY > battleFieldHeight - 18.0) {   
                predictedX = Math.min(Math.max(18.0, predictedX), battleFieldWidth - 18.0); 
                predictedY = Math.min(Math.max(18.0, predictedY), battleFieldHeight - 18.0);
                break;
            }
        }

        double theta = Utils.normalAbsoluteAngle(Math.atan2(predictedX - getX(), predictedY - getY()));
        setTurnRadarRightRadians(Utils.normalRelativeAngle(absBearing - getRadarHeadingRadians()) * 2);
        setTurnGunRightRadians(Utils.normalRelativeAngle(theta - getGunHeadingRadians()));

        if(getGunHeat() == 0) {
            fire(bulletPower);
            fired = true;
        }         
    }
}

1 个答案:

答案 0 :(得分:0)

检测机器人何时会很快撞到墙壁,并改变你的路线(或向后行驶),如果是这样的话?

要检测附近的墙壁,您可能需要使用getX()getY()检查机器人的当前位置,并将其与竞技场getBattlefieldWidth()的高度和宽度进行比较,{{1} }。

要了解您是否正在开车,可以查看getBattlefieldHeight()getHeading()的标志。

(如果您想要更具体的答案,请尝试指出您遇到问题的哪个方面。)