Browse Source

Dateien hochladen nach „“

master
Aylin Goekce 3 months ago
parent
commit
b67921efcc
1 changed files with 87 additions and 6 deletions
  1. 87
    6
      EscapeBot.java

+ 87
- 6
EscapeBot.java View File

@@ -3,17 +3,98 @@ package bot;
import java.util.Scanner;
public class EscapeBot extends Bot {
public EscapeBot (String[] args){
private int currentX = 0;
private int currentY = 0;
private int spiralSize = 1;
private int stepsInCurrentDirection = 0;
private char currentDirection = 'd'; // Start with moving to the right
public EscapeBot(String[] args) {
super(args);
}
public static void main (String[] args){
EscapeBot escapebot = new EscapeBot(args);
escapebot.run();
public static void main(String[] args) {
EscapeBot escapeBot = new EscapeBot(args);
escapeBot.run();
}
@Override
protected char nextMove(View view) throws Exception { //Strategie: In einer Spirale laufen bis es die Rakete findet
//Suche nach der Rakete im Spielfeld
protected char nextMove(View view) throws Exception {
char[][] map = parseMap(view.data, view.width);
char nextMove = 'w'; // Default move is to go forward
if (map[currentY][currentX] == 'o') {
System.out.println("Rakete gefunden!");
nextMove = 'q'; // Beende die Verbindung, wenn die Rakete gefunden wurde
} else {
// Bewegungslogik im Spiralmuster
switch (currentDirection) {
case 'd': // Bewege nach rechts
currentX++;
break;
case 's': // Bewege nach unten
currentY++;
break;
case 'a': // Bewege nach links
currentX--;
break;
case 'w': // Bewege nach oben
currentY--;
break;
}
stepsInCurrentDirection++;
// Wenn der Rover die maximale Anzahl von Schritten in dieser Richtung erreicht hat
if (stepsInCurrentDirection == spiralSize) {
// Ändere die Richtung nach rechts
switch (currentDirection) {
case 'd':
currentDirection = 's';
break;
case 's':
currentDirection = 'a';
break;
case 'a':
currentDirection = 'w';
break;
case 'w':
currentDirection = 'd';
break;
}
// Setze die Anzahl der Schritte in dieser Richtung zurück und erhöhe die Spiralgöße
stepsInCurrentDirection = 0;
if (currentDirection == 'd' || currentDirection == 'a') {
spiralSize++;
}
}
// Überprüfe, ob der nächste Zug innerhalb der Karte liegt
if (currentX < 0 || currentX >= view.width || currentY < 0 || currentY >= view.width) {
// Der nächste Zug liegt außerhalb der Karte, also drehe den Rover um
nextMove = 'a';
} else {
// Der nächste Zug liegt innerhalb der Karte, bewege den Rover dorthin
nextMove = currentDirection;
}
}
return nextMove;
}
// Hilfsmethode zum Parsen des Kartenscans in ein char-Array
private char[][] parseMap(String data, int width) {
char[][] map = new char[width][width];
String[] lines = data.split("\n");
for (int i = 0; i < width; i++) {
String line = lines[i];
for (int j = 0; j < width; j++) {
map[i][j] = line.charAt(j);
}
}
return map;
}
}

Loading…
Cancel
Save