Rumble, Snake and Escape Bot
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 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package praktikum05;
  2. import java.util.Random;
  3. public class RumbleBot extends Bot {
  4. public static void main(String[] args) {
  5. RumbleBot rumbleBot = new RumbleBot(args);
  6. rumbleBot.run();
  7. }
  8. protected RumbleBot(String[] args) {
  9. super(args);
  10. }
  11. @Override
  12. protected char nextMove(View view) throws Exception {
  13. if (view.data.indexOf('*') > -1) {
  14. return shootIfEnemyInRange(view);
  15. } else {
  16. return moveRandomly();
  17. }
  18. }
  19. protected char moveRandomly() {
  20. Random random = new Random();
  21. int randomDirection = random.nextInt(4);
  22. switch (randomDirection) {
  23. case 0:
  24. return '^'; // Vorwärts
  25. case 1:
  26. return 'v'; // Rückwärts
  27. case 2:
  28. return '<'; // Linksdrehung
  29. case 3:
  30. return '>'; // Rechtsdrehung
  31. default:
  32. return 'q'; // Abbruch der Verbindung
  33. }
  34. }
  35. protected char shootIfEnemyInRange(View view) {
  36. int positionEnemy = view.data.indexOf('B');
  37. int positionRover = view.data.indexOf('A');
  38. // Überprüfen, ob der Feind in derselben Zeile istdocker run --rm -p 63187:63187 mediaeng/bots escape
  39. if (positionRover / 5 == positionEnemy / 5) {
  40. return 'f'; // Feuerbefehl
  41. }
  42. // Ansonsten zufällig bewegen
  43. return moveRandomly();
  44. }
  45. }