From f66359ec72ade312f27916da7fda8fb37f316774 Mon Sep 17 00:00:00 2001 From: juniemoon Date: Tue, 11 Feb 2025 20:58:41 +0100 Subject: [PATCH] =?UTF-8?q?Escape,=20Snake=20aufger=C3=A4umt=20+=20RumbleB?= =?UTF-8?q?ot=20fertig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/EscapeBot.java | 24 ++++++------- src/RumbleBot.java | 88 ++++++++++++++++++++++++++++++++++++++++++++++ src/SnakeBot.java | 6 ++-- 3 files changed, 101 insertions(+), 17 deletions(-) create mode 100644 src/RumbleBot.java diff --git a/src/EscapeBot.java b/src/EscapeBot.java index 1632691..de4215b 100644 --- a/src/EscapeBot.java +++ b/src/EscapeBot.java @@ -1,13 +1,12 @@ public class EscapeBot extends Bot { - //Rechts,Unten,Links,Oben enum Direction { Right, Down, Left, Up } - //5 nach rechts, 5 nach unten, 10 nach links, 10 hoch, 15 nach rechts und 15 nach unten + int[] dx = {3,0,6,0}; int[] dy = {0,3,0,6}; char[] Commands = {'>','v','<','^'}; @@ -17,7 +16,7 @@ public class EscapeBot extends Bot { int NextIncrement = StepsToGoUntilNextEvaluation *2; boolean first_time = true; public EscapeBot(String[] args) { - super(args); // Konstruktor von Elternklasse Bot + super(args); } @Override @@ -29,24 +28,21 @@ public class EscapeBot extends Bot { if(first_time) { first_time = false ; return this.Commands[Direction.Right.ordinal()]; - } - if(-1 != rocketIndex) { + + if (-1 != rocketIndex) { Y_Rocket = rocketIndex / view.width; X_Rocket = rocketIndex % view.width; - //System.out.println("X_Rocket: " + X_Rocket + " Y_Rocket: " + Y_Rocket); - - if(2 != Y_Rocket) { + if (2 != Y_Rocket) { return (2 > Y_Rocket ) ? this.Commands[Direction.Up.ordinal()] : this.Commands[Direction.Down.ordinal()]; } - if(X_Rocket != 2) { - return (2>X_Rocket) ? this.Commands[Direction.Left.ordinal()] : this.Commands[Direction.Right.ordinal()]; + if (X_Rocket != 2) { + return (2 > X_Rocket) ? this.Commands[Direction.Left.ordinal()] : this.Commands[Direction.Right.ordinal()]; } } - else if(0 < this.StepsToGoUntilNextEvaluation) { - //System.out.println("CurrentDirection:" + this.CurrentDirection.toString() + " StepsToGo: " + this.StepsToGoUntilNextEvaluation); + else if (0 < this.StepsToGoUntilNextEvaluation) { StepsToGoUntilNextEvaluation--; return this.Commands[Direction.Up.ordinal()]; } @@ -73,12 +69,12 @@ public class EscapeBot extends Bot { StepsToGoUntilNextEvaluation = this.dx[Direction.Right.ordinal()]; break; } + this.Start = StepsToGoUntilNextEvaluation; return this.Commands[Direction.Right.ordinal()]; } - throw new Exception("This Line should not be reacher!!"); - + throw new Exception("This Line should not be reached!"); } public static void main(String[] args) { diff --git a/src/RumbleBot.java b/src/RumbleBot.java new file mode 100644 index 0000000..eeda0af --- /dev/null +++ b/src/RumbleBot.java @@ -0,0 +1,88 @@ +public class RumbleBot extends Bot { + + //Rechts,Unten,Links,Oben + enum Direction { + Right, + Down, + Left, + Up + } + int[] dx = {3,0,6,0}; + int[] dy = {0,3,0,6}; + char[] Commands = {'>','v','<','^'}; + Direction CurrentDirection = Direction.Right; + int StepsToGoUntilNextEvaluation = 3; + int Start = StepsToGoUntilNextEvaluation; + int NextIncrement = StepsToGoUntilNextEvaluation *2; + char[] Enemies = {'>','v','<','^'}; + boolean first_time = true; + public RumbleBot(String[] args) { + super(args); + } + + @Override + protected char nextMove(View view) throws Exception { + for (char enemy : Enemies) { + int enemyIndex = view.data.indexOf(enemy); + if (-1 != enemyIndex) { + //System.out.println("Gefunden: " + enemy + " bei Index " + enemyIndex); + + int Y_Enemy = enemyIndex / view.width; + int X_Enemy = enemyIndex % view.width; + + if(4 != Y_Enemy) { + return (4 > Y_Enemy ) ? 'f' : this.Commands[Direction.Down.ordinal()]; + //return (4 > Y_Enemy ) ? this.Commands[Direction.Up.ordinal()] : this.Commands[Direction.Down.ordinal()]; + } + + if(X_Enemy != 4) { + return (4 > X_Enemy) ? 'f' : this.Commands[Direction.Right.ordinal()]; + //return (4 > X_Enemy) ? this.Commands[Direction.Left.ordinal()] : this.Commands[Direction.Right.ordinal()]; + } + } + } + + if(first_time) { + first_time = false ; + return this.Commands[Direction.Right.ordinal()]; + } + + else if(0 < this.StepsToGoUntilNextEvaluation) { + StepsToGoUntilNextEvaluation--; + return this.Commands[Direction.Up.ordinal()]; + } + else if (0 == this.StepsToGoUntilNextEvaluation){ + switch(this.CurrentDirection) { + case Direction.Right: + dx[Direction.Right.ordinal()]+= this.NextIncrement; + this.CurrentDirection = Direction.Down; + StepsToGoUntilNextEvaluation = this.dy[Direction.Down.ordinal()]; + break; + case Direction.Down: + dy[Direction.Down.ordinal()] += this.NextIncrement; + this.CurrentDirection = Direction.Left; + StepsToGoUntilNextEvaluation = this.dx[Direction.Left.ordinal()]; + break; + case Direction.Left: + dx[Direction.Left.ordinal()] += this.NextIncrement; + this.CurrentDirection = Direction.Up; + StepsToGoUntilNextEvaluation = this.dy[Direction.Up.ordinal()]; + break; + case Direction.Up: + dy[Direction.Up.ordinal()] += this.NextIncrement; + this.CurrentDirection = Direction.Right; + StepsToGoUntilNextEvaluation = this.dx[Direction.Right.ordinal()]; + break; + } + this.Start = StepsToGoUntilNextEvaluation; + return this.Commands[Direction.Right.ordinal()]; + } + + throw new Exception("This Line should not be reached!"); + } + + public static void main(String[] args) { + RumbleBot rumbleBot = new RumbleBot(args); + rumbleBot.run(); + } +} \ No newline at end of file diff --git a/src/SnakeBot.java b/src/SnakeBot.java index ed20622..d07de5a 100644 --- a/src/SnakeBot.java +++ b/src/SnakeBot.java @@ -4,8 +4,8 @@ import java.awt.Point; public class SnakeBot extends Bot { - List tail = new ArrayList<>(); // speichert Positionen der Schlange - Point currentPos = new Point(2, 2); // Startposition des Bots + List tail = new ArrayList<>(); + Point currentPos = new Point(2, 2); public SnakeBot(String[] args) { super(args); @@ -41,7 +41,7 @@ public class SnakeBot extends Bot { return safeDirection(null); // falls gefährlich, neue Richtung wählen } - // Erst aktualisieren, wenn sichergestellt ist, dass die Bewegung sicher ist + // Erst aktualisieren, wenn sichergestellt ist, dass Bewegung sicher ist currentPos.setLocation(newX, newY); tail.add(new Point(newX, newY)); return direction;