Browse Source

SnakeBot Breadth-First-Search

master
Your Average Code 3 months ago
parent
commit
d1ad5f80b4
4 changed files with 74 additions and 27 deletions
  1. 4
    0
      snaketest.sh
  2. 0
    2
      src/EscapeBot.java
  3. 70
    25
      src/SnakeBot.java
  4. 0
    0
      start.sh

+ 4
- 0
snaketest.sh View File

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

+ 0
- 2
src/EscapeBot.java View File

@@ -1,6 +1,4 @@
public class EscapeBot extends Bot {

boolean foundShip = false;
boolean offByOne = true;
int currentStepCount = 0;
int steps = 0;

+ 70
- 25
src/SnakeBot.java View File

@@ -1,13 +1,17 @@
public class SnakeBot extends Bot {
import java.util.LinkedList;
import java.util.Queue;

boolean foundShip = false;
boolean offByOne = true;
int currentStepCount = 0;
int steps = 0;
public class SnakeBot extends Bot {
protected final static String obstacles = "~#X*";
protected boolean offByOne = true;
protected int currentStepCount = 0;
protected int steps = 0;

public static void main(String[] args) {
Bot bot = new SnakeBot(args);
bot.run();
while (true) {
Bot bot = new SnakeBot(args);
bot.run();
}
}

protected SnakeBot(String[] args) {
@@ -16,38 +20,79 @@ public class SnakeBot extends Bot {

@Override
protected char nextMove(View view) throws Exception {
String data = view.data;
System.out.println();
String data = view.data;
char[][] grid = dataToGrid(view.data, view.width);

if (data.contains("@")) {
int index = data.indexOf('@');
if (index < view.width * 2 && !(index > view.width * 3)) {
return safeMove(data);
} else if (index % 5 < 2) {
return '<';
} else if (index % 5 > 2) {
return '>';
}
return ' ';
} else if (steps == 0) {
currentStepCount += 1;
return breadthFirstSearch(grid);
} else {
return walkAround(grid);
}
}

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) {
currentStepCount += 1;
currentStepCount++;
}
offByOne = !offByOne;
steps = currentStepCount;
return '>';
} else {
steps -= 1;
return safeMove(data);
steps--;
return safeMove(grid);
}
}

protected char safeMove(String data) {
if (data.charAt(7) == '*') {
return '>';
protected char safeMove(char[][] grid) {
int size = grid.length;
if (grid[size / 2][size / 2 - 1] == '*') {
return '<';
} else {
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) {
}

}

+ 0
- 0
start.sh View File


Loading…
Cancel
Save