Compare commits

..

No commits in common. "a3de94a4f8795304773b450d62437a0959c8221f" and "13955b7b29d1c65157b12c4ab6e6ffbe940cc40b" have entirely different histories.

3 changed files with 27 additions and 21 deletions

View File

@ -46,7 +46,6 @@ 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

View File

@ -28,14 +28,18 @@ 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);
} }
@ -92,23 +96,34 @@ 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 (players.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 random.nextBoolean() ? '<' : '>'; if(random.nextBoolean())
return '<';
else
return '>';
} else { } else {
steps--; steps--;
return obstacles.contains("" + grid[size / 2][size / 2 - 1]) ? '<' : '^'; return safeMove(grid);
}
}
protected char safeMove(char[][] grid) {
int size = grid.length;
if (obstacles.contains("" + grid[size / 2][size / 2 - 1])) {
return '<';
} else {
return '^';
} }
} }
@ -144,7 +159,7 @@ public class RumbleBot extends Bot {
} }
System.err.println("No path found"); System.err.println("No path found");
return walkAround(grid); return safeMove(grid);
} }
protected record Move(int x, int y, char direction) { protected record Move(int x, int y, char direction) {

View File

@ -3,19 +3,11 @@ 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();
} }
@ -110,7 +102,7 @@ public class SnakeBot extends Bot {
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] == target) return move.direction; if (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));