Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package man;
- import robocode.*;
- import java.util.Random;
- public class MyFirstRobot extends AdvancedRobot {
- private static boolean lockedOnEnemy = false;
- private static ScannedRobotEvent target;
- private static int moveDirection = 1;
- enum Direction{
- UP,
- DOWN,
- LEFT,
- RIGHT
- };
- public void run() {
- setAdjustGunForRobotTurn(true);
- setAdjustRadarForGunTurn(true);
- setTurnRadarRight(Double.POSITIVE_INFINITY);
- while (true) {
- //RobotHelper.MoveInRandomDirection(this);
- /*if(target.getRobotLife() <= 0) {
- lockedOnEnemy = false;
- }*/
- if(lockedOnEnemy) {
- setTurnRight(target.getBearing() + 90);
- setTurnGunRight(getHeading() - getGunHeading() + target.getBearing());
- fire(0.1);
- }
- if(Math.abs(getX() + 120) >= getBattleFieldWidth()) moveDirection *= -1;
- ahead(100 * moveDirection);
- /*turnGunRight(360);
- back(100);
- turnGunRight(360);*/
- }
- }
- public void onScannedRobot(ScannedRobotEvent enemy) {
- if( !lockedOnEnemy) {
- target = enemy;
- lockedOnEnemy = true;
- }else if(enemy.getName() == target.getName()) {
- target = enemy;
- }
- }
- }
- class RobotHelper{
- private static final int moveDistanceLimit = 150;
- private static final int moveRotationLimit = 360;
- public static void MoveInRandomDirection(MyFirstRobot robot){
- Random rand = new Random();
- int choice = rand.nextInt(4);
- double pixelsToMove = 1 + (moveDistanceLimit - 1) * rand.nextDouble();
- double rotation = 1 + (moveRotationLimit - 1) * rand.nextDouble();
- switch(choice) {
- case 0:
- robot.ahead(pixelsToMove);
- break;
- case 1:
- robot.back(pixelsToMove);
- break;
- case 2:
- robot.turnLeft(rotation);
- robot.ahead(pixelsToMove);
- break;
- case 3:
- robot.turnRight(rotation);
- robot.back(pixelsToMove);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement