2012-03-15 40 views
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); 
    } 
} 
+0

也许尝试发明了一些策略。像追逐另一个机器人。或者逃跑。或者只是隐藏在角落直到结束......你可以发明更多,然后随机决定什么待办事项。 – bdecaf 2012-03-15 20:37:00

+1

即时新增robocode,但我相信一个子弹的最大火力是3,所以你使用的3.5是无效的 – Yiannis 2015-04-25 15:25:18

回答

0

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

2

而不是只是随机转动,让您的侧面朝向扫描的机器人。这样你可以轻松地左右移动并避开子弹。你可以随意移动或者只在你注册其他机器人能量等级的变化时移动,因为这可能意味着他们向你开火。

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

4

首先编写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再见,弗兰克