2012-03-15 39 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再見,弗蘭克