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

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