From d1ad5f80b4f56b8b7dcc79fe379c2918ddfde0a4 Mon Sep 17 00:00:00 2001 From: Your Average Code <138674451+UrAvgCode@users.noreply.github.com> Date: Wed, 24 Jan 2024 23:39:55 +0100 Subject: [PATCH] SnakeBot Breadth-First-Search --- snaketest.sh | 4 ++ src/EscapeBot.java | 2 - src/SnakeBot.java | 95 ++++++++++++++++++++++++++++++++++------------ start.sh | 0 4 files changed, 74 insertions(+), 27 deletions(-) create mode 100755 snaketest.sh delete mode 100644 start.sh diff --git a/snaketest.sh b/snaketest.sh new file mode 100755 index 0000000..d2df877 --- /dev/null +++ b/snaketest.sh @@ -0,0 +1,4 @@ +while true +do +docker run --rm -p 63187:63187 mediaeng/bots snakes +done diff --git a/src/EscapeBot.java b/src/EscapeBot.java index 69c1955..0136dd7 100644 --- a/src/EscapeBot.java +++ b/src/EscapeBot.java @@ -1,6 +1,4 @@ public class EscapeBot extends Bot { - - boolean foundShip = false; boolean offByOne = true; int currentStepCount = 0; int steps = 0; diff --git a/src/SnakeBot.java b/src/SnakeBot.java index ede1736..37f4583 100644 --- a/src/SnakeBot.java +++ b/src/SnakeBot.java @@ -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 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) { + } + } diff --git a/start.sh b/start.sh deleted file mode 100644 index e69de29..0000000