From 62a296ef042ec9d1f3945240ea12670f621c5d37 Mon Sep 17 00:00:00 2001 From: Jasmin Zieroth Date: Sun, 4 Feb 2024 22:08:33 +0000 Subject: [PATCH] =?UTF-8?q?=E2=80=9ECollectBot.java=E2=80=9C=20hinzuf?= =?UTF-8?q?=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CollectBot.java | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 CollectBot.java diff --git a/CollectBot.java b/CollectBot.java new file mode 100644 index 0000000..1b15f1b --- /dev/null +++ b/CollectBot.java @@ -0,0 +1,72 @@ +package bot; + +public class CollectBot extends Bot { + + public CollectBot(String[] args) { + super(args); + } + + @Override + protected char nextMove(View view) throws Exception { + String data = view.data; + int width = view.width; + + // Position des Rovers finden + int roverPosition = data.indexOf('R'); + + // Überprüfen, ob eine Gesteinsprobe vorhanden ist + boolean hasRock = data.charAt(roverPosition) == '@'; + + // Bewegungsbefehl, um zur nächsten Gesteinsprobe zu gelangen + if (hasRock) { + return 'C'; // Beispielhaftes Zeichen für "Sammeln" + } + + // Richtung für die nächste Bewegung berechnen, um das Spielfeld systematisch zu erkunden + int nextPosition = getNextPosition(roverPosition, width, data); + + // Bewegungsbefehl generieren, um zur nächsten Position zu gelangen + char moveCommand = generateMoveCommand(roverPosition, nextPosition, width); + + return moveCommand; + } + + // Hilfsmethode, um die nächste Position zu berechnen, um das Spielfeld systematisch zu erkunden + private int getNextPosition(int roverPosition, int width, String data) { + int nextPosition; + + // Wenn der Rover sich am unteren Rand des Spielfelds befindet, bewege ihn eine Position nach rechts + if (roverPosition / width % 2 == 1) { + nextPosition = roverPosition + 1; + } else { + // Andernfalls bewege den Rover eine Position nach links + nextPosition = roverPosition - 1; + } + + // Überprüfen, ob die nächste Position gültig ist, sonst bewege den Rover eine Reihe nach unten + if (nextPosition >= data.length() || nextPosition < 0 || data.charAt(nextPosition) == '*') { + nextPosition = roverPosition + width; + } + + return nextPosition; + } + + // Hilfsmethode, um den Bewegungsbefehl basierend auf der aktuellen und nächsten Position zu generieren + private char generateMoveCommand(int roverPosition, int nextPosition, int width) { + if (nextPosition == roverPosition + 1) { + return 'R'; // Bewegung nach rechts + } else if (nextPosition == roverPosition - 1) { + return 'L'; // Bewegung nach links + } else if (nextPosition == roverPosition + width) { + return 'D'; // Bewegung nach unten + } else { + return 'U'; // Bewegung nach oben + } + } + + public static void main(String[] args) { + new CollectBot(args).run(); + } + } + +