首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助才能做一个好的Robocode机器人

需要帮助才能做一个好的Robocode机器人
EN

Stack Overflow用户
提问于 2012-03-16 04:16:11
回答 3查看 34.4K关注 0票数 2

我是来打听Robocode机器人的。我对我的机器人有一个代码,与我的26个朋友相比,它排在第11位。然而,我想试着让它变得更好。我已经查看了网站,并调整了代码,以便它可以不可预测地移动。这帮助它在十个回合中获得第一名。你能给我一些想法和提示来帮助改进这个机器人吗?然后我可以编辑我的机器人,看看它是怎么做的。我希望机器人留在extends中。

代码语言:javascript
复制
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);
    }   
}
EN

回答 3

Stack Overflow用户

发布于 2016-07-19 00:45:03

首先编写OnScannedRobot方法。

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

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

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

代码语言:javascript
复制
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度。

代码语言:javascript
复制
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()方法进行解析。

代码语言:javascript
复制
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()方法。当机器人被子弹击中时,将调用此方法。

代码语言:javascript
复制
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
}

访问此页面以观看所有机器人代码API http://robocode.sourceforge.net/docs/robocode/

:D再见,弗兰克

票数 3
EN

Stack Overflow用户

发布于 2012-08-21 13:44:12

而不是随意转动,这样你的侧面就可以面对你扫描的机器人。这样你就可以轻松地从一边移动到另一边,躲避子弹。你可以随意横向移动,也可以只在其他机器人的能级发生变化时移动,因为这可能意味着它们向你开火。

你也应该有一个更好的瞄准敌人的方法。当你看到他们时,你会开枪,所以当子弹到达他们的时候,他们可能已经移动了。你可以使用基本三角来猜测当子弹到达敌人时他们会在哪里。

票数 2
EN

Stack Overflow用户

发布于 2012-03-16 04:28:50

robowiki上有所有顶级机器人的信息--这应该会对你有所帮助。我做了一些机器人编码,发现wave surfing和模式匹配枪可能是你对抗大多数机器人的最好方法,但我花了几个月的时间摸索模式匹配和wave surfing到足以拼凑出一个不太像样的实现。即使这样,我也没有保留足够的知识来在代码丢失时重新实现它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9727485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档