Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. package man;
  2. import robocode.*;
  3. import java.util.Random;
  4.  
  5.  
  6. public class MyFirstRobot extends AdvancedRobot {
  7.  
  8.  
  9. private static boolean lockedOnEnemy = false;
  10. private static ScannedRobotEvent target;
  11.  
  12. private static int moveDirection = 1;
  13.  
  14. enum Direction{
  15. UP,
  16. DOWN,
  17. LEFT,
  18. RIGHT
  19. };
  20.  
  21. public void run() {
  22. setAdjustGunForRobotTurn(true);
  23. setAdjustRadarForGunTurn(true);
  24. setTurnRadarRight(Double.POSITIVE_INFINITY);
  25.  
  26. while (true) {
  27.  
  28. //RobotHelper.MoveInRandomDirection(this);
  29. /*if(target.getRobotLife() <= 0) {
  30. lockedOnEnemy = false;
  31. }*/
  32.  
  33. if(lockedOnEnemy) {
  34. setTurnRight(target.getBearing() + 90);
  35. setTurnGunRight(getHeading() - getGunHeading() + target.getBearing());
  36. fire(0.1);
  37. }
  38.  
  39.  
  40. if(Math.abs(getX() + 120) >= getBattleFieldWidth()) moveDirection *= -1;
  41. ahead(100 * moveDirection);
  42. /*turnGunRight(360);
  43. back(100);
  44. turnGunRight(360);*/
  45.  
  46. }
  47. }
  48.  
  49. public void onScannedRobot(ScannedRobotEvent enemy) {
  50. if( !lockedOnEnemy) {
  51. target = enemy;
  52. lockedOnEnemy = true;
  53. }else if(enemy.getName() == target.getName()) {
  54. target = enemy;
  55. }
  56. }
  57. }
  58.  
  59. class RobotHelper{
  60.  
  61. private static final int moveDistanceLimit = 150;
  62. private static final int moveRotationLimit = 360;
  63.  
  64. public static void MoveInRandomDirection(MyFirstRobot robot){
  65. Random rand = new Random();
  66. int choice = rand.nextInt(4);
  67.  
  68. double pixelsToMove = 1 + (moveDistanceLimit - 1) * rand.nextDouble();
  69. double rotation = 1 + (moveRotationLimit - 1) * rand.nextDouble();
  70.  
  71. switch(choice) {
  72. case 0:
  73. robot.ahead(pixelsToMove);
  74. break;
  75. case 1:
  76. robot.back(pixelsToMove);
  77. break;
  78. case 2:
  79. robot.turnLeft(rotation);
  80. robot.ahead(pixelsToMove);
  81. break;
  82. case 3:
  83. robot.turnRight(rotation);
  84. robot.back(pixelsToMove);
  85. break;
  86. }
  87.  
  88.  
  89. }
  90.  
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement