123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import java.util.LinkedList;
- import java.util.Queue;
-
- public class RumbleBot extends Bot {
- protected final static String obstacles = "~#X*";
- protected final static String targets = "v^<>";
- protected boolean offByOne = true;
- protected int currentStepCount = 0;
- protected int steps = 0;
-
- public static void main(String[] args) {
- if(args.length == 0) {
- String[] dummyArgs = {"localhost", "63187"};
- DummyBot dummy = new DummyBot(dummyArgs);
- new Thread(dummy).start();
- }
-
- Bot bot = new RumbleBot(args);
- bot.run();
- }
-
- protected RumbleBot(String[] args) {
- super(args);
- }
-
- @Override
- protected char nextMove(View view) {
- System.out.println();
-
- int size = view.width;
- String data = view.data;
- char[][] grid = dataToGrid(data, size);
-
- if (data.contains("v") || data.contains("^") || data.contains("<") || data.contains(">")) {
- if (isInLineOfSight(grid)) {
- return 'f';
- } else {
- return breadthFirstSearch(grid);
- }
- } else {
- return walkAround(grid);
- }
- }
-
- protected char[][] dataToGrid(String data, int size) {
- char[][] grid = new char[size][size];
- for (int i = 0; i < data.length(); i++) {
- grid[i % size][i / size] = data.charAt(i);
- }
- return grid;
- }
-
- protected void markFiringLines(char[][] grid) {
- int size = grid.length;
- for (int x = 0; x < size; x++) {
- for (int y = 0; y < size; y++) {
- switch (grid[x][y]) {
- case 'v' -> markLine(grid, x, y, 0, 1);
- case '^' -> markLine(grid, x, y, 0, -1);
- case '>' -> markLine(grid, x, y, 1, 0);
- case '<' -> markLine(grid, x, y, -1, 0);
- }
- }
- }
- }
-
- private void markLine(char[][] grid, int x, int y, int dx, int dy) {
- int size = grid.length;
- x += dx;
- y += dy;
- while (x >= 0 && x < size && y >= 0 && y < size && !obstacles.contains("" + grid[x][y])) {
- if (grid[x][y] == '.') {
- grid[x][y] = '*';
- }
- x += dx;
- y += dy;
- }
- }
-
- protected boolean isInLineOfSight(char[][] grid) {
- int size = grid.length;
- for (int y = size / 2; y > 0; y--) {
- if (obstacles.contains("" + grid[size / 2][y]))
- break;
-
- if (targets.contains("" + grid[size / 2][y]))
- return true;
- }
- return false;
- }
-
- protected char walkAround(char[][] grid) {
- if (steps == 0) {
- currentStepCount = (currentStepCount + 1) % 10;
- if (offByOne) {
- currentStepCount++;
- }
- offByOne = !offByOne;
- steps = currentStepCount;
- return '>';
- } else {
- steps--;
- return safeMove(grid);
- }
- }
-
- protected char safeMove(char[][] grid) {
- int size = grid.length;
- if (obstacles.contains("" + grid[size / 2][size / 2 - 1])) {
- return '<';
- } else {
- return '^';
- }
- }
-
- protected char breadthFirstSearch(char[][] grid) {
- markFiringLines(grid);
- int size = grid.length;
- int start = size / 2;
-
- for (int y = 0; y < size; y++) {
- for (char[] chars : grid) System.err.print(chars[y]);
- System.err.println();
- }
-
- boolean[][] visited = new boolean[size][size];
- Queue<Move> queue = new LinkedList<>();
-
- int[][] directions = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
- char[] commands = {'^', 'v', '>', '<'};
- for (int i = 0; i < 4; i++) {
- queue.add(new Move(start + directions[i][0], start + directions[i][1], commands[i]));
- }
-
- while (!queue.isEmpty()) {
- Move move = queue.poll();
- 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 (targets.contains("" + grid[move.x][move.y])) return move.direction;
-
- for (int[] direction : directions) {
- queue.add(new Move(move.x + direction[0], move.y + direction[1], move.direction));
- }
- }
-
- System.err.println("No path found");
- return safeMove(grid);
- }
-
- protected record Move(int x, int y, char direction) {
- }
-
- }
|