Aylin, Isabella, Edasu, Jasmin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RumbleBot.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package bot;
  2. public class RumbleBot extends Bot {
  3. private int currentDirection = 1; // Variable, um die aktuelle Bewegungsrichtung des Rovers zu halten
  4. // 0 - oben, 1 - rechts, 2 - unten, 3 - links
  5. private boolean shouldFire; // wichtig um den Feuerstatus des Bots zu verfolgen
  6. // Konstruktor für RumbleBot
  7. public RumbleBot(String[] args) {
  8. super(args); //Konstruktor der Elternklasse wird aufgerufen
  9. this.shouldFire = false;
  10. }
  11. // Logik für den RumbleBot, der in Fahrtrichtung schießt
  12. @Override
  13. protected char nextMove(View view) throws Exception {
  14. // Wenn andere Bots in Sicht sind, wird abgefeuert
  15. if (isOtherBotInSight(view.data)) {
  16. shouldFire = true;
  17. }
  18. if (shouldFire) {
  19. shouldFire = false;
  20. return 'f';
  21. }
  22. // Bewegungsbefehl für die nächste Runde generieren
  23. char moveCommand = getNextMove(view.data, view.width);
  24. // Überprüfen, ob der Rover am Rand des Spielfelds ist und die Richtung ändern muss, um im Spielfeld zu bleiben
  25. int nextPosition = getNextPosition(view.data.indexOf('A'), view.width, moveCommand);
  26. if (nextPosition < 0 || nextPosition >= view.data.length() || view.data.charAt(nextPosition) == '#') {
  27. currentDirection++;
  28. moveCommand = getNextMove(view.data, view.width); // Neue Richtung erneut berechnen
  29. }
  30. return moveCommand;
  31. }
  32. // Methode zur Berechnung des nächsten Bewegungsbefehls
  33. private char getNextMove(String data, int width) {
  34. char moveCommand;
  35. // Aktuelle Bewegungsrichtung des Roboters festlegen
  36. int moveDirection = currentDirection % 4; // Modulo 4, um innerhalb des Bereichs von 0 bis 3 zu bleiben
  37. // Bewegungsbefehl entsprechend der aktuellen Richtung generieren
  38. switch (moveDirection) {
  39. case 0: // Hoch
  40. moveCommand = 'w';
  41. break;
  42. case 1: // Rechts
  43. moveCommand = 'd';
  44. break;
  45. case 2: // Runter
  46. moveCommand = 's';
  47. break;
  48. case 3: // Links
  49. moveCommand = 'a';
  50. break;
  51. default:
  52. moveCommand = ' ';
  53. break;
  54. }
  55. return moveCommand;
  56. }
  57. // Methode zur Berechnung der nächsten Position basierend auf der aktuellen Position und dem Bewegungsbefehl
  58. private int getNextPosition(int roverPosition, int width, char moveCommand) {
  59. switch (moveCommand) {
  60. case 'w': // Hoch
  61. return roverPosition - width;
  62. case 'd': // Rechts
  63. return roverPosition + 1;
  64. case 's': // Runter
  65. return roverPosition + width;
  66. case 'a': // Links
  67. return roverPosition - 1;
  68. default:
  69. return -1; // Ungültige Position
  70. }
  71. }
  72. // Methode zur Überprüfung, ob andere Bots in Sicht sind
  73. private boolean isOtherBotInSight(String data) {
  74. // Überprüft, ob andere Bots in Sicht sind (z.B., "^", "<", ">", "v" in der Karte)
  75. return data.contains("^") || data.contains("<") || data.contains(">") || data.contains("v");
  76. }
  77. // main Methode um den Bot zu starten
  78. public static void main(String[] args) {
  79. new RumbleBot(args).run();
  80. }
  81. }