1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package bot;
-
- public class SnakeBot extends Bot {
-
- private int currentDirection = 1; // Variable, um die aktuelle Bewegungsrichtung des Rovers zu halten
- // 0 - oben, 1 - rechts, 2 - unten, 3 - links
-
- public SnakeBot(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) == '@';
-
- // 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;
- }
-
- // 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;
- }
-
- // Ü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
- }
-
- 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();
- }
- }
|