„SnakeBot.java“ ändern

This commit is contained in:
Jasmin Zieroth 2024-02-06 21:38:00 +00:00
parent 64f5e67470
commit e917165fa8

View File

@ -1,89 +1,85 @@
package bot; package praktikum05;
public class SnakeBot extends Bot { import java.util.ArrayList;
import java.util.List;
private int currentDirection = 1; // Variable, um die aktuelle Bewegungsrichtung des Rovers zu halten public class SnakeBotMimi extends Bot{
// 0 - oben, 1 - rechts, 2 - unten, 3 - links boolean lastMoveWasTurn = false; // Merker, ob der letzte Zug eine Drehung war
public SnakeBot(String[] args) { public static void main(String[] args) {
Bot snakeBot = new SnakeBotMimi(args);
snakeBot.run();
}
protected SnakeBotMimi(String[] args) {
super(args); super(args);
} }
@Override @Override
protected char nextMove(View view) throws Exception { protected char nextMove(View view) throws Exception {
String data = view.data; char nextMove = '^';
int width = view.width; boolean stoneDetected = view.data.contains("@");
System.out.println(view.data + " xxx: ");
// Position des Rovers finden // Das ist nur fuer debug
int roverPosition = data.indexOf('R'); // List<Integer> positions = positionsOfRock(view.data);
// for (Integer i : positions)
// System.out.println("Position: " + i);
// Überprüfen, ob eine Gesteinsprobe vorhanden ist if (lastMoveWasTurn) {
boolean hasRock = data.charAt(roverPosition) == '@'; lastMoveWasTurn = false; // Merker fuer die letzte Drehung zuruecksetzen
nextMove = '^';
// Wenn eine Gesteinsprobe vorhanden ist, sammeln
if (hasRock) {
return 'C'; // Beispielhaftes Zeichen für "Sammeln"
} }
else {
// Bewegungsbefehl für die nächste Runde generieren if (!stoneDetected)
char moveCommand = getNextMove(data, width, roverPosition); nextMove = '^';
else {
return moveCommand; nextMove = calculateNextMove(view);
}
}
return nextMove;
} }
// Methode zur Berechnung des nächsten Bewegungsbefehls /**
private char getNextMove(String data, int width, int roverPosition) { * Berechne den naechsten Zug auf Basis der gefundenen Steine im Scan
char moveCommand; * @param view
* @return
*/
private char calculateNextMove(View view){
List<Integer> positions = positionsOfRock(view.data);
int firstPostion = positions.get(0);
// 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 // Wenn das @Zeichen an Position kleiner 11, dann ist es oberhalb von uns
switch (moveDirection) { if (firstPostion < 11 )
case 0: // Hoch return '^';
moveCommand = 'U';
break; // Ist die Position vom @Zeichen zwischn 11 und 12, dann links von uns
case 1: // Rechts // wir muessen uns nach links drehen
moveCommand = 'R'; else if (firstPostion >= 11 && firstPostion < 13) {
break; lastMoveWasTurn = true; // Merker auf true, da wir uns gedreht haben
case 2: // Runter return '<';
moveCommand = 'D'; }
break; // Bei allen anderen Positionen drehen wir uns nach rechts drehen
case 3: // Links else {
moveCommand = 'L'; lastMoveWasTurn = true; // Merker auf true, da wir uns gedreht haben
break; return '>';
default: }
moveCommand = ' '; // Standardbewegungsbefehl, falls etwas schief geht
break;
} }
// Überprüfen, ob der Rover am Rand des Spielfelds ist und die Richtung ändern muss, um im Spielfeld zu bleiben /**
int nextPosition = getNextPosition(roverPosition, width, moveCommand); * Ermittle alle Positionen der Steine innerhalb des flachen Scans
if (nextPosition < 0 || nextPosition >= data.length() || data.charAt(nextPosition) == '*') { * @param row das Ergebnis von View.data (der Scan als String)
currentDirection++; * @return eine Liste mit den Positionen
moveCommand = getNextMove(data, width, roverPosition); // Neue Richtung erneut berechnen */
private List<Integer> positionsOfRock (String row){
int index = row.indexOf('@');
ArrayList<Integer> positions = new ArrayList<>();
while (index >= 0) {
positions.add(index);
index = row.indexOf('@', index + 1);
}
return positions;
} }
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 'U': // Hoch
return roverPosition - width;
case 'R': // Rechts
return roverPosition + 1;
case 'D': // Runter
return roverPosition + width;
case 'L': // Links
return roverPosition - 1;
default:
return -1; // Ungültige Position
}
}
public static void main(String[] args) {
new SnakeBot(args).run();
}
} }