import records.Move; import java.util.LinkedList; import java.util.Queue; import java.util.Random; public class RumbleBot extends Bot { protected final static String obstacles = "~#X*"; protected final static String players = "v^<>"; protected final static String targets = players + "o"; protected Random random = new Random(); protected int steps = 0; public static void main(String[] args) { if (args.length == 0) { String[] dummyArgs = {"localhost", "63187"}; for (int i = 0; i < 5; i++) { new Thread(new EnemyBot(dummyArgs)).start(); } } Bot bot = new RumbleBot(args); bot.run(); } protected RumbleBot(String[] args) { super(args); } @Override protected char nextMove(View view) { int size = view.width; String data = view.data; char[][] grid = dataToGrid(data, size); if (isInLineOfSight(grid)) { return 'f'; } else if (data.contains("v") || data.contains("^") || data.contains("<") || data.contains(">")) { 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++) { char c = data.charAt(i); if (Character.isUpperCase(c) && c != 'X') c = 'A'; grid[i % size][i / size] = c; } return grid; } protected void markFiringLines(char[][] grid) { int[][] dir = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; int size = grid.length; for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { switch (grid[x][y]) { case '^' -> markLine(grid, x, y, dir[0], '*', false); case 'v' -> markLine(grid, x, y, dir[1], '*', false); case '>' -> markLine(grid, x, y, dir[2], '*', false); case '<' -> markLine(grid, x, y, dir[3], '*', false); } if (players.contains("" + grid[x][y])) { for (int[] direction : dir) { markLine(grid, x, y, direction, 'o', true); } } } } } private void markLine(char[][] grid, int x, int y, int[] dir, char c, boolean block) { int size = grid.length; x += dir[0]; y += dir[1]; while (x >= 0 && x < size && y >= 0 && y < size && !obstacles.contains("" + grid[x][y])) { if (grid[x][y] == 'A' && block) break; if (grid[x][y] == '.') { grid[x][y] = c; } x += dir[0]; y += dir[1]; } } protected boolean isInLineOfSight(char[][] grid) { int size = grid.length; for (int y = size / 2; y >= 0; y--) { if (players.contains("" + grid[size / 2][y])) return true; if (obstacles.contains("" + grid[size / 2][y])) break; } return false; } protected char walkAround(char[][] grid) { int size = grid.length; if (steps == 0) { steps = random.nextInt(20); return random.nextBoolean() ? '<' : '>'; } else { steps--; return obstacles.contains("" + grid[size / 2][size / 2 - 1]) ? '<' : '^'; } } 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 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 walkAround(grid); } }