简体   繁体   中英

Need Help To Make A Good Robocode Robot

I came to ask about Robocode robots. I have a code for my robots and against 26 of my friends it came 11th. However, I want to try to make it better. I have looked over websites and adjusted my code so it can move unpredictably. This helped it come 1st once in ten rounds. Could you please give me some ideas and tips to help improve this robot please? I can then edit my robot and see how it does. I want the robot to remain in extends Robot though.

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

First write the OnScannedRobot method.

Don't use random values because it is inaccurate.

The radar points at the same angulation of the gun. So, when the radar points at robot and scan it, the robot is firing.

The method onScanned() is called when the radar scan a robot.

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

So, now we write the run() method.

We write only in the loop. So, the loop repeat the same operations in each second.

To scan all the zone, we rotate the gun at 360 degrees.

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.
}

Now, the robot will go ahead 25 pixels per second.

Sooner or later the robot will reach the wall of the map.

The robot can be blocked when reaches the wall.

We'll resolve using onHitWall() method.

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.
}

You want to create a coward robot :D? Use the onHitByBullet() method to get away if the energy is low. When the robot is stricken by a bullet, this method is called.

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
}

visit this page to watch all robocode API http://robocode.sourceforge.net/docs/robocode/

:D goodbye, Frank

Instead of just turning randomly turn so that your side faces a robot that you scan. This way you can move side to side easily and dodge the bullets. You can either move sideways randomly or only move when you register a change in the other robots energy level because that could mean that they fired at you.

Also you should have a better way of targeting the enemy. When you see them you fire so by the time the bullet reaches them they have probably moved. You can use basic trigonometry to guess where the enemy will be when the bullet reaches them.

The robowiki has information on all the top bots - that should help you out. I've done a bit of robocoding and found that wave surfing along with a pattern-matching gun is probably as good as you're going to get against most bots, but it took me months to grok pattern matching and wave surfing to enough of an extent to cobble together a half-decent implementation. Even then, I didn't retain enough of the knowledge to re-implement it when the code was lost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM