You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EscapeBot.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. public class EscapeBot extends Bot {
  4. protected final static String obstacles = "~#X*";
  5. protected boolean offByOne = true;
  6. protected int currentStepCount = 0;
  7. protected int steps = 0;
  8. public static void main(String[] args) {
  9. Bot bot = new EscapeBot(args);
  10. bot.run();
  11. }
  12. protected EscapeBot(String[] args) {
  13. super(args);
  14. }
  15. @Override
  16. protected char nextMove(View view) {
  17. System.out.println();
  18. String data = view.data
  19. .replace('^', '*')
  20. .replace('<', '*')
  21. .replace('>', '*')
  22. .replace('v', '*');
  23. char[][] grid = dataToGrid(data, view.width);
  24. if (data.contains("o")) {
  25. return breadthFirstSearch(grid);
  26. } else {
  27. return walkAround(grid);
  28. }
  29. }
  30. protected char[][] dataToGrid(String data, int size) {
  31. char[][] grid = new char[size][size];
  32. for (int i = 0; i < data.length(); i++) {
  33. grid[i % size][i / size] = data.charAt(i);
  34. }
  35. return grid;
  36. }
  37. protected char walkAround(char[][] grid) {
  38. if (steps == 0) {
  39. currentStepCount++;
  40. if (offByOne) {
  41. currentStepCount++;
  42. }
  43. offByOne = !offByOne;
  44. steps = currentStepCount;
  45. return '>';
  46. } else {
  47. steps--;
  48. return safeMove(grid);
  49. }
  50. }
  51. protected char safeMove(char[][] grid) {
  52. int size = grid.length;
  53. if (grid[size / 2][size / 2 - 1] == '*') {
  54. return '<';
  55. } else {
  56. return '^';
  57. }
  58. }
  59. protected char breadthFirstSearch(char[][] grid) {
  60. int size = grid.length;
  61. int start = size / 2;
  62. boolean[][] visited = new boolean[size][size];
  63. Queue<Move> queue = new LinkedList<>();
  64. int[][] directions = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
  65. char[] commands = {'^', '>', '>', '<'};
  66. for (int i = 0; i < 4; i++) {
  67. queue.add(new Move(start + directions[i][0], start + directions[i][1], commands[i]));
  68. }
  69. queue.add(new Move(start, start - 1, '^'));
  70. queue.add(new Move(start, start + 1, '>'));
  71. queue.add(new Move(start + 1, start, '>'));
  72. queue.add(new Move(start - 1, start, '<'));
  73. while (!queue.isEmpty()) {
  74. Move move = queue.poll();
  75. if (move.x < 0 || move.x >= size || move.y < 0 || move.y >= size || visited[move.x][move.y]) continue;
  76. visited[move.x][move.y] = true;
  77. if (obstacles.contains("" + grid[move.x][move.y])) continue;
  78. if (grid[move.x][move.y] == 'o') return move.direction;
  79. for (int[] direction : directions) {
  80. queue.add(new Move(move.x + direction[0], move.y + direction[1], move.direction));
  81. }
  82. }
  83. System.err.println("No path found");
  84. return safeMove(grid);
  85. }
  86. protected record Move(int x, int y, char direction) {
  87. }
  88. }