-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7efd57b
commit d4292c5
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
your_code_here/src/nl/ordina/nerdingout/round_3/Huntor.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package nl.ordina.nerdingout.round_3; | ||
|
||
import robocode.AdvancedRobot; | ||
import robocode.HitWallEvent; | ||
import robocode.ScannedRobotEvent; | ||
import robocode.util.Utils; | ||
|
||
import java.awt.*; | ||
|
||
public class Huntor extends AdvancedRobot { | ||
private int factor = 1; | ||
|
||
@Override | ||
public void run() { | ||
setColors(Color.CYAN, Color.WHITE, Color.YELLOW); | ||
setAdjustRadarForGunTurn(true); | ||
setAdjustRadarForRobotTurn(true); | ||
|
||
while(true) { | ||
turnRadarLeft(360); | ||
} | ||
} | ||
|
||
@Override | ||
public void onScannedRobot(ScannedRobotEvent event) { | ||
theHuntIsOn(event); | ||
} | ||
|
||
public void theHuntIsOn(ScannedRobotEvent event) { | ||
|
||
int distanceLimit = 250; | ||
if (event.getDistance() > distanceLimit) { | ||
setTurnRight(event.getBearing()); | ||
setAhead(Math.max(event.getDistance(), distanceLimit)); | ||
} else { | ||
setTurnRight(event.getBearing() - 90); | ||
setAhead(100 * factor); | ||
setTurnGunTowardsOpponent(event.getBearing()); | ||
setFire(Math.min(1.5, getEnergy())); | ||
} | ||
|
||
setTurnRadarRight(getHeading() - getRadarHeading() + event.getBearing()); | ||
} | ||
|
||
private void setTurnGunTowardsOpponent(double opponentsBearing) { | ||
double degreesToTurnGun = getDegreesBetweenGunAndOpponentBearing(getHeading(), getGunHeading(), opponentsBearing); | ||
|
||
if (degreesToTurnGun < 0) { | ||
setTurnGunLeft(Math.abs(degreesToTurnGun)); | ||
} else { | ||
setTurnGunRight(degreesToTurnGun); | ||
} | ||
} | ||
|
||
double getDegreesBetweenGunAndOpponentBearing(double ownHeading, double ownGunHeading, double opponentsBearing) { | ||
double opponentsHeading = fromBearingToDegrees(ownHeading, opponentsBearing); | ||
|
||
double degreesInBetween = opponentsHeading - ownGunHeading; | ||
if (degreesInBetween < -180) { | ||
degreesInBetween = 360 + degreesInBetween; | ||
} else if (degreesInBetween > 180) { | ||
degreesInBetween = 360 - degreesInBetween; | ||
} | ||
|
||
return degreesInBetween; | ||
} | ||
|
||
double fromBearingToDegrees(double ownHeading, double bearing) { | ||
double heading = ownHeading + bearing; | ||
|
||
if (heading < 0) { | ||
heading = heading + 360; | ||
} else if (heading > 360) { | ||
heading = heading - 360; | ||
} | ||
|
||
return heading; | ||
} | ||
|
||
@Override | ||
public void onHitWall(HitWallEvent event) { | ||
factor = -factor; | ||
} | ||
} |