SnakeBot simplify

This commit is contained in:
Your Average Code 2024-01-25 00:46:34 +01:00
parent d1ad5f80b4
commit 74e8133b7c

View File

@ -19,10 +19,14 @@ public class SnakeBot extends Bot {
} }
@Override @Override
protected char nextMove(View view) throws Exception { protected char nextMove(View view) {
System.out.println(); System.out.println();
String data = view.data; String data = view.data
char[][] grid = dataToGrid(view.data, view.width); .replace('^', '*')
.replace('<', '*')
.replace('>', '*')
.replace('V', '*');
char[][] grid = dataToGrid(data, view.width);
if (data.contains("@")) { if (data.contains("@")) {
return breadthFirstSearch(grid); return breadthFirstSearch(grid);
@ -31,11 +35,10 @@ public class SnakeBot extends Bot {
} }
} }
protected char[][] dataToGrid(String data, int width) { protected char[][] dataToGrid(String data, int size) {
int height = data.length() / width; char[][] grid = new char[size][size];
char[][] grid = new char[width][height];
for (int i = 0; i < data.length(); i++) { for (int i = 0; i < data.length(); i++) {
grid[i % width][i / width] = data.charAt(i); grid[i % size][i / size] = data.charAt(i);
} }
return grid; return grid;
} }
@ -58,34 +61,61 @@ public class SnakeBot extends Bot {
protected char safeMove(char[][] grid) { protected char safeMove(char[][] grid) {
int size = grid.length; int size = grid.length;
if (grid[size / 2][size / 2 - 1] == '*') { if (grid[size / 2][size / 2 - 1] == '*') {
return '<'; return safeTurn(grid);
} else { } else {
return '^'; return '^';
} }
} }
protected char breadthFirstSearch(char[][] grid) { protected char safeTurn(char[][] grid) {
int width = grid[0].length; int size = grid.length;
int height = grid.length;
int startX = width / 2;
int startY = height / 2;
int leftStars = 0;
int rightStars = 0;
for (int x = 0; x < size / 2; x++) {
for (int y = 0; y < size; y++) {
if (grid[x][y] == '*')
leftStars++;
if (grid[size - x - 1][y] == '*')
rightStars++;
}
}
if (leftStars > rightStars) {
return '>';
} else {
return '<';
}
}
protected char breadthFirstSearch(char[][] grid) {
int size = grid.length;
int start = size / 2;
boolean[][] visited = new boolean[size][size];
Queue<Move> queue = new LinkedList<>(); Queue<Move> queue = new LinkedList<>();
queue.add(new Move(startX, startY - 1, '^'));
queue.add(new Move(startX, startY + 1, '>')); int[][] directions = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
queue.add(new Move(startX + 1, startY, '>')); char[] commands = {'^', '>', '>', '<'};
queue.add(new Move(startX - 1, startY, '<')); for (int i = 0; i < 4; i++) {
queue.add(new Move(start + directions[i][0], start + directions[i][1], commands[i]));
}
queue.add(new Move(start, start - 1, '^'));
queue.add(new Move(start, start + 1, '>'));
queue.add(new Move(start + 1, start, '>'));
queue.add(new Move(start - 1, start, '<'));
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Move move = queue.poll(); Move move = queue.poll();
if (move.x < 0 || move.x >= width || move.y < 0 || move.y >= height) 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;
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] == '@') return move.direction;
queue.add(new Move(move.x, move.y - 1, move.direction)); for (int[] direction : directions) {
queue.add(new Move(move.x, move.y + 1, move.direction)); queue.add(new Move(move.x + direction[0], move.y + direction[1], move.direction));
queue.add(new Move(move.x + 1, move.y, move.direction)); }
queue.add(new Move(move.x - 1, move.y, move.direction));
} }
System.err.println("No path found"); System.err.println("No path found");