Skip to content

Instantly share code, notes, and snippets.

@djalmajr
Created September 14, 2017 20:32
Show Gist options
  • Save djalmajr/c19568a69981bb13a687c04fde704cc8 to your computer and use it in GitHub Desktop.
Save djalmajr/c19568a69981bb13a687c04fde704cc8 to your computer and use it in GitHub Desktop.
RoboCode Robot Wall Variation
package ifal;
import robocode.*;
import java.awt.Color;
import static robocode.util.Utils.normalRelativeAngleDegrees;
// API help : http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html
/**
* RedRibbon - Robô feito por Djalma e Nywton
*/
public class RedRibbon extends Robot
{
public void run() {
boolean shouldTurn = false;
double size = getWidth();
// Set colors
setBodyColor(Color.black);
setGunColor(Color.black);
setRadarColor(Color.orange);
setBulletColor(Color.cyan);
setScanColor(Color.cyan);
double moveAmount = Math.max(
getBattleFieldWidth(),
getBattleFieldHeight()
);
// turnLeft to face a wall.
turnLeft(getHeading() % 90);
ahead(moveAmount);
turnGunRight(90);
turnRight(90);
while (true) {
ahead(size * 1.5);
turnGunRight(90);
turnGunLeft(180);
turnGunRight(90);
System.out.println();
System.out.println("size:" + size);
System.out.println("getX:" + getX());
System.out.println("getY:" + getY());
System.out.println("getX+size:" + getX()+size);
System.out.println("getY+size:" + getY()+size);
// Lado direito
if (getX() + size >= getBattleFieldWidth()) {
System.out.println("rightSize");
shouldTurn = (
getY() <= size ||
getY() + size >= getBattleFieldHeight()
);
}
// Lado esquerdo
if (getX() <= size) {
System.out.println("leftSize");
shouldTurn = (
size <= getY() ||
getY() + size >= getBattleFieldHeight()
);
}
if (shouldTurn) {
System.out.println("turn");
turnRight(90);
}
}
}
public void onHitRobot(HitRobotEvent e) {
turnGunRight(normalRelativeAngleDegrees(
e.getBearing() + getHeading() - getGunHeading()
));
fire(5);
}
public void onScannedRobot(ScannedRobotEvent e) {
if (getEnergy() > 50) {
int power = e.getDistance() < 30 ? 10 : 4;
fire(power);
} else {
fire(1);
}
scan();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment