Compare commits

...
5 Commits
Author SHA1 Message Date
erenk bb4cd98c0f Bot implementiert 2025-02-10 15:53:56 +01:00
erenk 21034d5255 Implementierung 2025-02-10 15:47:15 +01:00
erenk aa214fd32d RumbleBot implementiert 2025-02-10 15:29:02 +01:00
erenk 030a7da54f RumbleBot implementiert 2025-02-10 15:28:13 +01:00
erenk 820f954a77 RumbleBot implementiert 2025-02-10 15:25:15 +01:00
14 changed files with 131 additions and 1 deletions

No files matched your search

+1 -1
View File
@@ -19,7 +19,7 @@ public abstract class Bot {
private final int port; // Port des Servers private final int port; // Port des Servers
protected Bot(String[] args) { protected Bot(String[] args) {
host = args.length > 0 ? args[0] : "localhost"; host = args.length > 0 ? args[0] : "141.75.33.181";
port = args.length > 1 ? Integer.parseInt(args[1]) : 63187; port = args.length > 1 ? Integer.parseInt(args[1]) : 63187;
} }
+96
View File
@@ -0,0 +1,96 @@
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
View File
@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml
+11
View File
@@ -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>
+6
View File
@@ -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
View File
@@ -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>
+6
View File
@@ -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.