Java 需要帮助来制作一个好的 Robocode 机器人

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9727485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:48:01  来源:igfitidea点击:

Need Help To Make A Good Robocode Robot

javarobocode

提问by user1255673

I came to ask about Robocode robots. I have a code for my robots and against 26 of my friends it came 11th. However, I want to try to make it better. I have looked over websites and adjusted my code so it can move unpredictably. This helped it come 1st once in ten rounds. Could you please give me some ideas and tips to help improve this robot please? I can then edit my robot and see how it does. I want the robot to remain in extends Robot though.

我是来询问 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);
    }   
}

回答by Chris Browne

The robowikihas information on all the top bots - that should help you out. I've done a bit of robocoding and found that wave surfing along with a pattern-matching gun is probably as good as you're going to get against most bots, but it took me months to grok pattern matching and wave surfing to enough of an extent to cobble together a half-decent implementation. Even then, I didn't retain enough of the knowledge to re-implement it when the code was lost.

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

回答by Yep_It's_Me

Instead of just turning randomly turn so that your side faces a robot that you scan. This way you can move side to side easily and dodge the bullets. You can either move sideways randomly or only move when you register a change in the other robots energy level because that could mean that they fired at you.

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

Also you should have a better way of targeting the enemy. When you see them you fire so by the time the bullet reaches them they have probably moved. You can use basic trigonometry to guess where the enemy will be when the bullet reaches them.

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

回答by Frank Soll

First write the OnScannedRobot method.

首先编写 OnScannedRobot 方法。

Don't use random values because it is inaccurate.

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

The radar points at the same angulation of the gun. So, when the radar points at robot and scan it, the robot is firing.

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

The method onScanned() is called when the radar scan a robot.

当雷达扫描机器人时调用 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);
}

So, now we write the run() method.

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

We write only in the loop. So, the loop repeat the same operations in each second.

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

To scan all the zone, we rotate the gun at 360 degrees.

为了扫描所有区域,我们将枪旋转 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.
}

Now, the robot will go ahead 25 pixels per second.

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

Sooner or later the robot will reach the wall of the map.

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

The robot can be blocked when reaches the wall.

机器人碰到墙壁时会被挡住。

We'll resolve using onHitWall() method.

我们将使用 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.
}

You want to create a coward robot :D? Use the onHitByBullet() method to get away if the energy is low. When the robot is stricken by a bullet, this method is called.

你想创造一个懦弱的机器人: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
}

visit this page to watch all robocode API http://robocode.sourceforge.net/docs/robocode/

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

:D goodbye, Frank

:D 再见,弗兰克