Aylin, Isabella, Edasu, Jasmin
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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package bot;
  2. import java.util.*;
  3. public class EscapeBot extends Bot {
  4. private boolean rocketFound;
  5. private int roverX;
  6. private int roverY;
  7. public EscapeBot(String[] args) {
  8. super(args);
  9. this.rocketFound = false;
  10. this.roverX = -1; // Startposition des Rovers ist unbekannt
  11. this.roverY = -1;
  12. }
  13. @Override
  14. protected char nextMove(View view) throws Exception {
  15. if (rocketFound) {
  16. System.out.println("Rakete gefunden! Rover kehrt zurück.");
  17. return 'V'; // Rückwärts bewegen
  18. }
  19. if (roverX == -1 || roverY == -1) {
  20. for (int y = 0; y < view.width; y++) {
  21. for (int x = 0; x < view.width; x++) {
  22. char cell = view.data.charAt(y * view.width + x);
  23. if (cell == 'A') {
  24. roverX = x;
  25. roverY = y;
  26. }
  27. }
  28. }
  29. }
  30. char[][] grid = new char[view.width][view.width];
  31. for (int y = 0; y < view.width; y++) {
  32. for (int x = 0; x < view.width; x++) {
  33. grid[y][x] = view.data.charAt(y * view.width + x);
  34. }
  35. }
  36. List<Node> path = aStarSearch(grid, roverX, roverY);
  37. if (path != null && !path.isEmpty()) {
  38. Node nextNode = path.get(0);
  39. if (nextNode.x > roverX) {
  40. roverX++;
  41. return 'd'; // Nach rechts bewegen
  42. } else if (nextNode.x < roverX) {
  43. roverX--;
  44. return 'a'; // Nach links bewegen
  45. } else if (nextNode.y > roverY) {
  46. roverY++;
  47. return '^'; // Vorwärts bewegen
  48. } else if (nextNode.y < roverY) {
  49. roverY--;
  50. return 'V'; // Rückwärts bewegen
  51. }
  52. }
  53. return '^'; // Standardbewegung, wenn keine Richtung gefunden wurde
  54. }
  55. private List<Node> aStarSearch(char[][] grid, int startX, int startY) {
  56. PriorityQueue<Node> openList = new PriorityQueue<>(Comparator.comparingInt(a -> a.f));
  57. Set<Node> closedSet = new HashSet<>();
  58. Map<Node, Node> cameFrom = new HashMap<>();
  59. Map<Node, Integer> gScore = new HashMap<>();
  60. Map<Node, Integer> fScore = new HashMap<>();
  61. Node startNode = new Node(startX, startY);
  62. openList.add(startNode);
  63. gScore.put(startNode, 0);
  64. fScore.put(startNode, heuristic(startNode));
  65. while (!openList.isEmpty()) {
  66. Node current = openList.poll();
  67. if (grid[current.y][current.x] == 'o') {
  68. return reconstructPath(cameFrom, current);
  69. }
  70. closedSet.add(current);
  71. for (Node neighbor : getNeighbors(current, grid)) {
  72. if (closedSet.contains(neighbor)) {
  73. continue;
  74. }
  75. int tentativeGScore = gScore.getOrDefault(current, Integer.MAX_VALUE) + 1;
  76. if (!openList.contains(neighbor) || tentativeGScore < gScore.getOrDefault(neighbor, Integer.MAX_VALUE)) {
  77. cameFrom.put(neighbor, current);
  78. gScore.put(neighbor, tentativeGScore);
  79. fScore.put(neighbor, tentativeGScore + heuristic(neighbor));
  80. if (!openList.contains(neighbor)) {
  81. openList.add(neighbor);
  82. }
  83. }
  84. }
  85. }
  86. return null; // Kein Weg gefunden
  87. }
  88. private List<Node> getNeighbors(Node node, char[][] grid) {
  89. List<Node> neighbors = new ArrayList<>();
  90. int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // Rechts, Links, Unten, Oben
  91. for (int[] dir : directions) {
  92. int newX = node.x + dir[0];
  93. int newY = node.y + dir[1];
  94. if (newX >= 0 && newX < grid.length && newY >= 0 && newY < grid.length &&
  95. grid[newY][newX] != '#') {
  96. neighbors.add(new Node(newX, newY));
  97. }
  98. }
  99. return neighbors;
  100. }
  101. private List<Node> reconstructPath(Map<Node, Node> cameFrom, Node current) {
  102. List<Node> path = new ArrayList<>();
  103. path.add(current);
  104. while (cameFrom.containsKey(current)) {
  105. current = cameFrom.get(current);
  106. path.add(current);
  107. }
  108. Collections.reverse(path);
  109. return path;
  110. }
  111. private int heuristic(Node node) {
  112. // Verwenden Sie die Manhattan-Distanz als Heuristik, um die tatsächliche Entfernung zum Ziel zu schätzen
  113. return Math.abs(node.x - roverX) + Math.abs(node.y - roverY);
  114. }
  115. private static class Node {
  116. int x;
  117. int y;
  118. int f;
  119. Node(int x, int y) {
  120. this.x = x;
  121. this.y = y;
  122. this.f = 0;
  123. }
  124. @Override
  125. public boolean equals(Object o) {
  126. if (this == o) return true;
  127. if (o == null || getClass() != o.getClass()) return false;
  128. Node node = (Node) o;
  129. return x == node.x && y == node.y;
  130. }
  131. @Override
  132. public int hashCode() {
  133. return Objects.hash(x, y);
  134. }
  135. }
  136. public static void main(String[] args) {
  137. EscapeBot escapeBot = new EscapeBot(args);
  138. escapeBot.run();
  139. }
  140. }