Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
14e77646ff | ||
![]() |
946e029c3f | ||
![]() |
4372c2a991 | ||
![]() |
e4dc5ec815 | ||
![]() |
0d6feebb8e | ||
![]() |
ac45ada088 |
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -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
|
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/ProgA-Bot.iml" filepath="$PROJECT_DIR$/ProgA-Bot.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
11
ProgA-Bot.iml
Normal file
11
ProgA-Bot.iml
Normal 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>
|
8
out/production/ProgA-Bot/.idea/.gitignore
generated
vendored
Normal file
8
out/production/ProgA-Bot/.idea/.gitignore
generated
vendored
Normal file
@ -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
|
6
out/production/ProgA-Bot/.idea/misc.xml
generated
Normal file
6
out/production/ProgA-Bot/.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
out/production/ProgA-Bot/.idea/modules.xml
generated
Normal file
8
out/production/ProgA-Bot/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/ProgA-Bot.iml" filepath="$PROJECT_DIR$/ProgA-Bot.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
out/production/ProgA-Bot/.idea/vcs.xml
generated
Normal file
6
out/production/ProgA-Bot/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
11
out/production/ProgA-Bot/ProgA-Bot.iml
Normal file
11
out/production/ProgA-Bot/ProgA-Bot.iml
Normal 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>
|
3
out/production/ProgA-Bot/README.md
Normal file
3
out/production/ProgA-Bot/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# ProgA-Bot
|
||||
|
||||
Jeder erstellt seinen eigenen Branch und wir mergen am ende das was am besten funktioniert
|
BIN
out/production/ProgA-Bot/src/Bot$View.class
Normal file
BIN
out/production/ProgA-Bot/src/Bot$View.class
Normal file
Binary file not shown.
BIN
out/production/ProgA-Bot/src/Bot.class
Normal file
BIN
out/production/ProgA-Bot/src/Bot.class
Normal file
Binary file not shown.
BIN
out/production/ProgA-Bot/src/EscapeBot.class
Normal file
BIN
out/production/ProgA-Bot/src/EscapeBot.class
Normal file
Binary file not shown.
BIN
out/production/ProgA-Bot/src/ManualBot.class
Normal file
BIN
out/production/ProgA-Bot/src/ManualBot.class
Normal file
Binary file not shown.
BIN
out/production/ProgA-Bot/src/RumbleBot.class
Normal file
BIN
out/production/ProgA-Bot/src/RumbleBot.class
Normal file
Binary file not shown.
BIN
out/production/ProgA-Bot/src/SnakeBot.class
Normal file
BIN
out/production/ProgA-Bot/src/SnakeBot.class
Normal file
Binary file not shown.
BIN
out/production/ProgA-Bot/src/test.class
Normal file
BIN
out/production/ProgA-Bot/src/test.class
Normal file
Binary file not shown.
88
src/Bot.java
Normal file
88
src/Bot.java
Normal file
@ -0,0 +1,88 @@
|
||||
package src;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
49
src/EscapeBot.java
Normal file
49
src/EscapeBot.java
Normal file
@ -0,0 +1,49 @@
|
||||
package src;
|
||||
|
||||
public class EscapeBot extends Bot {
|
||||
int stepCounter = 0;
|
||||
int turnCount = 0;
|
||||
int viewRange = 5;
|
||||
int straightLength = 1;
|
||||
|
||||
protected EscapeBot(String[] args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EscapeBot bot = new EscapeBot(args);
|
||||
bot.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected char nextMove(View view) throws Exception {
|
||||
|
||||
if (!view.data.contains("o")) {
|
||||
|
||||
if (turnCount == 2) {
|
||||
turnCount = 0;
|
||||
straightLength++;
|
||||
}
|
||||
if (stepCounter < straightLength * viewRange) {
|
||||
stepCounter++;
|
||||
return '^';
|
||||
} else {
|
||||
stepCounter = 0;
|
||||
turnCount++;
|
||||
return '>';
|
||||
}
|
||||
} else {
|
||||
if (view.data.substring(0, 10).contains("o")) {
|
||||
return '^';
|
||||
} else if (view.data.substring(10, 12).contains("o")) {
|
||||
return '<';
|
||||
} else if (view.data.substring(13, 15).contains("o")) {
|
||||
return '>';
|
||||
} else if (view.data.substring(15, 25).contains("o")) {
|
||||
return 'v';
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
34
src/ManualBot.java
Normal file
34
src/ManualBot.java
Normal file
@ -0,0 +1,34 @@
|
||||
package src;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ManualBot extends Bot {
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
protected ManualBot(String[] args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ManualBot bot = new ManualBot(args);
|
||||
bot.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected char nextMove(View view) throws Exception {
|
||||
char ch = 0;
|
||||
if (scanner.hasNextLine()) {
|
||||
ch = scanner.nextLine().toCharArray()[0];
|
||||
}
|
||||
|
||||
return switch (ch) {
|
||||
case 'w' -> '^';
|
||||
case 's' -> 'v';
|
||||
case 'a' -> '<';
|
||||
case 'd' -> '>';
|
||||
case 'q' -> throw new Exception();
|
||||
default -> 'l';
|
||||
};
|
||||
}
|
||||
}
|
92
src/RumbleBot.java
Normal file
92
src/RumbleBot.java
Normal file
@ -0,0 +1,92 @@
|
||||
package src;
|
||||
|
||||
public class RumbleBot extends Bot{
|
||||
|
||||
|
||||
String[] playerChars = {"<",">","^","v"};
|
||||
int angle = 0;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
boolean left = false;
|
||||
boolean right = false;
|
||||
boolean up = false;
|
||||
boolean down = false;
|
||||
int turnCounter = 0;
|
||||
int waitTimer = 80;
|
||||
|
||||
protected RumbleBot(String[] args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
RumbleBot bot = new RumbleBot(args);
|
||||
bot.run();
|
||||
}
|
||||
|
||||
protected char nextMove(View view){
|
||||
|
||||
if(waitTimer > 0){
|
||||
waitTimer--;
|
||||
return 0;
|
||||
}
|
||||
//back away if possible
|
||||
if(playerAhead(view) && !view.data.substring(49,50).contains("X")){
|
||||
return 'v';
|
||||
}
|
||||
//shoot target
|
||||
if(playerAhead(view)){
|
||||
return 'f';
|
||||
}
|
||||
|
||||
if(view.data.substring(39,40).contains("X") && view.data.substring(49,50).contains("X")){
|
||||
return '^';
|
||||
}else turnRight();
|
||||
// if (x <= 15 && !up){
|
||||
// x++;
|
||||
// return '^';
|
||||
// }else if(angle != 180){
|
||||
// up = true;
|
||||
// return turnRight();
|
||||
// }else if (x >= -15 && !down){
|
||||
// x--;
|
||||
// return '^';
|
||||
// } else if (angle != 0) {
|
||||
// down = true;
|
||||
// return turnRight();
|
||||
// }else if (x < 0){
|
||||
// x++;
|
||||
// return '^';
|
||||
// } else if (angle != 90 && x == 0) {
|
||||
// return turnRight();
|
||||
// } else if (y <= 15 && !right) {
|
||||
// y++;
|
||||
// return '^';
|
||||
// } else if (angle != 270) {
|
||||
// right = true;
|
||||
// return turnRight();
|
||||
// } else if(y >= -15 && !left){
|
||||
// y--;
|
||||
// return '^';
|
||||
// }else if(angle != 90){
|
||||
// turnRight();
|
||||
// }else if (y > 0){
|
||||
// y++;
|
||||
// return '^';
|
||||
// }
|
||||
return 0;
|
||||
}
|
||||
|
||||
private boolean playerAhead(View view){
|
||||
for (String s : playerChars){
|
||||
if(view.data.substring(4,5).contains(s) || view.data.substring(43,45).contains(s)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private char turnRight(){
|
||||
angle = (angle + 90) % 360;
|
||||
return '>';
|
||||
}
|
||||
}
|
128
src/SnakeBot.java
Normal file
128
src/SnakeBot.java
Normal file
@ -0,0 +1,128 @@
|
||||
package src;
|
||||
|
||||
public class SnakeBot extends Bot {
|
||||
int straightLength = 1; //length of the straight the bot still needs to travel(in "FOV-Tiles")
|
||||
int stepCounter = 0; //steps the bot has already taken on the straight
|
||||
int turnCount = 2; //amount of times the bot needs to turn before increasing straightLength
|
||||
final int VIEWRANGE = 5; //distance from one end to the bots FOV to the other (assumes square FOV)
|
||||
int wagonCount = 0; //amount of wagons the rover is currently pulling
|
||||
int angle = 0; //current angle of the rover, compared to its initial angle
|
||||
boolean isOnPath = true; //if the bot is on its normal search path (not pathing to a mineral)
|
||||
char[] clearSequence = {'<', '^', '<', '^', '^', '>', '^', '>', '^', '^', '^', '^', '>', '^', '>', '^', '<', '^', '^', '>',
|
||||
'^', '^', '^', '<', '^', '<', '^', '^', '^', '^', '<', '^', '^', '>'};
|
||||
char[] cornerClearSequence = {'<', '^', '<', '^', '^', '>', '^', '>', '^', '^', '^', '^', '>', '^', '>', '^', '<', '^',
|
||||
'<', '^', '>', '^', '^', '>', '^', '>', '^', '<', '^', '^', '^', '<', '^', '<', '^', '^', '>'};
|
||||
int clearSequenceCounter = 0;
|
||||
boolean isClearing = false;
|
||||
|
||||
protected SnakeBot(String[] args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SnakeBot bot = new SnakeBot(args);
|
||||
bot.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected char nextMove(View view) throws Exception {
|
||||
|
||||
// if (!view.data.contains("@") && isOnPath) {
|
||||
//
|
||||
// if (turnCount <= 0) {
|
||||
// turnCount = 2;
|
||||
// straightLength++;
|
||||
// }
|
||||
// if (stepCounter < straightLength * viewRange) {
|
||||
// stepCounter++;
|
||||
// return '^';
|
||||
// } else {
|
||||
// stepCounter = 0;
|
||||
// turnCount--;
|
||||
// angle = (angle + 90) % 360;
|
||||
// return '>';
|
||||
// }
|
||||
// } else if (!view.data.contains("@") && !isOnPath) {
|
||||
//
|
||||
// } else {
|
||||
// isOnPath = false;
|
||||
// //check for minerals to the left of the rover (high prio)
|
||||
// for (int i = 0; i < view.data.length(); i += viewRange) {
|
||||
// if (view.data.substring(i, i + 2).contains("@")) {
|
||||
// angle = (angle + 270) % 360;
|
||||
// return '<';
|
||||
// }
|
||||
// }
|
||||
// //check for minerals in front of the rover (mid prio)
|
||||
// if (view.data.substring(0, view.data.length() / 2 - viewRange / 2).contains("@")) {
|
||||
// return '^';
|
||||
// }
|
||||
// //check for minerals to the right of the rover (low prio)
|
||||
// for (int i = 3; i < view.data.length(); i += viewRange) {
|
||||
// if (view.data.substring(i, i + 2).contains("@")) {
|
||||
// angle = (angle + 90) % 360;
|
||||
// return '>';
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (view.data.substring(0, 10).contains("@")) {
|
||||
// return '^';
|
||||
// } else if (view.data.substring(10, 12).contains("@")) {
|
||||
// return '<';
|
||||
// } else if (view.data.substring(13, 15).contains("@")) {
|
||||
// return '>';
|
||||
// } else if (view.data.substring(15, 25).contains("@")) {
|
||||
// return 'v';
|
||||
// }
|
||||
// }
|
||||
|
||||
if (stepCounter % VIEWRANGE == 0 && !isClearing && view.data.contains("@")) {
|
||||
isClearing = true;
|
||||
}
|
||||
if (isClearing) {
|
||||
return clearFov(view);
|
||||
} else {
|
||||
if (turnCount <= 0) {
|
||||
turnCount = 2;
|
||||
straightLength++;
|
||||
}
|
||||
//if rover hasn't reached corner
|
||||
if (stepCounter < straightLength * VIEWRANGE) {
|
||||
stepCounter++;
|
||||
return '^';
|
||||
} else {
|
||||
stepCounter = 0;
|
||||
turnCount--;
|
||||
return '>';
|
||||
}
|
||||
}
|
||||
//return 0;
|
||||
}
|
||||
|
||||
protected char clearFov(View view) {
|
||||
char move;
|
||||
//check if rover is at a corner of its search path
|
||||
if (stepCounter >= straightLength * VIEWRANGE) {
|
||||
move = cornerClearSequence[clearSequenceCounter++];
|
||||
|
||||
//update rover state and reset sequence after its done
|
||||
if (clearSequenceCounter >= cornerClearSequence.length) {
|
||||
isClearing = false;
|
||||
stepCounter = 2;
|
||||
turnCount--;
|
||||
clearSequenceCounter = 0;
|
||||
}
|
||||
} else {
|
||||
move = clearSequence[clearSequenceCounter++];
|
||||
|
||||
//update rover state and reset sequence after its done
|
||||
if (clearSequenceCounter >= clearSequence.length) {
|
||||
isClearing = false;
|
||||
stepCounter += 2;
|
||||
clearSequenceCounter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return move;
|
||||
}
|
||||
}
|
31
src/test.java
Normal file
31
src/test.java
Normal file
@ -0,0 +1,31 @@
|
||||
package src;
|
||||
|
||||
public class test extends Bot{
|
||||
|
||||
String[] playerChars = {"<",">","^","v"};
|
||||
|
||||
protected test(String[] args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test bot = new test(args);
|
||||
bot.run();
|
||||
}
|
||||
|
||||
protected char nextMove(View view){
|
||||
if(playerAhead(view)){
|
||||
return 'f';
|
||||
}
|
||||
return '>';
|
||||
}
|
||||
|
||||
private boolean playerAhead(View view){
|
||||
for (String s : playerChars){
|
||||
if(view.data.substring(4,5).contains(s) || view.data.substring(13,14).contains(s)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user