# Subotai

Robo Home | Changes | Preferences | AllPages

This bot has my first real attempt at innovation: a random-movement engine that will take you southwest in quadrant 1, southeast in quadrant 2, northwest in quadrant 3, and northwest in quadrant 4. The problem is it doesn't work. The robot will run for a few seconds, then the _whole battle_ will freeze for a few seconds, then Subotai will roll to a wall and stop. Here's the code below. Can you see any problems? Do note the places where I've "commented" the code so those parts are irrelevant - they will either be taken out or implemented later.

Incidentally, what's the difference between a long and a double?

```package pfvicm;
import robocode.*;
import java.awt.Color;
import java.awt.geom.*;

/*
* Subotai - PFVICM 6.7.1
*/
{
Point2D NextPosition, myloc;
double Fieldx, Fieldy;
static double eenergy;
double x, y, v, b, H, q, my, mx, rq, vq;
static int dir=1;
public void run() {
setColors(Color.cyan,Color.orange,Color.yellow);
v=getVelocity();
x=getX();
y=getY();
my=getBattleFieldHeight()/2;
mx=getBattleFieldWidth()/2;
myloc=new Point2D.Double(x,y);
Fieldx=getBattleFieldWidth();
Fieldy=getBattleFieldHeight();
execute();
while(true) {
/*NextPosition=new Point2D.Double(Math.random()*(Fieldx-36)+18,Math.random()*(Fieldy-36)+18);
goTo(NextPosition);
execute();
while (getDistanceRemaining()>0 || getTurnRemaining()>0) {
execute();
}
checkStop();*/
}
}
/*private void goTo(Point2D destination) {
Point2D location = new Point2D.Double(getX(), getY());
double distance = location.distance(destination);
double angle = normalRelativeAngle(absoluteBearing(location, destination) - getHeading());
if (Math.abs(angle) > 90) {
distance *= -1;
if (angle > 0) {
angle -= 180;
}
else {
angle += 180;
}
}
setTurnRight(angle);
}
private double absoluteBearing(Point2D source, Point2D target) {
return Math.toDegrees(Math.atan2(target.getX() - source.getX(), target.getY() - source.getY()));
}*/
private double normalRelativeAngle(double angle) {
return Math.toDegrees(Math.atan2(Math.sin(angle), Math.cos(angle)));
}
/*public void checkStop() {
if(v==0) {
goTo(NextPosition);
execute();
}
}*/
public void onScannedRobot(ScannedRobotEvent e) {
//setInterruptible(true);
b = eenergy - e.getEnergy();
eenergy = e.getEnergy();
/*if (0<=H && H<90) {
q=1; }
else { if (90<=H && H<180) {
q=4; }
else { if (180<=H && H<270) {
q=3; }
else { if (270<=H && H<360) {
q=2; }
}}}*/
if (x<mx) {
if (y<my) {
vq=0; }
else { vq=270; }
}
else {
if (y<my) {
vq=180; }
else { vq=90; }
}
if (b > 0 && b <= 3) {
if (Math.random()<.5) { dir=-dir; }
setTurnLeft(H-(((Math.random()*90)+vq)/**dir*/));
execute();
}
if (getEnergy()<0.1) {
stop();
turnGunLeft(e.getBearing());
fire(0.1);
resume();
}
else {
double absoluteBearing = getHeading() + e.getBearing();
double bearingFromGun = normalRelativeAngle(absoluteBearing - getGunHeading());
if (Math.abs(bearingFromGun) <= 3) {
turnGunRight(bearingFromGun);
if (getGunHeat() == 0)
setFire(Math.min(3 - Math.abs(bearingFromGun),getEnergy() - .1));
}
else {
turnGunRight(bearingFromGun);
}
}
setEventPriority("ScannedRobotEvent",10);
execute();
}
public double getRange(double x1,double y1, double x2,double y2) {
double xp = x2-x1;
double yp = y2-y1;
double h = Math.sqrt( xp*xp + yp*yp );
return h;
}
public double sec(double angle) {
double c = 1/Math.cos(angle);
return c;
}
/*public void onHitByBullet(HitByBulletEvent e) {
//goTo(NextPosition);
//stop();
if (x<getBattleFieldWidth()/2)  {
dir=-dir;
}
else { if (y<getBattleFieldHeight()/2)  {
dir=-dir;
}
}
setTurnRight(e.getBearing()-90);
setBack(Math.random()*(Math.min(Math.min((getBattleFieldHeight()-getY()),(getBattleFieldWidth()-getX())),(Math.min(getX(),getY()))))*dir*1.5);
execute();
//resume();
}*/
public void onHitRobot(HitRobotEvent e) {
//if(e.isMyFault()) {
setEventPriority("ScannedRobotEvent",25);
fire(3);
//else {
//setBack(Math.random()*(Math.min(Math.min((getBattleFieldHeight()-getY()),(getBattleFieldWidth()-getX())),(Math.min(getX(),getY())))));
//}
}
}
```

From your description of the problem, it seems that your bot stops due to an exception. Is there any output in the debug-window? You can open the debug-window by clicking on your bot's name in the upper right corner during a battle. -- GrubbmGait

A 'long' is a larger version of 'int' (just a higher MAX and lower MIN value), and a 'double' is a "double precision" floating point decimal number. (As an aside, 'long' is to 'int' as 'double' is to 'float'.) -- Voidious

You've got an empty while(true) loop in your run method. Your bot will only call execute() once then go into an infinite loop resulting in the game disabling it. --wcsv

Maybe what you meant was something like this?

```public void run()
{
setColors(Color.cyan,Color.orange,Color.yellow);
while(true)
{
v=getVelocity();
x=getX();
y=getY();
my=getBattleFieldHeight()/2;
mx=getBattleFieldWidth()/2;
myloc=new Point2D.Double(x,y);
Fieldx=getBattleFieldWidth();
Fieldy=getBattleFieldHeight();
execute();
}
}
```
--wcsv

Thanks a LOT! I'm WORKING now!!! :D :D --Pfvicm

Robo Home | Changes | Preferences | AllPages