我是来问Robocode机器人的。我有一个机器人代码,与我的 26 个 friend 相比,它排在第 11 位。但是,我想尝试让它变得更好。我查看了网站并调整了我的代码,以便它可以不可预测地移动。这帮助它在十轮比赛中获得第一名。你能给我一些想法和技巧来帮助改进这个机器人吗?然后我可以编辑我的机器人,看看它是如何工作的。不过,我希望机器人保留在 extends Robot 中。
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);
}
}
最佳答案
先写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 像素的速度前进。
机器人迟早会到达 map 的墙壁。
机器人到达墙壁时可以被挡住。
我们将使用 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/
:再见,弗兰克
关于java - 需要帮助来制作一个好的 Robocode 机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9727485/