96 lines
3.3 KiB
Java
96 lines
3.3 KiB
Java
|
|
public class RumbleBot extends Bot {
|
|
|
|
private int currentDirection = 1; // Variable, um die aktuelle Bewegungsrichtung des Rovers zu halten
|
|
// 0 - oben, 1 - rechts, 2 - unten, 3 - links
|
|
|
|
private boolean shouldFire; // wichtig um den Feuerstatus des Bots zu verfolgen
|
|
|
|
// Konstruktor für RumbleBot
|
|
public RumbleBot(String[] args) {
|
|
super(args); //Konstruktor der Elternklasse wird aufgerufen
|
|
this.shouldFire = false;
|
|
}
|
|
|
|
// Logik für den RumbleBot, der in Fahrtrichtung schießt
|
|
@Override
|
|
protected char nextMove(View view) throws Exception {
|
|
|
|
// Wenn andere Bots in Sicht sind, wird abgefeuert
|
|
if (isOtherBotInSight(view.data)) {
|
|
shouldFire = true;
|
|
}
|
|
|
|
if (shouldFire) {
|
|
shouldFire = false;
|
|
return 'f';
|
|
}
|
|
// Bewegungsbefehl für die nächste Runde generieren
|
|
char moveCommand = getNextMove(view.data, view.width);
|
|
|
|
// Überprüfen, ob der Rover am Rand des Spielfelds ist und die Richtung ändern muss, um im Spielfeld zu bleiben
|
|
int nextPosition = getNextPosition(view.data.indexOf('A'), view.width, moveCommand);
|
|
if (nextPosition < 0 || nextPosition >= view.data.length() || view.data.charAt(nextPosition) == '#') {
|
|
currentDirection++;
|
|
moveCommand = getNextMove(view.data, view.width); // Neue Richtung erneut berechnen
|
|
}
|
|
|
|
return moveCommand;
|
|
}
|
|
|
|
// Methode zur Berechnung des nächsten Bewegungsbefehls
|
|
private char getNextMove(String data, int width) {
|
|
char moveCommand;
|
|
|
|
// Aktuelle Bewegungsrichtung des Roboters festlegen
|
|
int moveDirection = currentDirection % 4; // Modulo 4, um innerhalb des Bereichs von 0 bis 3 zu bleiben
|
|
|
|
// Bewegungsbefehl entsprechend der aktuellen Richtung generieren
|
|
switch (moveDirection) {
|
|
case 0: // Hoch
|
|
moveCommand = 'w';
|
|
break;
|
|
case 1: // Rechts
|
|
moveCommand = 'd';
|
|
break;
|
|
case 2: // Runter
|
|
moveCommand = 's';
|
|
break;
|
|
case 3: // Links
|
|
moveCommand = 'a';
|
|
break;
|
|
default:
|
|
moveCommand = ' ';
|
|
break;
|
|
}
|
|
|
|
return moveCommand;
|
|
}
|
|
|
|
// Methode zur Berechnung der nächsten Position basierend auf der aktuellen Position und dem Bewegungsbefehl
|
|
private int getNextPosition(int roverPosition, int width, char moveCommand) {
|
|
switch (moveCommand) {
|
|
case 'w': // Hoch
|
|
return roverPosition - width;
|
|
case 'd': // Rechts
|
|
return roverPosition + 1;
|
|
case 's': // Runter
|
|
return roverPosition + width;
|
|
case 'a': // Links
|
|
return roverPosition - 1;
|
|
default:
|
|
return -1; // Ungültige Position
|
|
}
|
|
}
|
|
|
|
// Methode zur Überprüfung, ob andere Bots in Sicht sind
|
|
private boolean isOtherBotInSight(String data) {
|
|
// Überprüft, ob andere Bots in Sicht sind (z.B., "^", "<", ">", "v" in der Karte)
|
|
return data.contains("^") || data.contains("<") || data.contains(">") || data.contains("v");
|
|
}
|
|
|
|
// main Methode um den Bot zu starten
|
|
public static void main(String[] args) {
|
|
new RumbleBot(args).run();
|
|
}
|
|
} |