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 2.8KB

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