-
-
Save nywton/8a6a2d372723193984c098d1a5cba9fd to your computer and use it in GitHub Desktop.
RoboCode Robot Wall Variation
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
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 | |
{ | |
double size; | |
public void run() { | |
boolean shouldTurn = false; | |
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) { | |
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(); | |
} | |
public void onHitByBullet(HitByBulletEvent e) { | |
ahead(size * 3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment