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.

RumbleBot.java 3.6KB

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