Browse Source

SnakeBot Breadth-First-Search

master
Your Average Code 11 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

while true
do
docker run --rm -p 63187:63187 mediaeng/bots snakes
done

+ 0
- 2
src/EscapeBot.java View File

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;

+ 70
- 25
src/SnakeBot.java View File

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) { 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) { protected SnakeBot(String[] args) {


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

} }

+ 0
- 0
start.sh View File


Loading…
Cancel
Save