RumbleBot implementiert
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/Prog3A-Bot.iml" filepath="$PROJECT_DIR$/.idea/Prog3A-Bot.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user