2

我是来询问有关 Robocode 机器人的。我有我的机器人代码,我的 26 个朋友排名第 11。但是,我想尝试让它变得更好。我查看了网站并调整了我的代码,以便它可以不可预测地移动。这有助于它在十轮比赛中获得第一名。你能给我一些想法和技巧来帮助改进这个机器人吗?然后我可以编辑我的机器人,看看它是如何工作的。不过,我希望机器人留在扩展机器人中。

package aaa;
import robocode.*;
//import java.awt.Color;

// API help: http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html

/**  
 *Epictron - a robot by ASHAR ASLAM!!!
 */
public class Epictron extends Robot
{
    /**
     * run: Epictron's default behavior
     */
    public void run() {
        // Initialization of the robot should be put here
        // After trying out your robot, try uncommenting the import at the top,
        // and the next line:
        // setColors(Color.blue,Color.blue,Color.grey,Color.red,Color.green); // body,gun,radar
        // Robot main loop
        while(true) {
            // Replace the next 4 lines with any behavior you would like
            double distance = Math.random()*300;
            double angle = Math.random()*45;
            turnRight(angle);
            ahead(distance);
            ahead(100);
            turnGunRight(90);
            back(100);
            turnGunRight(90);
        }
    }

    /**
     * onScannedRobot: What to do when you see another robot
     */
    public void onScannedRobot(ScannedRobotEvent e) {
        // Replace the next line with any behavior you would like
        double distance = e.getDistance();

        if(distance<200)
        {
           fire(3.5);
        }
        else if(distance<500)
        {
           fire(2.5);
        }
        else if(distance<800)
        {
           fire(1.5);
        }
        else
        {
           fire(0.5);
        }
    }

    /**
     * onHitByBullet: What to do when you're hit by a bullet
     */
    public void onHitByBullet(HitByBulletEvent e) {
        // Replace the next line with any behavior you would like
        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);
    }   
}
4

3 回答 3

3

首先编写 OnScannedRobot 方法。

不要使用随机值,因为它不准确。

雷达指向枪的相同角度。因此,当雷达指向机器人并对其进行扫描时,机器人正在开火。

当雷达扫描机器人时调用 onScanned() 方法。

public void onScannedRobot(ScannedRobotEvent e){
    double distance = e.getDistance(); //get the distance of the scanned robot
    if(distance > 800) //this conditions adjust the fire force according the distance of the scanned robot.
        fire(5);
    else if(distance > 600 && distance <= 800)
        fire(4);
    else if(distance > 400 && distance <= 600)
        fire(3);
    else if(distance > 200 && distance <= 400)
        fire(2);
    else if(distance < 200)
        fire(1);
}

所以,现在我们编写 run() 方法。

我们只在循环中编写。因此,循环每秒重复相同的操作。

为了扫描所有区域,我们将枪旋转 360 度。

while(true){
    ahead(100); //Go ahead 100 pixels
    turnGunRight(360); //scan
    back(75); //Go back 75 pixels
    turnGunRight(360); //scan

    //For each second the robot go ahead 25 pixels.
}

现在,机器人将以每秒 25 个像素的速度前进。

机器人迟早会到达地图的墙壁。

机器人到达墙壁时可以被阻挡。

我们将使用 onHitWall() 方法解决。

public void onHitWall(HitWallEvent e){
    double bearing = e.getBearing(); //get the bearing of the wall
    turnRight(-bearing); //This isn't accurate but release your robot.
    ahead(100); //The robot goes away from the wall.
}

你想创造一个懦夫机器人:D?如果能量低,请使用 onHitByBullet() 方法离开。当机器人被子弹击中时,调用此方法。

double energy = getEnergy();
public void onHitByBullet(HitByBulletEvent e){
    double bearing = e.getBearing(); //Get the direction which is arrived the bullet.
    if(energy < 100){ // if the energy is low, the robot go away from the enemy
        turnRight(-bearing); //This isn't accurate but release your robot.
        ahead(100); //The robot goes away from the enemy.
    }
    else
        turnRight(360); // scan
}

访问此页面以观看所有 robocode API http://robocode.sourceforge.net/docs/robocode/

:D 再见,弗兰克

于 2016-07-18T16:45:03.017 回答
2

而不是随机转动,让您的一侧面向您扫描的机器人。这样您就可以轻松地左右移动并躲避子弹。您可以随机侧身移动,也可以仅在其他机器人能量水平发生变化时才移动,因为这可能意味着它们向您开火。

此外,您应该有更好的方法来瞄准敌人。当你看到它们时,你会开火,所以当子弹到达它们时,它们可能已经移动了。您可以使用基本的三角学来猜测子弹到达敌人时敌人的位置。

于 2012-08-21T05:44:11.500 回答
1

robowiki有关于所有顶级机器人的信息 - 这应该可以帮助你。我做了一些机器人编码,发现波浪冲浪和模式匹配枪可能和你对抗大多数机器人一样好,但是我花了几个月的时间来摸索模式匹配和波浪冲浪到足够的拼凑一个半体面的实现的程度。即便如此,当代码丢失时,我也没有保留足够的知识来重新实现它。

于 2012-03-15T20:28:49.670 回答