簡體   English   中英

需要幫助才能使一個好的Robocode機器人

[英]Need Help To Make A Good Robocode Robot

我來問一下Robocode機器人。 我有一個用於機器人的代碼,針對我的26個朋友,它排在第11位。 但是,我想嘗試使其更好。 我查看了網站並調整了代碼,以使它可以意外地移動。 這幫助它每十回合排名第一。 您能否給我一些想法和提示,以幫助改進此機器人? 然后,我可以編輯我的機器人,看看它是如何工作的。 我希望機器人保留在extend Robot中。

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);
    }   
}

首先編寫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再見,弗蘭克

而不是隨機轉彎,以便您的側面面對要掃描的機器人。 這樣,您可以輕松地左右移動並躲開子彈。 您可以隨意側向移動,也可以僅在其他機器人的能級發生變化時移動,因為這可能意味着它們會向您開火。

另外,您還應該有一種更好的針對敵人的方法。 當您看到它們時,您會開火,因此,當子彈到達它們時,它們可能已經移動了。 您可以使用基本的三角函數來猜測子彈到達敵人后的位置。

robowiki提供了所有頂級bot的信息-應該可以為您提供幫助。 我做了一些機器人編碼,發現與模式匹配槍一起使用Wave沖浪可能與您要對抗大多數機器人一樣好,但是花了我幾個月的時間才能完成模式匹配和Wave沖浪。拼湊出一個體面的實施方案的程度。 即使這樣,當代碼丟失時,我仍然沒有足夠的知識來重新實現它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM