-
-
Save robiXxu/cd3b2268cffc3c36afab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -*- mode: java; compile-command: "javac -classpath /usr/share/java/robocode.jar EightBot.java; robocode -battle /home/simen/.robocode/battles/eightbot-vs-crazy.battle" -*- */ | |
import robocode.*; | |
import java.util.*; | |
import java.awt.Color; | |
import static java.lang.Math.*; | |
public class EightBot extends AdvancedRobot { | |
final int TOLLERANCE = 10; | |
final int EIGHT_SIZE = 6; | |
final int GOTO_CENTER = 0; | |
final int DRAW_EIGHT = 1; | |
int state; | |
int turnDirection = 1; | |
void changeState(int state) { | |
setColors(Color.BLACK, Color.WHITE, Color.WHITE); | |
switch (state) { | |
case GOTO_CENTER: break; | |
case DRAW_EIGHT: setMaxVelocity(EIGHT_SIZE); break; | |
default: break; | |
}; | |
this.state = state; | |
} | |
double normalizeAngle(double angle) { | |
if (angle > 90) return 360 - (angle - 90); | |
else if (angle > 0) return 90 - angle; | |
else return 90 + abs(angle); | |
} | |
void goto_center() { | |
double x = getX(); | |
double y = getY(); | |
double cx = getBattleFieldWidth() / 2; | |
double cy = getBattleFieldHeight() / 2; | |
double dist = sqrt(pow(x - cx, 2) + pow(y - cy, 2)); | |
if (dist < TOLLERANCE) { | |
changeState(DRAW_EIGHT); | |
} else { | |
double angle = atan2(y - cy, x - cx) * 180 / PI; | |
double turn = (getHeading() - normalizeAngle(angle) + 180) % 360; | |
turnLeft(turn); | |
ahead(dist); | |
} | |
} | |
void draw_eight() { | |
setAhead(40000); | |
setTurnRight(360); | |
waitFor(new TurnCompleteCondition(this)); | |
setTurnLeft(360); | |
waitFor(new TurnCompleteCondition(this)); | |
} | |
public void run() { | |
// setAdjustRadarForGunTurn(true); | |
// setTurnRadarRightRadians(Double.POSITIVE_INFINITY); | |
setColors(Color.BLACK, Color.WHITE, Color.WHITE); | |
state = GOTO_CENTER; | |
while (true) { | |
switch (state) { | |
case GOTO_CENTER: goto_center(); break; | |
case DRAW_EIGHT: draw_eight(); break; | |
default: break; | |
}; | |
} | |
} | |
public void onHitWall(HitWallEvent e) { | |
// Never happens, lol! | |
} | |
public void onHitRobot(HitRobotEvent e) { | |
if (e.getBearing() >= 0) { | |
turnDirection = 1; | |
} else { | |
turnDirection = -1; | |
} | |
setTurnRight(e.getBearing()); | |
if (e.getEnergy() > 16) { | |
fire(3); | |
} else if (e.getEnergy() > 10) { | |
fire(2); | |
} else if (e.getEnergy() > 4) { | |
fire(1); | |
} else if (e.getEnergy() > 2) { | |
fire(.5); | |
} else if (e.getEnergy() > .4) { | |
fire(.1); | |
} | |
setAhead(40); | |
changeState(GOTO_CENTER); | |
} | |
public void onScannedRobot(ScannedRobotEvent e) { | |
double distance = e.getDistance(); | |
if (distance < 20) | |
fire(3); | |
else if (distance < 50) | |
fire(2); | |
else | |
fire(.1); | |
// found robot, time to RAM! | |
if (distance < 150) { | |
setAllColors(Color.RED); | |
if (e.getBearing() >= 0) | |
turnDirection = 1; | |
else | |
turnDirection = -1; | |
setTurnRight(e.getBearing()); | |
if (e.getDistance() > 50) | |
setAhead(e.getDistance() / 2); | |
else | |
setAhead(e.getDistance() + 5); | |
scan(); | |
} else { | |
changeState(GOTO_CENTER); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment