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.

SnakeBot.java 2.8KB

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