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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * SnakeBot.
  3. * Eine weitere wichtige Funktion des Rovers ist es, Gesteinsproben zu sammeln.
  4. * Interessante Steine sind im Scan mit einem @ gekennzeichnet.
  5. * Mit jeder aufgesammelten Gesteinsprobe wird an den Rover ein Wagen angehängt, der zukünftig hinter dem Rover mitgezogen wird.
  6. * Die Wagen sind im Scan mit * zu identifizieren.
  7. * Vorsicht: fährt der Rover in einen Wagen, ist er schwer beschädigt und kann keine weiteren Steine mehr sammeln.
  8. * Sie können Ihre Implementierung wieder testen mit:
  9. * docker run --rm -p 63187:63187 mediaeng/bots snakes
  10. * Für diese Funktion wird am 7.2.24 in einer gemeinsamen Arena mit allen Teams des Jahrgangs ein Wettbewerb durchgeführt.
  11. * Die besten acht Teams qualifizieren sich für die Königsdisziplin „Rumble“.
  12. */
  13. import java.util.Random;
  14. public class SnakeBot extends Bot{
  15. private String moves = "";
  16. private boolean frontIsBlocked, backIsBlocked, leftIsBlocked, rightIsBlocked, trapped;
  17. private char previousTurn = '<';
  18. private int spiralFactor = 1;
  19. private char spiralDirection = '>';
  20. private boolean goesForward = true;
  21. public static void main(String[] args) {
  22. Bot snakeBot = new SnakeBot(args);
  23. snakeBot.run();
  24. }
  25. protected SnakeBot(String[] args) {
  26. super(args);
  27. }
  28. protected char nextMove(View view) throws Exception {
  29. boolean stoneDetected = view.data.contains("@");
  30. char nextMove;
  31. checkBarriers(view);
  32. nextMove = stoneDetected ? goToStone(view) : walkBySpiral(view);
  33. nextMove = checkMove(nextMove);
  34. saveMove(nextMove);
  35. return nextMove;
  36. }
  37. private void checkBarriers(View view) throws Exception {
  38. int centerCoordinates = view.width / 2;
  39. int centerIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates);
  40. int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1);
  41. int backIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates + 1);
  42. int leftIndex = centerIndex - 1;
  43. int rightIndex = centerIndex + 1;
  44. char[] obstacles = {'#', '~', 'X', '*'};
  45. frontIsBlocked = containsChar(obstacles, view.data.charAt(frontIndex));
  46. backIsBlocked = containsChar(obstacles, view.data.charAt(backIndex));
  47. leftIsBlocked = containsChar(obstacles, view.data.charAt(leftIndex));
  48. rightIsBlocked = containsChar(obstacles, view.data.charAt(rightIndex));
  49. trapped = frontIsBlocked && backIsBlocked && leftIsBlocked && rightIsBlocked;
  50. if (trapped) {
  51. Exception trappedException = new Exception("Snake is trapped");
  52. System.err.println(trappedException.getMessage());
  53. throw trappedException;
  54. }
  55. }
  56. private boolean containsChar(char[] array, char target) {
  57. for (char c : array) {
  58. if (c == target) {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. private char checkMove(char move) throws Exception {
  65. if (frontIsBlocked) {
  66. resetMovesSequence();
  67. if (leftIsBlocked) move = '>';
  68. else if (rightIsBlocked) move = '<';
  69. else if (previousTurn == '<') move = '>';
  70. else if (previousTurn == '>') move = '<';
  71. else {
  72. Exception invalidTurnException = new Exception("The previous turn was neither '<' nor '>'.");
  73. System.err.println(invalidTurnException.getMessage());
  74. throw invalidTurnException;
  75. }
  76. }
  77. return move;
  78. }
  79. private void saveMove(char move) {
  80. if (move == '<' || move == '>') {
  81. previousTurn = move;
  82. }
  83. }
  84. private char walkBySpiral(View view) {
  85. if (moves.isEmpty()) {
  86. spiralFactor++;
  87. moves += "^".repeat(view.width * spiralFactor) + spiralDirection + "^".repeat(view.width * spiralFactor) + spiralDirection;
  88. }
  89. char nextMove = moves.charAt(0);
  90. moves = moves.substring(1);
  91. return nextMove;
  92. }
  93. private char walkByColumns(View view) {
  94. if(moves.isEmpty()){
  95. int min = 10;
  96. int max = 100;
  97. Random random = new Random();
  98. int steps = random.nextInt((max - min) + 1) + min;
  99. //int steps = 32 - view.width;
  100. moves = "^".repeat(steps);
  101. moves += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<");
  102. goesForward = !goesForward;
  103. }
  104. char nextMove = moves.charAt(0);
  105. moves = moves.substring(1);
  106. return nextMove;
  107. }
  108. private char goToStone(View view) {
  109. resetMovesSequence();
  110. int[] frontIndexes = getFrontIndexes(view);
  111. int[] leftIndexes = getLeftIndexes(view);
  112. int[] rightIndexes = getRightIndexes(view);
  113. for (int index : frontIndexes) {
  114. if (view.data.charAt(index) == '@' && view.data.charAt(index + view.width) != '*' && !frontIsBlocked) return '^';
  115. }
  116. for (int index : leftIndexes) {
  117. if (view.data.charAt(index) == '@' && view.data.charAt(index + 1) != '*' && !leftIsBlocked) return '<';
  118. }
  119. for (int index : rightIndexes) {
  120. if (view.data.charAt(index) == '@' && view.data.charAt(index - 1) != '*' && !rightIsBlocked) return '>';
  121. }
  122. return '^';
  123. }
  124. private int[] getLeftIndexes(View view) {
  125. int center = view.width / 2;
  126. int[] leftIndexes = new int[center];
  127. int index = 0;
  128. for (int col = 0; col < center; col++) {
  129. leftIndexes[index++] = getCharIndexFromCoordinates(view.width, col, center);
  130. }
  131. return leftIndexes;
  132. }
  133. private int[] getRightIndexes(View view) {
  134. int center = view.width / 2;
  135. int[] rightIndexes = new int[center];
  136. int index = 0;
  137. for (int col = center + 1; col < view.width; col++) {
  138. rightIndexes[index++] = getCharIndexFromCoordinates(view.width, col, center);
  139. }
  140. return rightIndexes;
  141. }
  142. private int[] getFrontIndexes(View view) {
  143. int center = view.width / 2;
  144. int[] frontIndexes = new int[center];
  145. int index = 0;
  146. for (int row = 0; row < center; row++) {
  147. frontIndexes[index++] = getCharIndexFromCoordinates(view.width, center, row);
  148. }
  149. return frontIndexes;
  150. }
  151. private int getCharIndexFromCoordinates(int width, int x, int y) {
  152. return width * y + x;
  153. }
  154. private void resetMovesSequence() {
  155. moves = "";
  156. System.out.println("Previous spiralNumber: " + spiralFactor + ". Resetting.");
  157. spiralFactor = 1;
  158. spiralDirection = (spiralDirection == '>') ? '<' : '>';
  159. goesForward = true;
  160. }
  161. }