„CollectBot.java“ hinzufügen

This commit is contained in:
Jasmin Zieroth 2024-02-04 22:08:33 +00:00
parent fe1f66715e
commit 62a296ef04

72
CollectBot.java Normal file
View File

@ -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();
}
}