Created
November 13, 2018 00:40
-
-
Save gabrielfern/222a4d181329db694058a3fbe8a1adb0 to your computer and use it in GitHub Desktop.
Robo que pega o Walls
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
import java.awt.Color; | |
import robocode.AdvancedRobot; | |
import robocode.BulletHitEvent; | |
import robocode.HitRobotEvent; | |
import robocode.ScannedRobotEvent; | |
public class RLGDT_Walls extends AdvancedRobot { | |
final int NE = 0, SE = 1, SO = 2, NO = 3; | |
double eixoDeDescida; | |
final double eixoDeBatalha = 30; | |
boolean emBatalha = false; | |
@Override | |
public void run() { | |
setBodyColor(Color.DARK_GRAY); | |
setGunColor(Color.RED); | |
setRadarColor(Color.DARK_GRAY); | |
setBulletColor(Color.WHITE); | |
setScanColor(Color.BLACK); | |
eixoDeDescida = getBattleFieldWidth() - 100; | |
posicionar(); | |
boolean indoParaTras = true; | |
while (true) { | |
if (getY() != eixoDeBatalha) | |
posicionar(); | |
if (getDistanceRemaining() == 0 && indoParaTras && !emBatalha) { | |
setBack(eixoDeDescida - (getBattleFieldWidth() - getX())); | |
indoParaTras = false; | |
} | |
if (getDistanceRemaining() == 0 && !indoParaTras && !emBatalha) { | |
setAhead(eixoDeDescida - getX()); | |
indoParaTras = true; | |
} | |
scan(); | |
} | |
} | |
int calculaMelhorAcao(double anguloInimigo) { | |
if (anguloInimigo >= 0 && anguloInimigo < 90) | |
return SO; | |
else if (anguloInimigo >= 90 && anguloInimigo < 180) | |
return NO; | |
else if (anguloInimigo >= 180 && anguloInimigo < 270) | |
return NE; | |
else | |
return SE; | |
} | |
@Override | |
public void onHitRobot(HitRobotEvent event) { | |
double anguloInimigo = getHeading() + event.getBearing(); | |
if (anguloInimigo > 360) | |
anguloInimigo = 0 + (anguloInimigo - 360); | |
else if (anguloInimigo < 0) | |
anguloInimigo = 360 + anguloInimigo; | |
switch (calculaMelhorAcao(anguloInimigo)) { | |
case NO: | |
northwestEvade(); | |
break; | |
case NE: | |
northestEvade(); | |
break; | |
case SO: | |
southwestEvade(); | |
break; | |
case SE: | |
southestEvade(); | |
break; | |
} | |
posicionar(); | |
} | |
@Override | |
public void onScannedRobot(ScannedRobotEvent ev) { | |
if (getY() == eixoDeBatalha) | |
setFire(3); | |
} | |
@Override | |
public void onBulletHit(BulletHitEvent event) { | |
emBatalha = true; | |
setAhead(0); | |
} | |
void posicionar() { | |
turnRight(90 - getHeading()); | |
ahead(eixoDeDescida - getX()); | |
turnRight(90); | |
ahead(getY() - eixoDeBatalha); | |
turnLeft(90); | |
} | |
void northestEvade() { | |
if (getHeading() < 180) | |
turnLeft(getHeading()); | |
else | |
turnRight(360 - getHeading()); | |
setAhead(100); | |
setTurnRight(90); | |
} | |
void northwestEvade() { | |
if (getHeading() < 180) | |
turnLeft(getHeading()); | |
else | |
turnRight(360 - getHeading()); | |
setAhead(100); | |
setTurnLeft(90); | |
} | |
void southestEvade() { | |
if (getHeading() < 180) | |
turnRight(180 - getHeading()); | |
else | |
turnLeft(getHeading() - 180); | |
setAhead(100); | |
setTurnLeft(90); | |
} | |
void southwestEvade() { | |
if (getHeading() < 180) | |
turnRight(180 - getHeading()); | |
else | |
turnLeft(getHeading() - 180); | |
setAhead(100); | |
setTurnRight(90); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment