From b67921efcc7d39e052d9a2a8360cbc5ccdc9de14 Mon Sep 17 00:00:00 2001 From: Aylin Goekce Date: Sat, 3 Feb 2024 11:21:54 +0000 Subject: [PATCH] =?UTF-8?q?Dateien=20hochladen=20nach=20=E2=80=9E=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EscapeBot.java | 95 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 7 deletions(-) diff --git a/EscapeBot.java b/EscapeBot.java index 0cc21a4..cd368da 100644 --- a/EscapeBot.java +++ b/EscapeBot.java @@ -3,17 +3,98 @@ package bot; import java.util.Scanner; public class EscapeBot extends Bot { - public EscapeBot (String[] args){ + private int currentX = 0; + private int currentY = 0; + private int spiralSize = 1; + private int stepsInCurrentDirection = 0; + private char currentDirection = 'd'; // Start with moving to the right + + public EscapeBot(String[] args) { super(args); } - public static void main (String[] args){ - EscapeBot escapebot = new EscapeBot(args); - escapebot.run(); + public static void main(String[] args) { + EscapeBot escapeBot = new EscapeBot(args); + escapeBot.run(); } - @Override - protected char nextMove(View view) throws Exception { //Strategie: In einer Spirale laufen bis es die Rakete findet - //Suche nach der Rakete im Spielfeld + @Override + protected char nextMove(View view) throws Exception { + char[][] map = parseMap(view.data, view.width); + + char nextMove = 'w'; // Default move is to go forward + + if (map[currentY][currentX] == 'o') { + System.out.println("Rakete gefunden!"); + nextMove = 'q'; // Beende die Verbindung, wenn die Rakete gefunden wurde + } else { + // Bewegungslogik im Spiralmuster + switch (currentDirection) { + case 'd': // Bewege nach rechts + currentX++; + break; + case 's': // Bewege nach unten + currentY++; + break; + case 'a': // Bewege nach links + currentX--; + break; + case 'w': // Bewege nach oben + currentY--; + break; + } + stepsInCurrentDirection++; + + // Wenn der Rover die maximale Anzahl von Schritten in dieser Richtung erreicht hat + if (stepsInCurrentDirection == spiralSize) { + // Ändere die Richtung nach rechts + switch (currentDirection) { + case 'd': + currentDirection = 's'; + break; + case 's': + currentDirection = 'a'; + break; + case 'a': + currentDirection = 'w'; + break; + case 'w': + currentDirection = 'd'; + break; + } + + // Setze die Anzahl der Schritte in dieser Richtung zurück und erhöhe die Spiralgöße + stepsInCurrentDirection = 0; + if (currentDirection == 'd' || currentDirection == 'a') { + spiralSize++; + } + } + + // Überprüfe, ob der nächste Zug innerhalb der Karte liegt + if (currentX < 0 || currentX >= view.width || currentY < 0 || currentY >= view.width) { + // Der nächste Zug liegt außerhalb der Karte, also drehe den Rover um + nextMove = 'a'; + } else { + // Der nächste Zug liegt innerhalb der Karte, bewege den Rover dorthin + nextMove = currentDirection; + } + } + + return nextMove; + } + + // Hilfsmethode zum Parsen des Kartenscans in ein char-Array + private char[][] parseMap(String data, int width) { + char[][] map = new char[width][width]; + String[] lines = data.split("\n"); + for (int i = 0; i < width; i++) { + String line = lines[i]; + for (int j = 0; j < width; j++) { + map[i][j] = line.charAt(j); + } + } + return map; } } + +