Browse Source

„SnakeBot.java“ ändern

master
Jasmin Zieroth 3 months ago
parent
commit
e917165fa8
1 changed files with 70 additions and 74 deletions
  1. 70
    74
      SnakeBot.java

+ 70
- 74
SnakeBot.java 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
// 0 - oben, 1 - rechts, 2 - unten, 3 - links
public class SnakeBotMimi extends Bot{
boolean lastMoveWasTurn = false; // Merker, ob der letzte Zug eine Drehung war

public SnakeBot(String[] args) {
super(args);
public static void main(String[] args) {
Bot snakeBot = new SnakeBotMimi(args);
snakeBot.run();
}

@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) == '@';

// Wenn eine Gesteinsprobe vorhanden ist, sammeln
if (hasRock) {
return 'C'; // Beispielhaftes Zeichen für "Sammeln"
}

// Bewegungsbefehl für die nächste Runde generieren
char moveCommand = getNextMove(data, width, roverPosition);

return moveCommand;
protected SnakeBotMimi(String[] args) {
super(args);
}

// Methode zur Berechnung des nächsten Bewegungsbefehls
private char getNextMove(String data, int width, int roverPosition) {
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 = 'U';
break;
case 1: // Rechts
moveCommand = 'R';
break;
case 2: // Runter
moveCommand = 'D';
break;
case 3: // Links
moveCommand = 'L';
break;
default:
moveCommand = ' '; // Standardbewegungsbefehl, falls etwas schief geht
break;
@Override
protected char nextMove(View view) throws Exception {
char nextMove = '^';
boolean stoneDetected = view.data.contains("@");
System.out.println(view.data + " xxx: ");

// Das ist nur fuer debug
// List<Integer> positions = positionsOfRock(view.data);
// for (Integer i : positions)
// System.out.println("Position: " + i);

if (lastMoveWasTurn) {
lastMoveWasTurn = false; // Merker fuer die letzte Drehung zuruecksetzen
nextMove = '^';
}
// Ü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);
if (nextPosition < 0 || nextPosition >= data.length() || data.charAt(nextPosition) == '*') {
currentDirection++;
moveCommand = getNextMove(data, width, roverPosition); // Neue Richtung erneut berechnen
else {
if (!stoneDetected)
nextMove = '^';
else {
nextMove = calculateNextMove(view);
}
}

return moveCommand;
return nextMove;
}

// 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
/**
* Berechne den naechsten Zug auf Basis der gefundenen Steine im Scan
* @param view
* @return
*/
private char calculateNextMove(View view){
List<Integer> positions = positionsOfRock(view.data);
int firstPostion = positions.get(0);


// Wenn das @Zeichen an Position kleiner 11, dann ist es oberhalb von uns
if (firstPostion < 11 )
return '^';

// Ist die Position vom @Zeichen zwischn 11 und 12, dann links von uns
// wir muessen uns nach links drehen
else if (firstPostion >= 11 && firstPostion < 13) {
lastMoveWasTurn = true; // Merker auf true, da wir uns gedreht haben
return '<';
}
// Bei allen anderen Positionen drehen wir uns nach rechts drehen
else {
lastMoveWasTurn = true; // Merker auf true, da wir uns gedreht haben
return '>';
}
}
}

public static void main(String[] args) {
new SnakeBot(args).run();
}
/**
* Ermittle alle Positionen der Steine innerhalb des flachen Scans
* @param row das Ergebnis von View.data (der Scan als String)
* @return eine Liste mit den Positionen
*/
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;
}
}

Loading…
Cancel
Save