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