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.

SnakeBot.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package bot;
  2. public class SnakeBot 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. public SnakeBot(String[] args) {
  6. super(args);
  7. }
  8. @Override
  9. protected char nextMove(View view) throws Exception {
  10. String data = view.data;
  11. int width = view.width;
  12. // Position des Rovers finden
  13. int roverPosition = data.indexOf('R');
  14. // Überprüfen, ob eine Gesteinsprobe vorhanden ist
  15. boolean hasRock = data.charAt(roverPosition) == '@';
  16. // Wenn eine Gesteinsprobe vorhanden ist, sammeln
  17. if (hasRock) {
  18. return 'C'; // Beispielhaftes Zeichen für "Sammeln"
  19. }
  20. // Bewegungsbefehl für die nächste Runde generieren
  21. char moveCommand = getNextMove(data, width, roverPosition);
  22. return moveCommand;
  23. }
  24. // Methode zur Berechnung des nächsten Bewegungsbefehls
  25. private char getNextMove(String data, int width, int roverPosition) {
  26. char moveCommand;
  27. // Aktuelle Bewegungsrichtung des Roboters festlegen
  28. int moveDirection = currentDirection % 4; // Modulo 4, um innerhalb des Bereichs von 0 bis 3 zu bleiben
  29. // Bewegungsbefehl entsprechend der aktuellen Richtung generieren
  30. switch (moveDirection) {
  31. case 0: // Hoch
  32. moveCommand = 'U';
  33. break;
  34. case 1: // Rechts
  35. moveCommand = 'R';
  36. break;
  37. case 2: // Runter
  38. moveCommand = 'D';
  39. break;
  40. case 3: // Links
  41. moveCommand = 'L';
  42. break;
  43. default:
  44. moveCommand = ' '; // Standardbewegungsbefehl, falls etwas schief geht
  45. break;
  46. }
  47. // Überprüfen, ob der Rover am Rand des Spielfelds ist und die Richtung ändern muss, um im Spielfeld zu bleiben
  48. int nextPosition = getNextPosition(roverPosition, width, moveCommand);
  49. if (nextPosition < 0 || nextPosition >= data.length() || data.charAt(nextPosition) == '*') {
  50. currentDirection++;
  51. moveCommand = getNextMove(data, width, roverPosition); // Neue Richtung erneut berechnen
  52. }
  53. return moveCommand;
  54. }
  55. // Methode zur Berechnung der nächsten Position basierend auf der aktuellen Position und dem Bewegungsbefehl
  56. private int getNextPosition(int roverPosition, int width, char moveCommand) {
  57. switch (moveCommand) {
  58. case 'U': // Hoch
  59. return roverPosition - width;
  60. case 'R': // Rechts
  61. return roverPosition + 1;
  62. case 'D': // Runter
  63. return roverPosition + width;
  64. case 'L': // Links
  65. return roverPosition - 1;
  66. default:
  67. return -1; // Ungültige Position
  68. }
  69. }
  70. public static void main(String[] args) {
  71. new SnakeBot(args).run();
  72. }
  73. }