commit 6cdba4e0d0e78a380f9f0bccf07b47e73ce22536 Author: david Date: Tue Feb 11 14:06:40 2025 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a6e1098 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b352f21 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Studientag.iml b/Studientag.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Studientag.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Bot.java b/src/Bot.java new file mode 100644 index 0000000..5fd98b5 --- /dev/null +++ b/src/Bot.java @@ -0,0 +1,85 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; + +public abstract class Bot { + + // Ein Bot ist ein Programm, das sich mit einem Server verbindet und + // mit diesem kommuniziert. Der Server sendet dem Bot eine Zeichenkette, + // die das Spielfeld repräsentiert. Der Bot sendet dem Server ein Zeichen, + // das die nächste Bewegung des Bots repräsentiert. + + + private final String host; // Hostname oder IP-Adresse des Servers + private final int port; // Port des Servers + + protected Bot(String[] args) { + host = args.length > 0 ? args[0] : "localhost"; + port = args.length > 1 ? Integer.parseInt(args[1]) : 63187; + } + + // Diese Methode stellt die Verbindung zum Server her und startet die + // Kommunikation mit dem Server. Die Methode wird von der main-Methode + // aufgerufen. + protected void run() { + try (Socket socket = new Socket()) { + socket.connect(new InetSocketAddress(host, port)); + OutputStream out = socket.getOutputStream(); + BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); + View view = new View(); + while (true) { + view.read(in); + view.print(); + try { + char ch = nextMove(view); + out.write(ch); + } + catch (Exception e) { break; } + } + socket.close(); + } catch (IOException e) { + System.err.println("Error: " + e.getMessage()); + } + } + + // Diese Methode ermittelt den nächsten Zug des Bots. Sie wird von der + // run-Methode aufgerufen, nachdem der Server das Spielfeld gesendet hat. + // Subklassen müssen diese Methode implementieren. + abstract protected char nextMove(View view) throws Exception; + + // Diese Klasse repräsentiert das Spielfeld. Sie wird von der run-Methode + // verwendet, um das Spielfeld zu lesen und auszugeben. + // Subklassen können diese Klasse verwenden, um das Spielfeld zu analysieren. + public static class View { + protected String data; + protected int width; + + // Diese Methode liest das Spielfeld vom Server. + private void read(BufferedReader in) throws IOException { + StringBuilder sb = new StringBuilder(); + data = in.readLine(); + if (data == null) { + return; + } + sb.append(data); + width = data.length(); + for (int i = 1; i < width; ++i) { + sb.append(in.readLine()); + } + data = sb.toString(); + } + + // Diese Methode gibt das Spielfeld aus. + protected void print() { + if (data == null || width < 1) { + return; + } + for (int i = 0, len = data.length(); i < len; i += width) { + System.out.println(data.substring(i, i + width)); + } + } + } +} diff --git a/src/EscapeBot.java b/src/EscapeBot.java new file mode 100644 index 0000000..ad20fdc --- /dev/null +++ b/src/EscapeBot.java @@ -0,0 +1,101 @@ +import java.util.LinkedList; +import java.util.Queue; + +public class EscapeBot extends Bot{ + private int index = 2; + private int FUT = 1; //Fields until Turn (5er Felder) + private boolean moving = false; + private int movingIndex = 0; + + private boolean inFinalMode = false; + private Queue finalAproachCourse = new LinkedList<>(); + + public static void main(String[] args){ + EscapeBot eb = new EscapeBot(args); + eb.run(); + + } + + public EscapeBot(String[] args){ + super(args); + } + protected char nextMove(View view) throws Exception { + + System.out.println("------------------------"); + + if(moving){ //Beim Laufen zum nächsten Feld wird das hier aufgerufen, es muss nicht gescanned werden etc. + movingIndex++; + if(movingIndex == 4){ + movingIndex = 0; + moving = false; + } + return '^'; + } + + if(scanForShip(view) || inFinalMode){ + inFinalMode = true; + return finalAproachCourse.poll(); + } + else{ + if (FUT == 0){ + index++; + FUT = index/2; + System.out.println("turn left"); + return '<'; + } + else{ + moving = true; //Wenn moving auf true gesetzt, geht Bot 5 Felder nach vorne + System.out.println(FUT); + FUT--; + return '^'; + } + } + } + + + + protected boolean scanForShip(View view){ + String scannedField = view.data; + int pos = scannedField.indexOf('o'); + if(pos == -1){ //Spaceship ist nicht im Feld + return false; + } else{ + createFinalPlan(pos); + return true; + } + } + + protected void createFinalPlan(int pos){ + int row = pos/5; + int column = pos%5; + switch (row){ + case 0: + finalAproachCourse.add('^'); + finalAproachCourse.add('^'); + break; + case 1: + finalAproachCourse.add('^'); + break; + case 3: + finalAproachCourse.add('v'); + break; + case 4: + finalAproachCourse.add('v'); + finalAproachCourse.add('v'); + break; + default: + break; + } + + if(column < 2){ + finalAproachCourse.add('<'); + finalAproachCourse.add('^'); + finalAproachCourse.add('^'); + } else{ + finalAproachCourse.add('>'); + finalAproachCourse.add('^'); + finalAproachCourse.add('^'); + } + } + +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..3e59c38 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/src/ManualBot.java b/src/ManualBot.java new file mode 100644 index 0000000..20ab6e4 --- /dev/null +++ b/src/ManualBot.java @@ -0,0 +1,53 @@ +import java.util.Scanner; + + public class ManualBot extends Bot{ + + private Scanner scanner; + + public static void main(String[] args){ + ManualBot mb = new ManualBot(args); + mb.run(); + + } + + public ManualBot(String[] args){ + super(args); + scanner = new Scanner(System.in); + } + + @Override + protected char nextMove(View view) throws Exception { + + char c = '\0'; + while (scanner.hasNextLine()) { + String input = scanner.nextLine().trim(); + if (input.isEmpty()) { + break; + } + if (input.length() == 1) { + c = translate(input.charAt(0)); + System.out.println(c); + break; + } else { + System.out.println("nur einen Schritt gehen"); + } + } + return c; + } + + private char translate(char x) throws Exception{ + switch (x){ + case 'w': + return '^'; + case 'a': + return '<'; + case 'd': + return '>'; + case 's': + return 'v'; + case 'q': + throw new Exception(); + } + return x; + } + } diff --git a/src/RumbleBot.java b/src/RumbleBot.java new file mode 100644 index 0000000..6aed152 --- /dev/null +++ b/src/RumbleBot.java @@ -0,0 +1,76 @@ +public class RumbleBot extends Bot { + + private int game_state = 0; + + public static void main(String[] args) { + RumbleBot eb = new RumbleBot(args); + eb.run(); + } + + public RumbleBot(String[] args) { + super(args); + } + + protected char nextMove(View view) throws Exception { + String scan = view.data; + //if (Gegner in Schusslinie) --> Feuer + if (checkFireLane(scan)) { + return 'f'; + } + //if (Gegner links, oder rechts) --> drehen + switch (checkLeftAndRight(scan)) { + case 0: + return '<'; + case 1: + return '>'; + default: + break; + } + + //if Hindernis --> drehen + if (checkForCollission(scan)) { + return '<'; + } + + //else --> laufen + return '^'; + } + + private boolean checkFireLane(String scan) { + String notEnemy = ".~#XA@"; + char c1 = scan.charAt(2); + char c2 = scan.charAt(7); + if (notEnemy.indexOf(c1) == -1 || notEnemy.indexOf(c2) == -1) { //wenn true, dann Gegner vor uns + return true; + } else { + return false; + } + } + + private int checkLeftAndRight(String scan) { + String notEnemy = ".~#XA@"; + char l1 = scan.charAt(10); + char l2 = scan.charAt(11); + char r1 = scan.charAt(13); + char r2 = scan.charAt(14); + + if (notEnemy.indexOf(l1) == -1 || notEnemy.indexOf(l2) == -1) { //wenn true, dann Gegner vor uns + return 0; + } + + if (notEnemy.indexOf(r1) == -1 || notEnemy.indexOf(r2) == -1) { //wenn true, dann Gegner vor uns + return 1; + } + + return 2; + } + + private boolean checkForCollission(String scan) { + char c = scan.charAt(7); + if (c != '.') { + return true; + } else { + return false; + } + } +} diff --git a/src/SnakeBot.java b/src/SnakeBot.java new file mode 100644 index 0000000..6c5c573 --- /dev/null +++ b/src/SnakeBot.java @@ -0,0 +1,56 @@ +public class SnakeBot extends Bot { + + private boolean moving = false; + private int stepCount = 0; + + public SnakeBot(String[] args){ + super(args); + } + public static void main(String[] args){ + SnakeBot sb = new SnakeBot(args); + sb.run(); + } + + protected char nextMove(View view) throws Exception { + String scannedField = view.data; + char front = scannedField.charAt(7); + + if(checkForCollission(front)){ + stepCount = 0; + return '<';//Mach was, sonst dod + } + + if(moving == true) {moving = false; return '^';} + + if(stepCount == 20){ + stepCount = 0; + return '<'; + } + + + if(scannedField.charAt(7) == '@' || scannedField.charAt(2) == '@'){ //Stein ist oberhalb + stepCount ++; + return '^'; + } + else if(scannedField.charAt(10) == '@' || scannedField.charAt(11) == '@'){ //Stein ist links + moving = true; + stepCount = 0; + return '<'; + } + else if(scannedField.charAt(13) == '@' || scannedField.charAt(14) == '@'){ //Stein ist links + moving = true; + stepCount = 0; + return '>'; + } + + else {stepCount++; return '^';} + } + + private boolean checkForCollission(char c){ + if(c == '*' || c == 'A'){ + return true; + }else { + return false; + } + } +} \ No newline at end of file