Compare commits

..
5 Commits
Author SHA1 Message Date
Your Average Code 3f96b89a47 outsource record Move 2024-01-26 16:38:39 +01:00
Your Average Code a3de94a4f8 SnakeBot enemies 2024-01-25 21:27:20 +01:00
Your Average Code 59514963eb simplify RumbleBot 2024-01-25 21:26:53 +01:00
Your Average Code 13955b7b29 RumbleBot random turn 2024-01-25 19:16:42 +01:00
Your Average Code 6391e9d149 RumbleBot shorter path to line of sight 2024-01-25 19:04:30 +01:00
5 changed files with 65 additions and 65 deletions

No files matched your search

+1
View File
@@ -46,6 +46,7 @@ public abstract class Bot implements Runnable {
protected void print(View view) { protected void print(View view) {
view.print(); view.print();
System.out.println();
} }
// Diese Methode ermittelt den nächsten Zug des Bots. Sie wird von der // Diese Methode ermittelt den nächsten Zug des Bots. Sie wird von der
+7 -8
View File
@@ -1,3 +1,5 @@
import records.Move;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
@@ -80,13 +82,13 @@ public class EscapeBot extends Bot {
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Move move = queue.poll(); Move move = queue.poll();
if (move.x < 0 || move.x >= size || move.y < 0 || move.y >= size || visited[move.x][move.y]) continue; if (move.x() < 0 || move.x() >= size || move.y() < 0 || move.y() >= size || visited[move.x()][move.y()]) continue;
visited[move.x][move.y] = true; visited[move.x()][move.y()] = true;
if (obstacles.contains("" + grid[move.x][move.y])) continue; if (obstacles.contains("" + grid[move.x()][move.y()])) continue;
if (grid[move.x][move.y] == 'o') return move.direction; if (grid[move.x()][move.y()] == 'o') return move.direction();
for (int[] direction : directions) { for (int[] direction : directions) {
queue.add(new Move(move.x + direction[0], move.y + direction[1], move.direction)); queue.add(new Move(move.x() + direction[0], move.y() + direction[1], move.direction()));
} }
} }
@@ -94,7 +96,4 @@ public class EscapeBot extends Bot {
return safeMove(grid); return safeMove(grid);
} }
protected record Move(int x, int y, char direction) {
}
} }
+37 -49
View File
@@ -1,22 +1,23 @@
import records.Move;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
import java.util.Random; import java.util.Random;
public class RumbleBot extends Bot { public class RumbleBot extends Bot {
protected final static String obstacles = "~#X*"; protected final static String obstacles = "~#X*";
protected final static String targets = "v^<>"; protected final static String players = "v^<>";
protected final static String targets = players + "o";
protected Random random = new Random(); protected Random random = new Random();
protected int steps = 0; protected int steps = 0;
public static void main(String[] args) { public static void main(String[] args) {
if(args.length == 0) { if (args.length == 0) {
String[] dummyArgs = {"localhost", "63187"}; String[] dummyArgs = {"localhost", "63187"};
new Thread(new EnemyBot(dummyArgs)).start(); for (int i = 0; i < 5; i++) {
new Thread(new EnemyBot(dummyArgs)).start(); new Thread(new EnemyBot(dummyArgs)).start();
new Thread(new EnemyBot(dummyArgs)).start(); }
new Thread(new EnemyBot(dummyArgs)).start();
new Thread(new EnemyBot(dummyArgs)).start();
} }
Bot bot = new RumbleBot(args); Bot bot = new RumbleBot(args);
@@ -29,18 +30,14 @@ public class RumbleBot extends Bot {
@Override @Override
protected char nextMove(View view) { protected char nextMove(View view) {
System.out.println();
int size = view.width; int size = view.width;
String data = view.data; String data = view.data;
char[][] grid = dataToGrid(data, size); char[][] grid = dataToGrid(data, size);
if (data.contains("v") || data.contains("^") || data.contains("<") || data.contains(">")) { if (isInLineOfSight(grid)) {
if (isInLineOfSight(grid)) { return 'f';
return 'f'; } else if (data.contains("v") || data.contains("^") || data.contains("<") || data.contains(">")) {
} else { return breadthFirstSearch(grid);
return breadthFirstSearch(grid);
}
} else { } else {
return walkAround(grid); return walkAround(grid);
} }
@@ -50,40 +47,42 @@ public class RumbleBot extends Bot {
char[][] grid = new char[size][size]; char[][] grid = new char[size][size];
for (int i = 0; i < data.length(); i++) { for (int i = 0; i < data.length(); i++) {
char c = data.charAt(i); char c = data.charAt(i);
if("ABCDEFGHIJKLMNOPQRSTUVWYZ".contains("" + c)) if (Character.isUpperCase(c) && c != 'X') c = 'A';
c = 'A';
grid[i % size][i / size] = c; grid[i % size][i / size] = c;
} }
return grid; return grid;
} }
protected void markFiringLines(char[][] grid) { protected void markFiringLines(char[][] grid) {
int[][] dir = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
int size = grid.length; int size = grid.length;
for (int x = 0; x < size; x++) { for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) { for (int y = 0; y < size; y++) {
int[][] dir = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
switch (grid[x][y]) { switch (grid[x][y]) {
case '^' -> markLine(grid, x, y, dir[0], '*'); case '^' -> markLine(grid, x, y, dir[0], '*', false);
case 'v' -> markLine(grid, x, y, dir[1], '*'); case 'v' -> markLine(grid, x, y, dir[1], '*', false);
case '>' -> markLine(grid, x, y, dir[2], '*'); case '>' -> markLine(grid, x, y, dir[2], '*', false);
case '<' -> markLine(grid, x, y, dir[3], '*'); case '<' -> markLine(grid, x, y, dir[3], '*', false);
} }
if(targets.contains("" + grid[x][y])) { if (players.contains("" + grid[x][y])) {
for (int[] direction : dir) { for (int[] direction : dir) {
markLine(grid, x, y, direction, 'o'); markLine(grid, x, y, direction, 'o', true);
} }
} }
} }
} }
} }
private void markLine(char[][] grid, int x, int y, int[] dir, char c) { private void markLine(char[][] grid, int x, int y, int[] dir, char c, boolean block) {
int size = grid.length; int size = grid.length;
x += dir[0]; x += dir[0];
y += dir[1]; y += dir[1];
while (x >= 0 && x < size && y >= 0 && y < size && !obstacles.contains("" + grid[x][y])) { while (x >= 0 && x < size && y >= 0 && y < size && !obstacles.contains("" + grid[x][y])) {
if (grid[x][y] == 'A' && block) break;
if (grid[x][y] == '.') { if (grid[x][y] == '.') {
grid[x][y] = c; grid[x][y] = c;
} }
@@ -94,32 +93,24 @@ public class RumbleBot extends Bot {
protected boolean isInLineOfSight(char[][] grid) { protected boolean isInLineOfSight(char[][] grid) {
int size = grid.length; int size = grid.length;
for (int y = size / 2; y > 0; y--) { for (int y = size / 2; y >= 0; y--) {
if (players.contains("" + grid[size / 2][y]))
return true;
if (obstacles.contains("" + grid[size / 2][y])) if (obstacles.contains("" + grid[size / 2][y]))
break; break;
if (targets.contains("" + grid[size / 2][y]))
return true;
} }
return false; return false;
} }
protected char walkAround(char[][] grid) { protected char walkAround(char[][] grid) {
int size = grid.length;
if (steps == 0) { if (steps == 0) {
steps = random.nextInt(20); steps = random.nextInt(20);
return '>'; return random.nextBoolean() ? '<' : '>';
} else { } else {
steps--; steps--;
return safeMove(grid); return obstacles.contains("" + grid[size / 2][size / 2 - 1]) ? '<' : '^';
}
}
protected char safeMove(char[][] grid) {
int size = grid.length;
if (obstacles.contains("" + grid[size / 2][size / 2 - 1])) {
return '<';
} else {
return '^';
} }
} }
@@ -144,21 +135,18 @@ public class RumbleBot extends Bot {
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Move move = queue.poll(); Move move = queue.poll();
if (move.x < 0 || move.x >= size || move.y < 0 || move.y >= size || visited[move.x][move.y]) continue; if (move.x() < 0 || move.x() >= size || move.y() < 0 || move.y() >= size || visited[move.x()][move.y()]) continue;
visited[move.x][move.y] = true; visited[move.x()][move.y()] = true;
if (obstacles.contains("" + grid[move.x][move.y])) continue; if (obstacles.contains("" + grid[move.x()][move.y()])) continue;
if (targets.contains("" + grid[move.x][move.y])) return move.direction; if (targets.contains("" + grid[move.x()][move.y()])) return move.direction();
for (int[] direction : directions) { for (int[] direction : directions) {
queue.add(new Move(move.x + direction[0], move.y + direction[1], move.direction)); queue.add(new Move(move.x() + direction[0], move.y() + direction[1], move.direction()));
} }
} }
System.err.println("No path found"); System.err.println("No path found");
return safeMove(grid); return walkAround(grid);
}
protected record Move(int x, int y, char direction) {
} }
} }
+16 -8
View File
@@ -1,13 +1,24 @@
import records.Move;
import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
public class SnakeBot extends Bot { public class SnakeBot extends Bot {
protected final static String obstacles = "~#X*"; protected final static String obstacles = "~#X*";
protected final static char target = '@';
protected boolean offByOne = true; protected boolean offByOne = true;
protected int currentStepCount = 0; protected int currentStepCount = 0;
protected int steps = 0; protected int steps = 0;
public static void main(String[] args) { public static void main(String[] args) {
if (args.length == 0) {
String[] dummyArgs = {"localhost", "63187"};
for (int i = 0; i < 5; i++) {
new Thread(new SnakeBot(dummyArgs)).start();
}
}
Bot bot = new SnakeBot(args); Bot bot = new SnakeBot(args);
bot.run(); bot.run();
} }
@@ -99,13 +110,13 @@ public class SnakeBot extends Bot {
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Move move = queue.poll(); Move move = queue.poll();
if (move.x < 0 || move.x >= size || move.y < 0 || move.y >= size || visited[move.x][move.y]) continue; if (move.x() < 0 || move.x() >= size || move.y() < 0 || move.y() >= size || visited[move.x()][move.y()]) continue;
visited[move.x][move.y] = true; visited[move.x()][move.y()] = true;
if (obstacles.contains("" + grid[move.x][move.y])) continue; if (obstacles.contains("" + grid[move.x()][move.y()])) continue;
if (grid[move.x][move.y] == '@') return move.direction; if (grid[move.x()][move.y()] == target) return move.direction();
for (int[] direction : directions) { for (int[] direction : directions) {
queue.add(new Move(move.x + direction[0], move.y + direction[1], move.direction)); queue.add(new Move(move.x() + direction[0], move.y() + direction[1], move.direction()));
} }
} }
@@ -113,7 +124,4 @@ public class SnakeBot extends Bot {
return safeMove(grid); return safeMove(grid);
} }
protected record Move(int x, int y, char direction) {
}
} }
+4
View File
@@ -0,0 +1,4 @@
package records;
public record Move(int x, int y, char direction) {
}