Created
October 23, 2010 15:11
-
-
Save alexandreaquiles/642313 to your computer and use it in GitHub Desktop.
Rambo - a robot by @alex_aquiles
This file contains 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
package Agil; | |
import robocode.*; | |
import java.awt.Color; | |
import static robocode.util.Utils.getRandom; | |
/** | |
* Rambo - a robot by @alex_aquiles | |
*/ | |
public class Rambo extends Robot | |
{ | |
private boolean rotate = false; | |
public void run() { | |
setColors(Color.black,Color.red,Color.red); | |
while(true) { | |
if(rotate) { | |
turnGunRight(360); | |
rotate = false; | |
} else { | |
turnGunRight(45); | |
rotate = true; | |
} | |
} | |
} | |
public void onScannedRobot(ScannedRobotEvent e) { | |
smartFire(e.getDistance()); | |
ahead(distanceToMoveTowardEnemy(e)); | |
} | |
private double distanceToMoveTowardEnemy(ScannedRobotEvent e){ | |
double distanceToMove = Math.abs(e.getDistance() - getRandom().nextInt(20)); | |
distanceToMove = Math.max(20, distanceToMove); | |
if (!(e.getBearing() > -90 && e.getBearing() < 90)) { | |
distanceToMove *= -1; | |
} | |
return distanceToMove; | |
} | |
private void smartFire(double robotDistance) { | |
if (robotDistance > 200 || getEnergy() < 15) { | |
fire(1); | |
} else if (robotDistance > 50) { | |
fire(1); | |
fire(2); | |
} else { | |
fire(3); | |
fire(3); | |
fire(3); | |
} | |
} | |
public void onHitByBullet(HitByBulletEvent e) { | |
turnLeft(90 - e.getBearing()); | |
runAway(e); | |
} | |
private void runAway(HitByBulletEvent e) { | |
if (e.getBearing() > -90 && e.getBearing() < 90) { | |
back(20 + getRandom().nextInt(10)); | |
} | |
else { | |
ahead(20 + getRandom().nextInt(10)); | |
} | |
} | |
public void onHitWall(HitWallEvent e) { | |
turnLeft(180 - e.getBearing()); | |
ahead(20); | |
} | |
public void onHitRobot(HitRobotEvent e) { | |
turnLeft(e.getBearing()); | |
ahead(20); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment