Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要帮助做一个好的机器人代码机器人吗_Java_Robocode - Fatal编程技术网

Java 需要帮助做一个好的机器人代码机器人吗

Java 需要帮助做一个好的机器人代码机器人吗,java,robocode,Java,Robocode,我是来询问机器人游戏机器人的。我有一个机器人代码,在我的26个朋友中排名第11。然而,我想努力让它变得更好。我浏览了一些网站,调整了我的代码,使它可以不可预测地移动。这使得它在十轮比赛中排名第一。你能给我一些想法和建议来帮助改进这个机器人吗?然后我可以编辑我的机器人,看看它是如何工作的。不过我想让机器人留在机器人里 package aaa; import robocode.*; //import java.awt.Color; // API help: http://robocode.sour

我是来询问机器人游戏机器人的。我有一个机器人代码,在我的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);
    }   
}
aaa包;
进口机器人代码。*;
//导入java.awt.Color;
//API帮助:http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html
/**  
*Epictron-由ASHAR ASLAM设计的机器人!!!
*/
公共级Epictron机器人
{
/**
*运行:Epictron的默认行为
*/
公开募捐{
//机器人的初始化应该放在这里
//试用机器人后,尝试取消顶部的导入注释,
//下一行:
//设置颜色(颜色。蓝色,颜色。蓝色,颜色。灰色,颜色。红色,颜色。绿色);//车身,枪,雷达
//机器人主回路
while(true){
//用您想要的任何行为替换接下来的4行
双距离=数学随机()*300;
双角度=数学随机()*45;
右转(角度);
前方(距离);
领先(100);
右转(90);
背面(100);
右转(90);
}
}
/**
*当你看到另一个机器人时该怎么办
*/
CannedRobot上的公共空间(扫描机器人事件e){
//用您想要的任何行为替换下一行
双距离=e.getDistance();

如果(distance有关于所有顶级机器人的信息——这应该会对你有所帮助。我做了一点机器人编码,发现带模式匹配枪的海浪冲浪可能和你对付大多数机器人一样好,但我花了几个月的时间摸索模式匹配和海浪冲浪,到了足够拼凑出半个机器人的程度-不错的实现。即使这样,我也没有保留足够的知识,在代码丢失时重新实现它。

而不是只是随机旋转,这样你的侧面就可以面对一个你扫描的机器人。这样你可以轻松地左右移动并躲避子弹。你可以随机移动,也可以只在注册一个机器人时移动通用电气在其他机器人的能量水平,因为这可能意味着他们向你开枪


此外,你还应该有一种更好的瞄准敌人的方法。当你看到敌人时,你就开火,这样当子弹击中他们时,他们可能已经移动了。你可以使用基本的三角学来猜测当子弹击中他们时,敌人将在哪里。

首先写下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);
}
现在,机器人将以每秒25像素的速度前进

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

机器人到达墙壁时会被阻挡

我们将使用onHitWall()方法解决此问题

你想创建一个懦夫机器人: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
}
double energy=getEnergy();
HitbyBullet上的公共无效(HitbyBullet事件e){
double-bearing=e.getBearing();//获取子弹到达的方向。
如果(能量<100){//如果能量低,机器人就会离开敌人
右转(-方位);//这不准确,但释放你的机器人。
向前(100);//机器人离开了敌人。
}
其他的
右转(360);//扫描
}
访问此页面查看所有robocode API


:D再见,弗兰克

也许可以尝试发明一些策略。比如追赶另一个机器人。或者逃跑。或者躲在角落里直到最后……你可以发明更多,然后随机决定要做什么。我是机器人代码的新手,但我相信子弹的最大火力是3,所以你使用的3.5是无效的
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.
}
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
}