SnakeBot Breadth-First-Search

This commit is contained in:
Your Average Code 2024-01-24 23:39:55 +01:00
parent a3c5acb8bd
commit d1ad5f80b4
4 changed files with 74 additions and 27 deletions

4
snaketest.sh Executable file
View File

@ -0,0 +1,4 @@
while true
do
docker run --rm -p 63187:63187 mediaeng/bots snakes
done

View File

@ -1,6 +1,4 @@
public class EscapeBot extends Bot { public class EscapeBot extends Bot {
boolean foundShip = false;
boolean offByOne = true; boolean offByOne = true;
int currentStepCount = 0; int currentStepCount = 0;
int steps = 0; int steps = 0;

View File

@ -1,14 +1,18 @@
public class SnakeBot extends Bot { import java.util.LinkedList;
import java.util.Queue;
boolean foundShip = false; public class SnakeBot extends Bot {
boolean offByOne = true; protected final static String obstacles = "~#X*";
int currentStepCount = 0; protected boolean offByOne = true;
int steps = 0; protected int currentStepCount = 0;
protected int steps = 0;
public static void main(String[] args) { public static void main(String[] args) {
while (true) {
Bot bot = new SnakeBot(args); Bot bot = new SnakeBot(args);
bot.run(); bot.run();
} }
}
protected SnakeBot(String[] args) { protected SnakeBot(String[] args) {
super(args); super(args);
@ -16,38 +20,79 @@ public class SnakeBot extends Bot {
@Override @Override
protected char nextMove(View view) throws Exception { protected char nextMove(View view) throws Exception {
String data = view.data;
System.out.println(); System.out.println();
String data = view.data;
char[][] grid = dataToGrid(view.data, view.width);
if (data.contains("@")) { if (data.contains("@")) {
int index = data.indexOf('@'); return breadthFirstSearch(grid);
if (index < view.width * 2 && !(index > view.width * 3)) { } else {
return safeMove(data); return walkAround(grid);
} else if (index % 5 < 2) {
return '<';
} else if (index % 5 > 2) {
return '>';
} }
return ' '; }
} else if (steps == 0) {
currentStepCount += 1; protected char[][] dataToGrid(String data, int width) {
int height = data.length() / width;
char[][] grid = new char[width][height];
for (int i = 0; i < data.length(); i++) {
grid[i % width][i / width] = data.charAt(i);
}
return grid;
}
protected char walkAround(char[][] grid) {
if (steps == 0) {
currentStepCount++;
if (offByOne) { if (offByOne) {
currentStepCount += 1; currentStepCount++;
} }
offByOne = !offByOne; offByOne = !offByOne;
steps = currentStepCount; steps = currentStepCount;
return '>'; return '>';
} else { } else {
steps -= 1; steps--;
return safeMove(data); return safeMove(grid);
} }
} }
protected char safeMove(String data) { protected char safeMove(char[][] grid) {
if (data.charAt(7) == '*') { int size = grid.length;
return '>'; if (grid[size / 2][size / 2 - 1] == '*') {
return '<';
} else { } else {
return '^'; return '^';
} }
} }
protected char breadthFirstSearch(char[][] grid) {
int width = grid[0].length;
int height = grid.length;
int startX = width / 2;
int startY = height / 2;
Queue<Move> queue = new LinkedList<>();
queue.add(new Move(startX, startY - 1, '^'));
queue.add(new Move(startX, startY + 1, '>'));
queue.add(new Move(startX + 1, startY, '>'));
queue.add(new Move(startX - 1, startY, '<'));
while (!queue.isEmpty()) {
Move move = queue.poll();
if (move.x < 0 || move.x >= width || move.y < 0 || move.y >= height) continue;
if (obstacles.contains("" + grid[move.x][move.y])) continue;
if (grid[move.x][move.y] == '@') return move.direction;
queue.add(new Move(move.x, move.y - 1, move.direction));
queue.add(new Move(move.x, move.y + 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");
return safeMove(grid);
}
protected record Move(int x, int y, char direction) {
}
} }

View File